Share via


How to encrypt large data using .net asymmetric encryption algorithm

Question

Wednesday, October 1, 2014 10:58 AM

Hi,

I have implemented RSA crypto provider to achieve asymmetric encryption. However it works only for smaller range of data. Can somebody please help me with exact solution around how can I get large data encrypted using Asymmetric algorithm. I have to use Asymmetric algorithms ONLY and not symmetric ones. Please help.

I have also read that RSA algorithms are not for larger data encryption, but what choice do I have then to encrypt large data. Please help me with exact solution, if you can share code that will be great.

Following is what I have implemented at the moment which works only for smaller data (say 120 bytes input data length)-

public class MyEncryptor
    {
        static RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider(1024);
        
        public string Encrypt(string data, string publicKey)
        {
            try
            {
                rsaCSP.FromXmlString(publicKey);
                var encryptedData = rsaCSP.Encrypt(Encoding.UTF8.GetBytes(data), true);
                return Convert.ToBase64String(encryptedData);
            }
            catch (Exception ex)
            {
                throw new EncryptionException("Error when calling the Encrypt method", ex.InnerException);
            }
        }

        public string Decrypt(string data, string privateKey)
        {
            try
            {
                rsaCSP.FromXmlString(privateKey);
                var resultBytes = Convert.FromBase64String(data);
                var decryptedBytes = rsaCSP.Decrypt(resultBytes, true);
                return Encoding.UTF8.GetString(decryptedBytes);
            }
            catch (Exception ex)
            {
                throw new DecryptionException("Error when calling the Decrypt method", ex.InnerException);
            }
        }
    }

All replies (4)

Thursday, October 2, 2014 7:28 AM

Hi,

It is not recommended to use asymmetric algorithms for large data, I suggest this articles: http://dotnetcodr.com/2013/11/11/introduction-to-asymmetric-encryption-in-net-cryptography/

and

http://stackoverflow.com/questions/1199058/how-to-use-rsa-to-encrypt-files-huge-data-in-c-sharp


Thursday, October 2, 2014 7:35 AM

Hello Divya,

Please take a look at the answers in these two threads that about how to encrypt large data using RSA.

RSA Encryption of large data in C#

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

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.


Friday, October 3, 2014 7:22 AM

Hi,

I have checked all these links shared before posting my question on this forum. Is everybody saying there is alternative way of achieving this? Any work around to do the same?

Please guide.


Thursday, March 15, 2018 11:01 AM

I know this is very old, but did you reach to any answer? I am facing the same issue where I can only encrypt/decrypt small files but the customer is asking for large files and providing me with .pfx file to use. Any support would be appreciated