Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. it was ok with small data to encrypt. But when using it on relatively larger data "2 lines", i get the exception CryptographicException "Bad Length"!

//Create a new instance of RSACryptoServiceProvider. using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) {      //Import the RSA Key information. This only needs     //toinclude the public key information.     //RSA.ImportParameters(RSAKeyInfo);     byte[] keyValue = Convert.FromBase64String(publicKey);     RSA.ImportCspBlob(keyValue);      //Encrypt the passed byte array and specify OAEP padding.       //OAEP padding is only available on Microsoft Windows XP or     //later.       encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding); } 

Then I found some samples of encrypting large data (or files) by using CryptoStream, and only use symmetric algorithms like DES or 3DES, which have the function CreateEncryptor to return ICryptoTransform as one of the input to the constructor of CryptoStream!!!

CryptoStream cStream = new CryptoStream(fStream,                 new TripleDESCryptoServiceProvider().CreateEncryptor(Key, IV),                 CryptoStreamMode.Write); 

What is the way to encrypt files using RSA?

like image 439
ala Avatar asked Jul 29 '09 09:07

ala


People also ask

Can RSA encrypt large data?

RSA was not designed to work with large amount of data . You can process messages only with limited length, that depends on the key size. The bigger key is, the bigger message can be encrypted. Be aware that using big key sizes will increase encryption time and may affect application performance.

How do I encrypt a large file with RSA?

The answer is to encrypt the data with a symmetric algorithm such as AES which is designed to encrypt small and large data. If an RSA public/private key pair are required encrypt the symmetric (AES) key with RSA.

How much data can you encrypt with RSA?

RSA, as defined by PKCS#1, encrypts "messages" of limited size. With the commonly used "v1. 5 padding" and a 2048-bit RSA key, the maximum size of data which can be encrypted with RSA is 245 bytes. No more.

Why is RSA not suitable to encrypt large amounts of data?

Simply, RSA is very resource expensive algorithm, it takes time to generate RSA keys and to perform operations on these enormous prime numbers. As the size of data increases, the process load increases and the whole thing ends up taking too much time to complete.


1 Answers

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
like image 154
jcoder Avatar answered Sep 20 '22 11:09

jcoder