Error:"Key not valid for use in specified state" for "RSACryptoServiceProvider.Encrypt()"

  • Thread starter =?iso-8859-9?Q?Ismail_Fatih_Y=FDld=FDr=FDm?=
  • Start date
?

=?iso-8859-9?Q?Ismail_Fatih_Y=FDld=FDr=FDm?=

I modified the RSACSPSample from MSDN to try out a simple commutative
encryption model using RSA encryption but when i run the progrem the first
encryption command works but during the second encryption command (line :
encryptedData2 = RSAE...) i get a "Key not valid for use in specified
state." exception error even though i provide a valid second key to encrypt
it. How can i overcome this error and get double encryption to work ?

The Code I use :
############################################################################################

using System;
using System.Security.Cryptography;
using System.Text;

class RSATest
{

static void Main()
{
try
{
//Create a UnicodeEncoder to convert between byte array and
string.
UnicodeEncoding ByteConverter = new UnicodeEncoding();

//Create byte arrays to hold original, encrypted, and decrypted
data.
byte[] dataToEncrypt = ByteConverter.GetBytes("Data to
Encrypt");
byte[] encryptedData;
byte[] encryptedData2;
byte[] decryptedData;

//Create a new instances of RSACryptoServiceProvider
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSACryptoServiceProvider RSA2 = new RSACryptoServiceProvider();


//Pass the data to ENCRYPT, the public key information
encryptedData = RSAEncrypt(dataToEncrypt,
RSA.ExportParameters(false), false);


encryptedData2 = RSAEncrypt(encryptedData,
RSA2.ExportParameters(false), false);

//Pass the data to DECRYPT using private key information

decryptedData = RSADecrypt(encryptedData2,
RSA.ExportParameters(true), false);

decryptedData = RSADecrypt(decryptedData,
RSA2.ExportParameters(true), false);

//Display the decrypted plaintext to the console.
Console.WriteLine("Decrypted plaintext: {0}",
ByteConverter.GetString(decryptedData));
}
catch (Exception ex)
{
//Catch this exception in case the encryption did
//not succeed.
Console.WriteLine(ex.Message);

}
}

static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters
RSAKeyInfo, bool DoOAEPPadding)
{
try
{
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

//Import the RSA Key information.
RSA.ImportParameters(RSAKeyInfo);

//Encrypt the passed byte array and specify OAEP padding.
return RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
}
//Catch and display a CryptographicException
//to the console.
catch (CryptographicException e)
{
Console.WriteLine(e.Message);

return null;
}

}

static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters
RSAKeyInfo, bool DoOAEPPadding)
{
try
{
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

//Import the RSA Key information. This needs
RSA.ImportParameters(RSAKeyInfo);

//Decrypt the passed byte array and specify OAEP padding.

return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
}
//Catch and display a CryptographicException

catch (CryptographicException e)
{
Console.WriteLine(e.ToString());

return null;
}

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top