Large data not encrypted with RSA Encryption

前端 未结 2 510
我在风中等你
我在风中等你 2021-01-02 08:44

My Problem:

My encryption code working fine for below 64 characters. but if it exceeds 64 character I got following error<

相关标签:
2条回答
  • 2021-01-02 09:25

    Here is a direct quote from the seminal book titled Cryptography Engineering by Ferguson, Schneier, and Kohno,

    Encrypting a message is the canonical application of RSA, yet it is almost never used in practice. The reason is simple: the size of the message that can be encrypted using RSA is limited by the size of n. In real systems, you cannot even use all the bits, because the encoding function has an overhead. This limited message size is too impractical for most applications, and because the RSA operation is quite expensive in computational terms, you don’t want to split a message into smaller blocks and encrypt each of them with a separate RSA operation.

    In other words, for a n-bit RSA key, the maximum length of data RSA can encrypt in bytes is

    Floor(n/8) - 11 
    

    where 11 bytes is for padding

    So for a key size of 512 bits, the maximum length of data that can be encrypted is,

    512/8 - 11 = 53 bytes
    

    Again from the book Cryptography Engineering,

    The solution used almost everywhere is to choose a random secret key K, and encrypt K with the RSA keys. The actual message m is then encrypted with key K using a block cipher or stream cipher. So instead of sending something like ERSA(m), you send ERSA(K),EK(m).

    Basically, it's telling you do the following to get over the limitation of RSA,

    1. Generate a secret key, K using an algorithm such as AES.
    2. Encrypt the plaintext, m, with the newly generated secret key to get cipher text, say EK(m).
    3. Encrypt the secret key a RSA public key, ERSA(K).
    4. Sent the client the cipher text, EK(m), and the encrypted key ERSA(K).
    5. The client can decrypt ERSA(K) with the RSA private key to get K.
    6. The client then decrypt the cipher text, EK(m) with K to get m.
    0 讨论(0)
  • 2021-01-02 09:26

    The number of bytes you can encrypt in one RSA block is dictated by the key size used minus any bytes taken up for padding.

    Generally RSA is not suited for bulk encryption as it's quite slow. Instead use a symmetric encryption algorithm like AES if you can. If you really need the two key's of RSA, use a Hybrid approach where you encrypt the data with a random symmetric key, and then encrypt that key with the RSA key.

    A benefit of using symmetric encryption is also that the libraries automatically supports bulk encryption - which they don't for RSA.

    0 讨论(0)
提交回复
热议问题