gpt4 book ai didi

c# - 修改密码功能中的加密和解密?

转载 作者:行者123 更新时间:2023-11-29 21:16:10 25 4
gpt4 key购买 nike

我想执行更改密码功能,下面显示了我到目前为止所做的代码和屏幕截图:

        private void Password_Change()
{
int rowsAffected = 0;
string query = "UPDATE staff_user SET staff_password = @newpassword WHERE staff_password = @staff_password";
string constr = ConfigurationManager.ConnectionStrings["dbyouthworkConnectionString"].ConnectionString;

ConfirmPassword.Text = Encrypt(ConfirmPassword.Text.Trim());
CurrentPassword.Text = Decrypt(CurrentPassword.Text.Trim());
using (MySqlConnection con = new MySqlConnection(constr))
{
using (MySqlCommand cmd = new MySqlCommand(query))
{
con.Open();


using (MySqlDataAdapter sda = new MySqlDataAdapter())


{
cmd.Parameters.AddWithValue("@staff_password",CurrentPassword.Text );
cmd.Parameters.AddWithValue("@newpassword", (ConfirmPassword.Text));
cmd.Connection = con;


rowsAffected = cmd.ExecuteNonQuery();

con.Close();

}


if (rowsAffected > 0)
{
Label1.ForeColor = System.Drawing.Color.Green;
Label1.Text = "Password has been changed successfully.";
}
else
{
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Text = "Password does not match with our database records.";
}
if (CurrentPassword.Text == New_Password.Text)
{
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Text = "Old Password and New Password cannot be the same !";
}

if (CurrentPassword.Text == ConfirmPassword.Text)
{
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Text = "Old Password and New Password cannot be the same !";
}

}
}
}
private string Encrypt(string clearText)
{
string EncryptionKey = "MAKV2SPBNI99212";
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
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();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
return clearText;
}
private string Decrypt(string cipherText)
{
string EncryptionKey = "MAKV2SPBNI99212";
byte[] cipherBytes = Convert.FromBase64String(cipherText);
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();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}[![enter image description here][1]][1]

但是当我运行该项目时这是我得到的错误: enter image description here

我似乎看不到自己去了哪里,因为我在用户登录时使用了相同的解密函数,在用户创建帐户时使用了相同的加密函数。

最佳答案

错误消息说明了一切:“输入数据不是完整的 block 。”

AES 是一种 block 密码,它逐 block 处理数据, block 的大小为 16 字节。如果数据不是 block 大小的倍数,则必须以某种方式填充,AES 通常使用的填充是 PKCS#7 née PKCS#5。

将该填充选项添加到加密代码中。填充将在加密时添加并在解密时删除。您需要确保加密输出缓冲区比输入数据长一个 block (16 字节)。

关于c# - 修改密码功能中的加密和解密?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35931636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com