views:

1900

answers:

1

I've got a message contained in an byte[], encrypted with "RSA/ECB/PKCS1Padding". To decrypt it I create a Cipher c and initiate it with

c = Cipher.getInstance("RSA/ECB/PKCS1Padding");

Untill now I have only decrypted small messages, using the doFinal() method, returning an byte[] with the decrypted bytes.

c.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptetBytes = c.doFinal(encryptedBytes);

But in this case the data is bigger (approx 500 Bytes), and the doFinal()-method throws an exception (javax.crypto.IllegalBlockSizeException: Data must not be longer than 128 bytes). I guess I need to use the update()- method, but I can't figure out how to get it to work properly. How is this done?

+1  A: 

Answer from schnaader isn't quite correct.

byte[] message = ...
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
cipher.update(message);
byte[] plaintext = cipher.doFinal();

As usual care should be taken if you're using the getBytes() method on String objects. You should always be specifying the encoding to be used. This can easily bite you when encrypting on one platform and decrypting on another.

Can't remember right now but if it looks like the output of doFinal() is only the end of your plaintext you may need to collect the byte[] returned by cipher.update(message);

koregan