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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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;
}

}
}

.



Relevant Pages

  • Re: Symmetric encryption algorithm with group like properties
    ... >> Solutions that exist today are not as secure as they can be. ... I wouldn't expect more than PGP / GPG type encryption, ... > versions - with the key, protected by RSA encryption under a RSA public key ... > Alice needs a secure decryption mechanism to read her emails, ...
    (sci.crypt)
  • RE: rsa encrtyption
    ... It seems to work for her now and all we did was apply some Sql Server updates. ... My only guess was that the MS dll she used to apply the RSA encryption was ... According to the RSA encryption reference, I've performed some local tests, ... Export the machine-level RSA key container: ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • RE: rsa encrtyption
    ... According to the RSA encryption reference, I've performed some local tests, ... Grant Read Access to the RSA Encryption Key: ... Export the machine-level RSA key container: ... Import the the machine-level RSA key container on the 2nd server: ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Rabin vs. RSA/ElGamal
    ... the speed difference between RSA ... encryption and Rabin encryption probably is irrelevant. ... For RSA or ElGamal you need exponentiation. ...
    (sci.crypt)
  • Re: Rabin vs. RSA/ElGamal
    ... the speed difference between RSA ... encryption and Rabin encryption probably is irrelevant. ... What DOES takes the time is decryption. ... This also doesn't change the fact that Rabin encryption is still a lot ...
    (sci.crypt)