这可能是已回答问题的重复,但是我似乎无法获得相同的结果。希望在这里有一些指导。
JSEncrypt(客户端)
let encrypt = new Encrypt.JSEncrypt();
encrypt.setPublicKey(this.publicKey); // retrieved from server
encrypt.encrypt(password);
Run Code Online (Sandbox Code Playgroud)
BouncyCastle(服务器)-RSA密钥生成
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(1024);
KeyPair pair = generator.generateKeyPair();
PublicKey pubKey = pair.getPublic();
PrivateKey privKey = pair.getPrivate();
// returned to client
String publicKeyStr = new String(Base64.encodeBase64(pubKey.getEncoded()));
String privateKeyStr = new String(Base64.encodeBase64(privKey.getEncoded()));
Run Code Online (Sandbox Code Playgroud)
BouncyCastle(服务器)-解密
Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
// org.apache.commons.codec.binary.Hex
byte[] cipherText = cipher.doFinal(Hex.decodeHex(encrypted.toCharArray()));
decrypted = new String(cipherText, BaseConstant.ENC_UTF8);
Run Code Online (Sandbox Code Playgroud)
错误
org.apache.commons.codec.DecoderException:org.apache.commons.codec.binary.Hex.toDigit(Hex.java:178)处索引0处的非法十六进制字符I在org.apache.commons.codec.binary.Hex中.decodeHex(Hex.java:89)
我注意到的一件事是JSEncrypt加密的文本的长度为172,而服务器端的加密则为256。
提到的已回答问题是使用我已经设置的RSA / None / PKCS1Padding。我还能缺少什么?