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 verify:”+result);
return result;
}
}
RSA.java
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”;
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”;
public static void main(String[] args) {
// 登录rosp返回给客户端
HashMap<String, Object> map = RSAUtil.getKeys();
RSAPublicKey pubKey = (RSAPublicKey) map.get(“pubKey”);
RSAPrivateKey priKey = (RSAPrivateKey) map.get(“priKey”);
// 客户端get提交给服务器
String res= RSAUtil.encryptByPriKey(src, priKey);
// 服务端验证
RSAUtil.decryptByPubKey(_src, pubKey, res);
}
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
最后
95191)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
[外链图片转存中…(img-WUnXru5M-1710672295191)]
最后
想要了解更多关于大厂面试的同学可以**点击这里免费获取《面试文档》**除此之外,我也分享一些免费的优质资源,包括:Android学习PDF+架构视频+源码笔记,高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料 这几块的内容。分享给大家,非常适合近期有面试和想在技术道路上继续精进的朋友。快来获取学习资料吧~