how to use RSA to encrypt files (huge data) in C#

后端 未结 6 1915
悲&欢浪女
悲&欢浪女 2020-11-28 02:38

I\'m new to encryption. I need to implement asymmetric encryption algorithm, which i think it uses private/public key. I started using a sample of RSACryptoServiceProvider.

相关标签:
6条回答
  • 2020-11-28 02:43

    As mentioned in other answers asymmetric encryption is only designed for encrypting data smaller than its key size.

    One option that I have implemented when needing to transfer large amounts of encrypted data between two systems is to have an RSA keypair whose public key is known to both the sender and the receiver then when data needs to be sent the receiver generates a new RSA keypair, encrypts the public key of that keypair with the common public key and sends the encrypted public key to the sender. The sender decrypts the receivers public key using its private key (which the receiver does not need to know, just as the sender does not need to know the receivers generated private key), generates a symmetric encryption key, encrypts the data with the symmetric key and then encrypts the symmetric key using the public key received from the receiver. Both the encrypted symmetric key and the encrypted data are then sent to the receiver which uses its generated private key to decrypt the symmetric key and then decrypts the data.

    You can use the RSACryptoServiceProvider.ToXMLString() and RSACryptoServiceProvider.FromXMLString() methods to store the common public key as an XML string literal in the receiver application.

    Don't forget, when you generate the symmetric encryption key to use RNGCryptoServiceProvider() to generate the key as it is a much more secure method of generating (pseudo) random numbers.

    Also, I strongly recommend against using 3DES as your symmetric encryption algorithm, it is old and starting to show its age. Use AES symmetric encryption with either the AesCryptoServiceProvicer or RijndaelManaged classes.

    0 讨论(0)
  • 2020-11-28 02:44

    we have:

    MaxBlockSize=((KeySize - 384) / 8) + 37
    

    OR

    MaxBlockSize=((KeySize - 384) / 8) + 7
    

    so, we can divide Data to some blocks and then encrypt each one and then merge them

    0 讨论(0)
  • 2020-11-28 02:48

    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, 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.

    0 讨论(0)
  • 2020-11-28 02:50

    Usually, RSA is only used to transfer a symmetric key (at the start of the stream for example) and then the bulk data is encrypted with that key.

    Asymmetric encryption isn't efficient enough to transfer a lot of data.

    0 讨论(0)
  • 2020-11-28 02:51

    RSA can only encrypt data blocks that are shorter than the key length so what you normally do is

    1. Generate a random key of the correct length required for AES (or similar).
    2. Encrypt your data using AES or similar using that key
    3. Encrypt the random key using your RSA key

    Then you publish both the outputs from 2 and 3

    To decrypt

    1. Decrypt the AES key using your RSA key.
    2. Decrypt the data using that AES key
    0 讨论(0)
  • 2020-11-28 02:56

    The .NET implementations of RSA (and all public/private key algorithms) do not support large blocks of data - because that's not the aim of public/private key.

    Instead what you would do is generate a new symmetric key and use that to encrypt the data. Then you use public/private key to encrypt the symmetric key and exchange it with the other party securely. They then decrypt the symmetric key and use it to unencrypt your data.

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