IllegalBlockSizeException: Data must not be longer than 245 bytes
843810Nov 25 2004 — edited Nov 26 2004Hi.. I'm having a problem using RSA encryption. When the data gets to big java throws an IllegalBlockSizeException. Thus far I've been un-able to find any kind of solution.. so hopefully someone can help!
Im running a system based on secure sockets. However I also want to be able to encrypt data files using 2048bit RSA and then send them over the secure socket connection. As part of this process I have setup an RSA key exchange where data is encrypted by using a local key and the the public key of the place its being sent to. However whenever I try to encrypt just a single key the IllegalBlockSizeException is thrown.
I have chosen not to include all of my coding because most it isnt relevant to the problem. However I have formulated a cut-down version below:
cipher = Cipher.getInstance("RSA");
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA);
keyGen.initialize(2048);
KeyPair keyPair = keyGen.generateKeyPair();
Key nPrivateKey = (Key) keyPair.getPrivate();
Key nPublicKey = (Key) keyPair.getPublic();
Vector args = new Vector();
try {
byte[] encrypted1 = encrypt(nPublicKey.getEncoded(),privateKey);
//Error is thrown by line above.
//byte[] encrypted2 = encrypt(encrypted1,serverPublicKey);
//sendMessage("KEY",args,encrypted1);
//newPublicKey = nPublicKey;
//newPrivateKey = nPrivateKey;
//newKeyAcknowledged = false;
}
catch (Exception e) { System.out.println("Exception caught "+e); }
private byte[] encrypt(byte[] input, Key key) throws Exception {
cipher.init(Cipher.ENCRYPT_MODE, key);
cipher.update(input);
return cipher.doFinal(); //Error is thrown from this line
}
Thanks
-Rich