By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,173 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,173 IT Pros & Developers. It's quick & easy.

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

P: n/a
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;
}

}
}

Dec 2 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.