rsa js pidCrypt encryption java decryption

1.3k views Asked by At

I am trying to encrypt some simple message with pidCrypt in browser, then decrypt it with java crypto on the server. But I kept getting the error message:

javax.crypto.IllegalBlockSizeException: Data must not be longer than 128 bytes

Keys:

keys are generated with openssl genrsa (sslery). The private key then converted to pkcs#8 der format and read into java code

JS code snippet:

/*---* 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());  
0

There are 0 answers