RSA Encryption, getting bad length

旧巷老猫 提交于 2019-11-27 03:30:05
blowdart

RSA encryption is only mean for small amounts of data, the amount of data you can encrypt is dependent on the size of the key you are using, for example for 1024 bit RSA keys, and PKCS # 1 V1.5 padding, you can encrypt 117 bytes at most, with a 2048 RSA key, you can encrypt 245 bytes.

There's a good reason for this, asymmetric encryption is computationally expensive. If you want to encrypt large amounts of data you should be using symmetric encryption. But what if you want non-repudiation? Well what you then do is use both. You create a symmetric key and exchange it using asymmetric encryption, then that safely exchanged symmetric key to encrypt your large amounts of data. This is what SSL and WS-Secure use underneath the covers.

ObjectType

For future searches regarding RSA bad length exceptions...

You can calculate the max number of bytes which can be encrypted with a particular key size with the following:

((KeySize - 384) / 8) + 37

However, if the optimal asymmetric encryption padding (OAEP) parameter is true, as it is in the original post, the following can be used to calculate the max bytes:

((KeySize - 384) / 8) + 7

The legal key sizes are 384 thru 16384 with a skip size of 8.

AndyUK

As explained above, the solution to the 'bad length' type exceptions is to hybridize the use of symmetric and asymmetric encryption, so that the size of the text you are encrypting is not constrained by the key size. You basically use RSA encryption to asymmetrically encrypt the random key .

For encryption:

  1. Generate a random key of the length required for symmetrical encryption technique such as AES or Rijndael.

  2. Symmetrically encrypt your text/data using AES/Rijndael using the random key generated in step 1.

  3. Using RSA, asymmetrically encrypt the random key generated in step 1.

For decryption:

  1. First decrypt the AES/Rijndael-generated random key using your private RSA key.

  2. Then decrypt the original text/data using the RSA-decrypted random key

For a demonstration, you may wish to have a look this following example in C#:

http://www.technical-recipes.com/2013/using-rsa-to-encrypt-large-data-files-in-c/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!