Discussion:
IllegalBlockSizeException
(too old to reply)
Tumzadoc
2005-06-10 08:32:01 UTC
Permalink
Hi
I'm trying to encrypt a 256 bytes of data using a 2048 bits RSA key.
----Code Snippet----
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, rsaPublic);
encryptedData = cipher.doFinal(originalData);
----End of Code Snippet----

It gives me the following exception :
javax.crypto.IllegalBlockSizeException: Data must not be longer than 245
bytes

Is it possible to encrypt data of over 246 bytes using RSA? If yes how do
i go about doing it

Thanx
dala
2005-06-12 19:01:12 UTC
Permalink
Hi,

The RSA algorithm can only encrypt data that has a maximum byte length
of the RSA key length in bits divided with eight minus eleven padding
bytes, i.e. number of maximum bytes = key length in bits / 8 - 11. In
your case it means 2048 / 8 - 11 = 245.
If you want to encrypt larger data, then use a larger key, for example,
a key with 4096 bits will allow you to encrypt 501 bytes of data.

You can read more about it in the RFC 3447 (PKCS#1):
ftp://ftp.isi.edu/in-notes/rfc3447.txt

Regards,
Tommy Grändefors
Post by Tumzadoc
Hi
I'm trying to encrypt a 256 bytes of data using a 2048 bits RSA key.
----Code Snippet----
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, rsaPublic);
encryptedData = cipher.doFinal(originalData);
----End of Code Snippet----
javax.crypto.IllegalBlockSizeException: Data must not be longer than 245
bytes
Is it possible to encrypt data of over 246 bytes using RSA? If yes how do
i go about doing it
Thanx
Mr. Skeptic
2005-06-14 01:21:53 UTC
Permalink
Try

Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
Tumzadoc
2005-06-14 09:23:22 UTC
Permalink
I tried
Cipher.getInstance("RSA/ECB/NoPadding");

it works sometimes, but sometimes it gives me this exception :
javax.crypto.BadPaddingException: Message is larger than Modulus.
Mr. Skeptic
2005-06-16 00:47:22 UTC
Permalink
This is probably true. It is common parlance to describe the modulus N
as being "k bits" in length (2048 in your example), but this just means
that
2^(k-1) < N < 2^k. To properly use RSA, you must ensure that your
message is not merely limited to k bits, but more specifically that it
is less than N. This condition is necessary to ensure unique
decryption.

This is just one of the issues that is solved by using PKCS1 padding
(the default) instead of NoPadding. The downside is that your
transmission efficiency is reduced because, as the previous responder
stated, you can only encrypt 2048-88 bits for every 2048 bits you send.
Loading...