Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

RSA -- org.bouncycastle.crypto.DataLengthException: input too large for RSA

843811Nov 15 2007 — edited Sep 12 2009
Hi
Can some please help me here.
I have a requirement where I need to encrypt using JavaScript and decrypt in Java. Here is my sample code for this. I am using RSA with Bouncy Castle provider.

		
		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
		KeyPairGenerator RSAkeyPairGen = KeyPairGenerator.getInstance("RSA","BC");
		SecureRandom sunSha1prngSecRand = SecureRandom.getInstance("SHA1PRNG","SUN");
		sunSha1prngSecRand.setSeed(System.currentTimeMillis());
		// for demonstration purposes we generate a key-pair each time.
		//RSAkeyPairGen.initialize(1024,sunSha1prngSecRand);
		RSAkeyPairGen.initialize(128,sunSha1prngSecRand);
		rsaKeyPair = RSAkeyPairGen.generateKeyPair();

	RSAPrivateKey rsaPrivKey = (RSAPrivateKey) rsaKeyPair.getPrivate();
	String privateE = rsaPrivKey.getPrivateExponent().toString(16);
	String modulus = rsaPrivKey.getModulus().toString(16);

session.setAttribute("keyPair", rsaKeyPair);
I got the supporting JS files at http://www.ohdave.com/rsa/.
Most of the cases it works perfect. But few times it throws org.bouncycastle.crypto.DataLengthException: input too large for RSA.
I see there are some posting abott this error and I am not able to incorporate in this code to work. I also noticed if the encrypted first byte is >8 (i.e 8,9,a,b,c,d,e) the error occurs else it works perfect. I really appreciate if some one can help me.

Javascript Code :
setMaxDigits(76);
key = new RSAKeyPair("<%=privateE%>", "", "<%=modulus%>");
document.secretform.secret.value = encryptedString(key, document.secretform.secret.value);
Here is the decrypted code :

	KeyPair rsaKeyPair = (KeyPair) session.getAttribute("keyPair");
	String secret = request.getParameter("secret");
	out.println(rsaKeyPair+"secret:"+secret);
	
	Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
	PublicKey rsaPubKey = rsaKeyPair.getPublic();
		
	Cipher decrypt = Cipher.getInstance("RSA","BC");
	decrypt.init(Cipher.DECRYPT_MODE,rsaPubKey);
	// if data encrypted was too long on browser, it has been chunked into
	// separately encrypted pieces - separated by space.
	String[] chunks = secret.split(" ");
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	for (int i = chunks.length-1; i >= 0; i--) {
		BigInteger iSecret = new BigInteger(chunks, 16);
byte[] ciphertext = iSecret.toByteArray();
//decrypt.update(ciphertext);
byte[] buf = decrypt.doFinal(ciphertext);
baos.write(buf);
}
byte[] decrypted = baos.toByteArray();
String value = new String(decrypted, "UTF-8");
// for some reason, with the javascript package the value is getting reversed?
StringBuffer sb = new StringBuffer(value);
value = sb.reverse().toString();

out.println("Decrypted Value:"+value);
Edited by: Dileep on Nov 15, 2007 2:21 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 10 2009
Added on Nov 15 2007
9 comments
2,926 views