C# 在选择查询中使用函数来解密表数据

标签 c# mysql

我尝试将数据形式 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/

相关文章:

mysql - 列出具有唯一列的结果

c# - 当工作线程非竞争性地写入本地或类变量时,是否需要锁定或 volatile ?

mysql 创建表脚本失败 - 错误 150

mysql - 根据日期从 3x 列中提取行

mysql - 如何在mysql中将带有方括号和逗号的字符串拆分为列和行

c# - 在 Windows Workflow 3.5 中处理异常时如何获取堆栈跟踪?

c# - 控制台应用程序在鼠标单击时卡住

c# - 以 2 位精度存储 double

c# - 当泛型可能不可为空或可能不是对象时,为什么我可以测试它是否为 null?

python - optparse 查找字符串