Javax.crypto.IllegalBlockSizeException

javax.crypto.IllegalBlockSizeException: data should not be longer than 53 bytes I know that because of the RSA algorithm it can only encrypt data that has a maximum RSA key length byte length in bits divided by eight minus eleven indent bytes, i.e. the number maximum bytes = key length in bits / 8-11.

Here I use a key size of 512, so it does not allow more than 53. But I need to support a 512-bit key, but there is some possibility for encryption of more than 53 bytes.

+6
source share
2 answers

Yes and no. You cannot encrypt using RSA, but you can go to one of the following:

  • The usual encryption approach is used with a symmetric algorithm and the transmission of a key encrypted using RSA. For example, to send D data to another person with the PK public key:

    • send Ek (D) (D is encrypted with a symmetric algorithm with key K)
    • send also Epk (K) (K is encrypted with RSA algorithm with PK)

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

  • split the data into small parts and encrypt each separately.

The first approach is much better for two main reasons:

  • You will not ruin the data (except for encryption itself).
  • Symmetric encryption / decryption is much faster than public encryption, for example, RC4 is a simple XOR data, while RSA uses a lot of power.

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

+7
source

Why aren't you using wrapping? Create a symmetric key (AES), encrypt the data with this AES key, then encrypt the AES key with the RSA public key. Send the encrypted data along with the encrypted AES key. Then decrypt the AES key using the RSA private key and use it to decrypt the rest of the data. This will allow you to encrypt data of any size.

+1
source

Source: https://habr.com/ru/post/887629/


All Articles