我正在尝试使用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 code snippet:
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, myPrivKey);
byte[] descryptedData = cipher.doFinal(cyphertext.getBytes());