0
点赞
收藏
分享

微信扫一扫

RSA数字签名算法

搬砖的小木匠 2022-04-17 阅读 70

RSAPublicKey rsapubKey = (RSAPublicKey)keyPair.getPublic();

RSAPrivateKey rsapriKey = (RSAPrivateKey)keyPair.getPrivate();

map.put(“pubKey”, rsapubKey);

map.put(“priKey”, rsapriKey);

return map;

}

/**

  • 私密加密

  • @para Android开源项目《ali1024.coding.net/public/P7/Android/git》 m data

  • @param priKey

  • @return

  • @throws Exception

*/

public static String encryptByPriKey(String data, RSAPrivateKey priKey){

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(priKey.getEncoded());

KeyFactory keyFactory;

PrivateKey key;

Signature sign;

byte[] result = null;

try {

keyFactory = KeyFactory.getInstance(“RSA”);

key = keyFactory.generatePrivate(keySpec);

sign = Signature.getInstance(“MD5withRSA”);

sign.initSign(key);

sign.update(data.getBytes());// 处理的内容

result = sign.sign();

} catch (Exception e) {

e.printStackTrace();

}

System.out.println(“rsa sign:”+Hex.encodeHexString(result));

System.out.println(“sign len:”+result.length+",str hex:"+Hex.encodeHexString(result).length());

return Hex.encodeHexString(result);

}

/**

  • 公钥验证

  • @param data

  • @param pubKey

  • @param res

  • @return

  • @throws Exception

*/

public static boolean decryptByPubKey(String data, RSAPublicKey pubKey, String res) {

System.out.println(“res len:”+res.length());

X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(pubKey.getEncoded());

KeyFactory keyFactory;

PublicKey key;

Signature sign;

boolean result = false;

try {

keyFactory = KeyFactory.getInstance(“RSA”);

key = keyFactory.generatePublic(x509KeySpec);

sign = Signature.getInstance(“MD5withRSA”);

sign.initVerify(key);

sign.update(data.getBytes());

result = sign.verify(Hex.decodeHex(res.toCharArray()));

} catch (Exception e) {

e.printStackTrace();

}

System.out.println(“rsa veri 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》开源 fy:”+result);

return result;

}

}

package com.pro.rsa;

import java.security.interfaces.RSAPrivateKey;

import java.security.interfaces.RSAPublicKey;

import java.util.HashMap;

public class RSA {

public final static String src = “hello world hello world hello world hello world hello world hello worldhello worldhello world hello world hello world hello world hello worldhello world hello world hello world hello world v hello world hello world hello world vhello world hello world”;

尾声

最后,我再重复一次,如果你想成为一个优秀的 Android 开发人员,请集中精力,对基础和重要的事情做深度研究。

对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。 整理的这些架构技术希望对Android开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。

这里,笔者分享一份从架构哲学的层面来剖析的视频及资料分享给大家梳理了多年的架构经验,筹备近6个月最新录制的,相信这份视频能给你带来不一样的启发、收获。

Android进阶学习资料库

一共十个专题,包括了Android进阶所有学习资料,Android进阶视频,Flutter,java基础,kotlin,NDK模块,计算机网络,数据结构与算法,微信小程序,面试题解析,framework源码!

Android进阶学习资料库

一共十个专题,包括了Android进阶所有学习资料,Android进阶视频,Flutter,java基础,kotlin,NDK模块,计算机网络,数据结构与算法,微信小程序,面试题解析,framework源码!
[外链图片转存中…(img-HmTa5jW0-1650172737154)]

举报

相关推荐

0 条评论