我怎么可能decrypt
数据库中的encrypted
数据。我正在这样加密我的数据
private void btnRegister_Click(object sender, EventArgs e)
{
try
{
//Best practice in c# to interact with database (using statement)
using (MySqlConnection mysqlCon = new MySqlConnection(connection.connectionString))
{
mysqlCon.Open();
//Add the stored procedure that we uses in the mysqlworkbench
MySqlCommand mysqlCmd = new MySqlCommand("LoginAdd", mysqlCon);
mysqlCmd.CommandType = CommandType.StoredProcedure;
//lets encrypt the data
method.Encrypt(textBox2,hash,results);
//Use trim to avoid spaces
mysqlCmd.Parameters.AddWithValue("_LoginID", LoginID);
mysqlCmd.Parameters.AddWithValue("_LoginUser", textBox1.Text.Trim());
mysqlCmd.Parameters.AddWithValue("_LoginPass", textBox2.Text.Trim());
mysqlCmd.ExecuteNonQuery();
MessageBox.Show("Registered Succesfully");
//proceed to login page
this.Hide();
method.ConstantLoginForm();
}
}
catch(Exception ex)
{
MessageBox.Show(""+ ex);
}
}
现在这是我用于加密数据的方法
public void Encrypt(TextBox txtEncrypt, string hash, byte[] results)
{
hash = "placeholder";
byte[] data = UTF8Encoding.UTF8.GetBytes(txtEncrypt.Text);
using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
{
byte[] keys = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));
using (TripleDESCryptoServiceProvider tripDes = new TripleDESCryptoServiceProvider() { Key = keys, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 })
{
ICryptoTransform transform = tripDes.CreateEncryptor();
results = transform.TransformFinalBlock(data, 0, data.Length);
txtEncrypt.Text = Convert.ToBase64String(results, 0, results.Length);
}
}
}
现在,我的问题是如何decrypt
。我正在做的是一个登录系统。
这是我的解密方法
public void Decrypt(TextBox txtDecrypt, string hash, byte[] results)
{
hash = "placeholder";
byte[] data = Convert.FromBase64String(txtDecrypt.Text); // decrypt the incrypted text
using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
{
byte[] keys = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));
using (TripleDESCryptoServiceProvider tripDes = new TripleDESCryptoServiceProvider() { Key = keys, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 })
{
ICryptoTransform transform = tripDes.CreateDecryptor();
results = transform.TransformFinalBlock(data, 0, data.Length);
//textBox4.Text = UTF8Encoding.UTF8.GetString(results);
}
}
}
这是我到目前为止所做的
private void button1_Click(object sender, EventArgs e)
{
string user = textBox1.Text;
string pass = textBox2.Text;
if (string.IsNullOrEmpty(user) || string.IsNullOrEmpty(pass))
{
MessageBox.Show("Empty Fields Detected!");
return;
}
bool r = validate_login(user, pass);
if (r)
{
this.Hide();
method.ConstantViewUserForm();
}
else
{
MessageBox.Show("Incorrect Credentials");
}
}
private bool validate_login(string user, string pass)
{
using (MySqlConnection mySqlCon = new MySqlConnection(connection.connectionString))
{
mySqlCon.Open();
MySqlCommand mySqlCmd = new MySqlCommand("SELECT *FROM login WHERE loginuser=@username AND loginpass=@password", mySqlCon);
method.Decrypt(textBox2, hash, results);
mySqlCmd.Parameters.AddWithValue("@username", textBox1.Text);
mySqlCmd.Parameters.AddWithValue("@password", textBox2.Text);
MySqlDataReader login = mySqlCmd.ExecuteReader();
if (login.Read())
{
mySqlCon.Close();
return true;
}
else
{
mySqlCon.Close();
return false;
}
}
}
我可以为此找到正确的逻辑。