April 30, 2005 at 1:51 am
Using the code given below I am able to encrypt the text using RSA encryption but am not able to decrypt it.
I get error Bad Data.
How to resolve this?
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmEncryptPass());
}
private void btnEncrypt_Click(object sender, System.EventArgs e)
{
try
{
//Encrypting password. Applying DES Encryption Algo
Byte[] CypherTextBArray = EncryptingPassword();
string sCrypt = "";
for (int i = 0; i <= CypherTextBArray.Length - 1;i+=1)
{
sCrypt += Convert.ToChar(CypherTextBArray);
}
this.txtEncryptedPassword.Text = sCrypt;
updatePasswordInDB("Server=Server1; database=CSharpDB; uid=sa; pwd=sa");
}
catch(Exception ex)
{
throw ex;
}
}
public byte[] EncryptingPassword()
{
//Return Encrypted Password
/*return
CspParameters cspParams = new CspParameters();
cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspParams);
RSAParameters xmlKeys = rsa.ExportParameters(true);
RSAParameters xmlPublicKey = rsa.ExportParameters(false);
//RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
//Hold the public key to be used in the encryption procedure
//'get the public key so you can encrypt the message:
//rsa.FromXmlString(xmlPublicKey);
rsa.ImportParameters(xmlPublicKey);
byte [] IV = {10, 20, 30, 40, 50, 60, 70, 80};
byte[] InputByteArray = System.Text.UnicodeEncoding.Default.GetBytes((this.txtPassword.Text.Trim()));
CypherTextBArray = rsa.Encrypt(InputByteArray,false);
rsa.PersistKeyInCsp = true;
return CypherTextBArray;
}
public string DecryptingPassword(Byte[] CypherTextBArray)
{
CspParameters cspParams = new CspParameters();
cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspParams);
rsa.FromXmlString(GetXmlKeys());
//create a byte array and then put the decrypted plaintext into it
Byte[] RestoredPlainText = (rsa.Decrypt(CypherTextBArray,false));
//'Step through the two-byte unicode plaintext, displaying the restored 'plaintext message
string sCrypt = "";
for (int i = 0; i <= CypherTextBArray.Length - 1;i+=1)
{
sCrypt += Convert.ToChar(RestoredPlainText);
}
return sCrypt;
}
public void updatePasswordInDB(string connectionString)
{
SqlConnection objConn= new SqlConnection(connectionString);
objConn.Open();
SqlCommand objcmd = new SqlCommand("update Users set password = '" +
this.txtEncryptedPassword.Text.Trim() + "' where NTUser_name = 'Samuel'", objConn);
objcmd.ExecuteNonQuery();
}
public string GetPasswordFromDB(string connectionString)
{
SqlConnection objConn= new SqlConnection(connectionString);
objConn.Open();
SqlCommand objcmd = new SqlCommand("select password from Users where NTUser_name = 'Samuel'", objConn);
SqlDataAdapter sqlDa = new SqlDataAdapter(objcmd);
DataSet ds = new DataSet();
sqlDa.Fill(ds);
return(ds.Tables[0].Rows[0]["password"].ToString());
}
private void btnDecrypt_Click(object sender, System.EventArgs e)
{
try
{
//Encrypting password. Applying DES Encryption Algo
String sPwd = GetPasswordFromDB("Server=Server1; database=CSharpDB; uid=sa; pwd=sa");
byte[] InputByteArray = System.Text.UnicodeEncoding.Default.GetBytes(sPwd.TrimEnd());
this.txtPassword.Text = DecryptingPassword(InputByteArray);
}
catch(Exception ex)
{
throw ex;
}
}
public string GetXmlKeys()
{
CspParameters cspParams = new CspParameters();
cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspParams);
//Hold both keys in a global variable that will be used in the
//RSAParameters xmlKeys = rsa.ExportParameters(true);
xmlKeys = rsa.ToXmlString(true);
return xmlKeys.ToString();
}
}
}
April 30, 2005 at 6:40 pm
Sorry nothing setup to test here. However hav you run in debug and verified which line is failing and stepped thru the process in full?
May 1, 2005 at 6:22 am
The line that is failing is :
Byte[] RestoredPlainText = (rsa.Decrypt(CypherTextBArray,false));
I am unable to decrypt the string that I have sucessfully encrypted.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply