Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.crypto.IllegalBlockSizeException

javax.crypto.IllegalBlockSizeException: Data must not be longer than 53 bytes i know that it is because of 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.

Here am using a 512 key size so it is not allowing more than 53.but i need to maintain 512 bit key but to encrypt more than 53 byte is there any possibility .

like image 624
Lalchand Avatar asked May 09 '11 14:05

Lalchand


1 Answers

Yes and no. You can't encrypt with RSA, but you can go with one of the following:

  1. Do the usual approach of encrypting with symmetric algorithm and passing the key encrypted with RSA. For example, to send data D to another man with public key PK:

    1. send Ek(D) (D encrypted with symmetric algorithm with key K)
    2. send Epk(K) as well (K encrypted with RSA algorithm with PK)

    The other side open PK(K) to get K, and open K(D) to get D.

  2. split the data into small pieces and encrypt each one separately.

The first approach is a much better one for two main reasons:

  1. You don't mess with the data (except of the encryption itself).
  2. Symmetric encryption / decryption is much faster than public encryption, for example, RC4 is a simple XOR of the data, while RSA uses a large power.

(3. There must be a reason why PGP is so common...)

like image 97
MByD Avatar answered Oct 05 '22 11:10

MByD