C# C使用select查询中的函数对表数据进行解密
我尝试使用函数Decrypt将数据表单dtls表显示给datagridview-Decrypted 代码在没有函数的情况下运行良好。不确定如何在select查询中实现该函数C# C使用select查询中的函数对表数据进行解密,c#,mysql,C#,Mysql,我尝试使用函数Decrypt将数据表单dtls表显示给datagridview-Decrypted 代码在没有函数的情况下运行良好。不确定如何在select查询中实现该函数 try { con = new MySqlConnection(); con.ConnectionString = constring; con.Open(); MySqlDataAdapter myDA = new MySqlDataAdapter(); string cmd =
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不知道您的解密函数。您需要做的是运行查询来选择用户名:从sys.dtls中选择username,然后迭代结果并解密用户名,然后:
myDA.Fill(table);
foreach(DataRow row in table.Rows)
{
row["username"] = Decrypt(row["username"].ToString());
}
现在谢谢,没有错误,但是Datagridview保持为空-有什么想法吗?我不确定。你试过调试它吗?循环结束后,在该行上放置一个断点,以查看数据是否如您所期望的那样存在,然后继续查看发生了什么。