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.
public static void Encrypt(string publicKeyFileName, string plainFileName, string encryptedFileName)
{
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
{
cspParams = new CspParameters();
cspParams.ProviderType = 1;
rsaProvider = new RSACryptoServiceProvider(cspParams);
publicKeyFile = File.OpenText(publicKeyFileName);
publicKeyText = publicKeyFile.ReadToEnd();
rsaProvider.FromXmlString(publicKeyText);
plainFile = File.OpenText(plainFileName);
plainText = plainFile.ReadToEnd();
plainBytes = Encoding.Unicode.GetBytes(plainText);
encryptedBytes = rsaProvider.Encrypt(plainBytes, false);
encryptedFile = File.Create(encryptedFileName);
encryptedFile.Write(encryptedBytes, 0, encryptedBytes.Length);
}
catch (Exception ex)
{
}
finally
{
if (publicKeyFile != null)
{
publicKeyFile.Close();
}
if (plainFile != null)
{
plainFile.Close();
}
if (encryptedFile != null)
{
encryptedFile.Close();
}
}
}
public static void Decrypt(string privateKeyFileName, string encryptedFileName, string plainFileName)
{
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
{
cspParams = new CspParameters();
cspParams.ProviderType = 1;
rsaProvider = new RSACryptoServiceProvider(cspParams);
privateKeyFile = File.OpenText(privateKeyFileName);
privateKeyText = privateKeyFile.ReadToEnd();
rsaProvider.FromXmlString(privateKeyText);
encryptedFile = File.OpenRead(encryptedFileName);
encryptedBytes = new byte[encryptedFile.Length];
encryptedFile.Read(encryptedBytes, 0, (int)encryptedFile.Length);
plainBytes = rsaProvider.Decrypt(encryptedBytes, false);
plainFile = File.CreateText(plainFileName);
plainText = Encoding.Unicode.GetString(plainBytes);
plainFile.Write(plainText);
}
catch (Exception ex)
{
}
finally
{
if (privateKeyFile != null)
{
privateKeyFile.Close();
}
if (encryptedFile != null)
{
encryptedFile.Close();
}
if (plainFile != null)
{
plainFile.Close();
}
}
}
}
}
I GET ERROR NEAR
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.