我尝试将数据形式 dtls
表显示到 datagridview - 已解密 - 使用函数 Decrypt
代码在没有该函数的情况下工作正常。不知道如何在选择查询中实现该功能
try
{
con = new MySqlConnection();
con.ConnectionString = constring;
con.Open();
MySqlDataAdapter myDA = new MySqlDataAdapter();
string cmd = "SELECT Decrypt(username) FROM sys.dtls ";
myDA.SelectCommand = new MySqlCommand(cmd, con);
DataTable table = new DataTable();
myDA.Fill(table);
BindingSource bSource = new BindingSource();
bSource.DataSource = table;
dgvShow.DataSource = bSource;
{
}
con.Close();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
public 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;
}
最佳答案
SQL 不知道您的 Decrypt
函数。您所要做的就是运行查询来选择用户名:SELECT username FROM sys.dtls
,然后迭代结果并解密用户名:
myDA.Fill(table);
foreach(DataRow row in table.Rows)
{
row["username"] = Decrypt(row["username"].ToString());
}
关于C# 在选择查询中使用函数来解密表数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59545214/