Click here to Skip to main content
15,938,218 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All

I have generated public-private key pair(RSA 4096 bits) using puttygen and I am using this key in my C#.net code. Basically the idea behind generating these keys is to give the public key to the client and they would encrypt the file using this public key and put this file on a secured server and we would download this through SFTP and decrypt it.
Now that the client is not yet ready , for my coding purpose I thought I would encrypt the test file myself using the public key and I was able to do it.
Now when I am using this encrypted file and the private key, it says the key doesn't exists.
I am unable to proceed any further and this is really urgent.
I am working with the Encryption/Decryption for the first time.
Any help would be appreciated.

C#
public  static void Encrypt(string publicKeyFileName, string plainFileName, string encryptedFileName)

        {

            // Variables

            CspParameters cspParams = null;

            RSACryptoServiceProvider rsaProvider = null;

            StreamReader publicKeyFile = null;

            StreamReader plainFile = null;

            FileStream encryptedFile = null;

            string publicKeyText = "";

            string plainText = "";

            byte[] plainBytes = null;

            byte[] encryptedBytes = null;

            try

            {

                // Select target CSP

                cspParams = new CspParameters();

                cspParams.ProviderType = 1; // PROV_RSA_FULL

                //cspParams.ProviderName; // CSP name

                rsaProvider = new RSACryptoServiceProvider(cspParams);

                // Read public key from file

                publicKeyFile = File.OpenText(publicKeyFileName);

                publicKeyText = publicKeyFile.ReadToEnd();

                // Import public key

                rsaProvider.FromXmlString(publicKeyText);

                // Read plain text from file

                plainFile = File.OpenText(plainFileName);

                plainText = plainFile.ReadToEnd();

                // Encrypt plain text

                plainBytes = Encoding.Unicode.GetBytes(plainText);

                encryptedBytes = rsaProvider.Encrypt(plainBytes, false);

                // Write encrypted text to file

                encryptedFile = File.Create(encryptedFileName);

                encryptedFile.Write(encryptedBytes, 0, encryptedBytes.Length);

            }

            catch (Exception ex)

            {

                // Any errors? Show them

                //Console.WriteLine("Exception encrypting file! More info:");

                //Console.WriteLine(ex.Message);

            }

            finally

            {

                // Do some clean up if needed

                if (publicKeyFile != null)

                {

                    publicKeyFile.Close();

                }

                if (plainFile != null)

                {

                    plainFile.Close();

                }

                if (encryptedFile != null)

                {

                    encryptedFile.Close();

                }

            }

        } // Encrypt


C#
public   static void Decrypt(string privateKeyFileName, string encryptedFileName, string plainFileName)

        {

            // Variables

            CspParameters cspParams = null;

            RSACryptoServiceProvider rsaProvider = null;

            StreamReader privateKeyFile = null;

            FileStream encryptedFile = null;

            StreamWriter plainFile = null;

            string privateKeyText = "";

            string plainText = "";

            byte[] encryptedBytes = null;

            byte[] plainBytes = null;

            try

            {

                // Select target CSP

                cspParams = new CspParameters();

                cspParams.ProviderType = 1; // PROV_RSA_FULL

                //cspParams.ProviderName; // CSP name

                rsaProvider = new RSACryptoServiceProvider(cspParams);

                // Read private/public key pair from file

                privateKeyFile = File.OpenText(privateKeyFileName);

                privateKeyText = privateKeyFile.ReadToEnd();

                // Import private/public key pair

                rsaProvider.FromXmlString(privateKeyText);

                // Read encrypted text from file

                encryptedFile = File.OpenRead(encryptedFileName);

                encryptedBytes = new byte[encryptedFile.Length];

                encryptedFile.Read(encryptedBytes, 0, (int)encryptedFile.Length);

                // Decrypt text

                plainBytes = rsaProvider.Decrypt(encryptedBytes, false);

                // Write decrypted text to file

                plainFile = File.CreateText(plainFileName);

                plainText = Encoding.Unicode.GetString(plainBytes);

                plainFile.Write(plainText);

            }

            catch (Exception ex)

            {

                // Any errors? Show them

                //Console.WriteLine("Exception decrypting file! More info:");

                //Console.WriteLine(ex.Message);

            }

            finally

            {

                // Do some clean up if needed

                if (privateKeyFile != null)

                {

                    privateKeyFile.Close();

                }

                if (encryptedFile != null)

                {

                    encryptedFile.Close();

                }

                if (plainFile != null)

                {

                    plainFile.Close();

                }

            }

        } // Decrypt

    }

}



I GET ERROR NEAR
C#
plainBytes = rsaProvider.Decrypt(encryptedBytes, false);





Thank you!

What I have tried:

I have tried regenerating the keys and tried encrypt decrypt with various programs and I see same error.
Posted
Updated 30-Jun-16 9:52am
v2
Comments
OriginalGriff 30-Jun-16 14:06pm    
Without seeing the code you are using to encrypt and decrypt, there really isn't anything we can do to help: we have no idea what you are doing, so we have even less idea what you are doing wrong!
Try posting the relevant code fragments, and maybe it'll help.
Use the "Improve question" widget to edit your question and provide better information.
Member 11960509 30-Jun-16 14:24pm    
Thank you, I have updated my question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900