473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
0 5653

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: qe_Cui | last post by:
Hello everyone: I am using vb.net 2003, sqlserver2000 database, and windows 2000 professional OS. In the program, I encrypt the login user's userid and passed with RSACryptoServiceProvider....
7
by: Dica | last post by:
i've used the sample code from msdn to create an encyption/decryption assembly as found here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp i'm...
6
by: karim | last post by:
I have an asp.net page that stopped running properly giving the error below. The app uses a SQL Server 2000 on another server. Enterprise Manager and Query analyzer on the web server can connect to...
3
by: Brian Foree | last post by:
I am developing an ASP.NET application that uses Access 2000 as its backend, and have just started getting the following error on 2 ASP.NET pages that had been working until late last week (and I...
0
by: qe_Cui | last post by:
Hello everyone: I am using vb.net 2003, sqlserver2000 database, and windows 2000 professional OS. In the program, I encrypt the login user's userid(is "sa") and passed(is "sa") with...
2
by: GRB | last post by:
A client wants me to decrypt a cookie. They encrypted the data in java using TripleDES. The key provided is a 168 bit 44 character key, DESede alogorithm. Ive tried to decrypt in vb.net and get the...
4
by: floppyzedolfin | last post by:
Hello! I'm actually encoding an encryption / decryption program. The encryption programes takes a file path in parameter, and encrypts the contents of the file and stores that into another file. ...
3
by: Mahesh | last post by:
#include <stdio.h> #include <string.h> #include <malloc.h> int main(void) { int key = 120; char *s = "String to Encrypt using XOR"; char *ss = s; char echar; char dchar;
1
by: SM | last post by:
Hi, I need your help, the following code is not working, ret = sr.ReadToEnd() throws the message error : Length of the data to decrypt is invalid. Any idea ? thank you public string...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.