我正在尝试在浏览器中使用 pidCrypt 加密一些简单的消息,然后在服务器上使用 java crypto 对其进行解密。但我不断收到错误消息:
javax.crypto.IllegalBlockSizeException:数据不能超过 128 字节
键:
密钥是使用 openssl genrsa (sslery) 生成的。然后将私钥转换为pkcs#8 der格式并读入java代码
JS代码片段:
/*---* ENCRYPT: RSA 1024 bit ---------*/
// public key
var params = certParser(publickey);
var key = pidCryptUtil.decodeBase64(params.b64);
// new RSA instance
var rsa = new pidCrypt.RSA();
/* RSA encryption
* get the modulus and exponent from certificate (ASN1 parsing)
* pem(Array of Bytes)
*/
// ASN1 parsing
var asn = pidCrypt.ASN1.decode(pidCryptUtil.toByteArray(key));
var tree = asn.toHexTree();
// setting the public key for encryption with retrieved ASN.1 tree
rsa.setPublicKeyFromASN(tree);
/*** encrypt */
var crypted = rsa.encrypt(plaintext);
//var crypted64 = pidCryptUtil.encodeBase64(crypted);
java代码片段:
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, myPrivKey);
byte[] descryptedData = cipher.doFinal(cyphertext.getBytes());