简体   繁体   English

RSA使用JSEncrypt加密并使用BouncyCastle(Java)解密

[英]RSA encrypt using JSEncrypt and decrypt using BouncyCastle (Java)

This might be a duplicate of this answered question , but I can't seem to get the same results. 这可能与这个已回答的问题重复,但我似乎无法得到相同的结果。 Hoping for some guidance here. 希望在这里有一些指导。

JSEncrypt (client) JSEncrypt(客户端)

let encrypt = new Encrypt.JSEncrypt();
encrypt.setPublicKey(this.publicKey);  // retrieved from server
encrypt.encrypt(password);

BouncyCastle (server) - RSA key generation 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()));

BouncyCastle (server) - Decryption 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);

Error 错误

org.apache.commons.codec.DecoderException: Illegal hexadecimal character I at index 0 at org.apache.commons.codec.binary.Hex.toDigit(Hex.java:178) at org.apache.commons.codec.binary.Hex.decodeHex(Hex.java:89) org.apache.commons.codec.DecoderException:org.apache.com上的非法十六进制字符I org.apache.com上的org.apache.com上的org.apache.com上的org.apache.com。 .decodeHex(Hex.java:89)

One thing I noticed is the length of encrypted text by JSEncrypt, which is 172, while encryption at server side produces 256. 我注意到的一件事是JSEncrypt的加密文本长度为172,而服务器端的加密产生256。

The answered question mentioned to use RSA/None/PKCS1Padding, which I had already set. 回答的问题提到使用RSA / None / PKCS1Padding,我已经设置了。 What else could I be missing? 我还能错过什么?

The error occurs in Hex.decodeHex() method, which means that your data is not a Hex encoded string. Hex.decodeHex()方法中发生错误,这意味着您的数据不是十六进制编码的字符串。

JSEncrypt.encrypt() method returns the encrypted data in Base64 (instead of Hex string). JSEncrypt.encrypt()方法返回Base64中的加密数据(而不是Hex字符串)。 In order to decrypt it, you must decode it from base64 format. 要解密它,您必须从base64格式解码它。

So instead of: 所以代替:

byte[] cipherText = cipher.doFinal(Hex.decodeHex(encrypted.toCharArray()));

Do this: 做这个:

byte[] cipherText = cipher.doFinal(Base64.decodeBase64(encrypted.toCharArray()));

You can also solve this problem just from the client side. 您也可以从客户端解决此问题。 See the code below: 请参阅以下代码:

let encrypt = new Encrypt.JSEncrypt(); 
encrypt.setPublicKey(this.publicKey);
encrypt.getKey().encrypt(password);

Just add getKey() after encrypt . 只需在encrypt后添加getKey() It worked for me! 它对我有用! I encrypted my password into Hex string using this approach. 我使用这种方法将密码加密为Hex字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM