Hi I am a beginner I am creating an Login Form in windows forms application and I have done encryption and decryption iam getting exeception in decrypted method whether I am not calling the methods on my add method correctly or not
as
An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in System.Core.dll
Additional information: The input data is not a complete block.
private void btnlogin_Click(object sender, EventArgs e)
{
Encrypt(tbpwd.Text);
Decrypt(tbpwd.Text);
SqlConnection cn = new SqlConnection("data source=AP134;initial catalog=Agilepoint;integrated security=sspi");
string qry = "insert into Logininfo(email,password) values('" + tbemail.Text + "','" + tbpwd.Text + "')";
SqlCommand cmd = new SqlCommand(qry, cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
private string Encrypt(string password)
{
string EncryptionKey = "GDTDJJW765946HN";
byte[] clearBytes = Encoding.Unicode.GetBytes(password);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
password = Convert.ToBase64String(ms.ToArray());
}
}
return password;
}
private string Decrypt(string password)
{
string EncryptionKey = "GDTDJJW765946HN";
byte[] cipherBytes = Convert.FromBase64String(password);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
password = Encoding.Unicode.GetString(ms.ToArray());
}
}
return password;
}