采用hutool工具生成
KeyPair keyPair = SecureUtil.generateKeyPair("RSA"); PrivateKey aPrivate = keyPair.getPrivate(); String privateEncode = Base64.encodeUrlSafe(aPrivate.getEncoded()); String publicEncode = Base64.encodeUrlSafe(keyPair.getPublic().getEncoded());
String privateKey=""; String publicKey=""; String rsaPassword=""; RSA rsa=new RSA(privateKey,null); byte[] encrypt = rsa.encrypt(StrUtil.bytes(password, CharsetUtil.UTF_8), KeyType.PublicKey); String decrypt2= rsa.decryptStr(rsaPassword, KeyType.PrivateKey); System.out.println("解密后:"+decrypt2);
提示:RSA对象为Hutool工具对象,初始化只用其中一个密钥即可,不要指定算法。
错误示范:
RSA rsa=new RSA("RSA",privateKey,null);
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/jsencrypt/3.3.2/jsencrypt.js"></script> <script type="text/javascript"> function test(){ let encrypt = new JSEncrypt(); encrypt.setPublicKey('MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCgnN_dFn6aWl2GKEiesdNV0uEuSURE7XBGrHpGc-zqW1F9lJTxfbTC6o7pcrdzCtHlplhPuXPuJqEM_hGYbK3pcggsXL13DKDrCEftYjgNBjHHYY4DnrPW6Sf8Iro4xISw2PtyrkdTFNegp-9pTUC1n9F33KLIAKncuCEdK8dQiQIDAQAB');//此处为RSA公钥,public.pem let passWord="123456"; passWord = encrypt.encrypt(passWord);//加密后的密码 console.log(passWord) alert(passWord) } test(); </script>
https://www.leftso.com/article/1683451092928565250.html