0

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());  
neubert
  • 15,947
  • 24
  • 120
  • 212
rickcoup
  • 275
  • 1
  • 5
  • 20
  • possible duplicate of [getting a IllegalBlockSizeException: Data must not be longer than 256 bytes when using rsa](http://stackoverflow.com/questions/10007147/getting-a-illegalblocksizeexception-data-must-not-be-longer-than-256-bytes-when) – Oleg Estekhin May 30 '14 at 17:12
  • Please use the search function, it is that easy: [Data must not be longer than 128 bytes](http://stackoverflow.com/search?q=Data+must+not+be+longer+than+128+bytes) – Oleg Estekhin May 30 '14 at 17:13
  • I don't think it's the same issue here. I only encrypted "hello". The crypted is " 19c7d98adeac0f52a9a91df2cb90d1782197d621606b280f5b5e2111ded0308c1329123651fe73677f50a6a3cbc6c8f71923653c8e7cfc51f779459299160cbe9784b3359e322dd5eeaf9439fb8326a7d629da121459b69a1409b260040ba43bcc65e10fa9b0bb9d55412ee6e4d652b1df0aeeba2a686dcb59dc79a57eb27de0" So the java side thinks it's too long. Any more suggestions? – rickcoup May 30 '14 at 17:55

0 Answers0