Bad data when decrypting using RSA
Hi, I found an error when I try to decrypt an encrypted array of bytes, but the following error occurs:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Security.Cryptography.CryptographicExceptio n: Bad Data.
Codes:
//Encryption
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//Create a new instance of the RSAParameters structure.
RSAParameters RSAKeyInfo = new RSAParameters();
//Initialize the byte arrays to the public key information.
byte[] PublicKey = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,19 1,178,56,
74,90,36,248,103,18,144,170,163,145,87,54,61,34,22 0,222,
207,137,149,173,14,92,120,206,222,158,28,40,24,30, 16,175,
108,128,35,230,118,40,121,113,125,216,130,11,24,90 ,48,194,
240,105,44,76,34,57,249,228,125,80,38,9,136,29,117 ,207,139,
168,181,85,137,126,10,126,242,120,247,121,8,100,12 ,201,171,
38,226,193,180,190,117,177,87,143,242,213,11,44,18 0,113,93,
106,99,179,68,175,211,164,116,64,148,226,254,172,1 47};
byte[] Exponent = { 1, 0, 1 };
//Create values to store password
string password = txtInput.Text;
byte[] btyPwd = UnicodeEncoding.ASCII.GetBytes(password);
//Set RSAKeyInfo to the public key values.
RSAKeyInfo.Modulus = PublicKey;
RSAKeyInfo.Exponent = Exponent;
//Import key parameters into RSA.
RSA.ImportParameters(RSAKeyInfo);
//Encrypting
byte[] encrypted = RSA.Encrypt(btyPwd, false);
lblOutput.Text = UnicodeEncoding.ASCII.GetString(encrypted);
//Decryption
//Create a new instance of the RSACryptoServiceProvider class.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
byte[] encrypted = UnicodeEncoding.ASCII.GetBytes(lblOutput.Text);
byte[] btyPwd = RSA.Decrypt(encrypted, false);
lblResult.Text = UnicodeEncoding.ASCII.GetString(btyPwd);
WHat's the problem I faced? Solution? thanks
|