【问题标题】:encrypt and decrypt a database with c#用c#加密和解密数据库
【发布时间】:2017-05-16 16:08:52
【问题描述】:

我想将文本框中的数据存储到数据库中,但应该加密 然后通过一些搜索键显示这些数据 我使用了一种加密方法并且工作正常 主要问题是显示解密的数据

 string encClass = AESencDec.Decrypt(txt_class.Text);
 SqlConnection connection = new SqlConnection(ConnectionString);
        SqlCommand command = new SqlCommand("select * from nuclear where Class='" + encClass + "'", connection);
        connection.Open();
        command.CommandType = CommandType.Text;
        command.ExecuteNonQuery();
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter();                             
        da.Fill(dt);
        dataGridView1.DataSource = dt;
        connection.Close();

它实际上是从数据库中读取数据并显示,但我想知道如何显示它已解密???

这是我在这个项目中使用的解密类

 public static string Decrypt(string text)

    {
        string hash = "f0xle@rn";
        byte[] plaintextbytes = Convert.FromBase64String(text);
        using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
        {
            byte[] keys = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));
            using (TripleDESCryptoServiceProvider triples = new TripleDESCryptoServiceProvider() { Key = keys, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 })
            {
                ICryptoTransform transform = triples.CreateDecryptor();
                byte[] results = transform.TransformFinalBlock(plaintextbytes, 0, plaintextbytes.Length);
                return UTF8Encoding.UTF8.GetString(results);
            }
        }

    }

这是加密函数

 public static string Encrypt(string text)
    {
         string hash = "f0xle@rn";
         byte[] plaintextbytes = UTF8Encoding.UTF8.GetBytes(text);
        using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
        {
            byte[] keys = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));
                using (TripleDESCryptoServiceProvider triples = new TripleDESCryptoServiceProvider() {Key = keys, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 })
            {
                ICryptoTransform transform = triples.CreateEncryptor();
                byte[] results = transform.TransformFinalBlock(plaintextbytes, 0, plaintextbytes.Length);
                return Convert.ToBase64String(results);
            }
        }
    }

【问题讨论】:

  • 您应该能够遍历DataTable 中的每个DataRow 并在DataRow 中的列上调用Decrypt 方法?
  • AESencDec.Encrypt 看起来像什么?您确定它与您的Decrypt 匹配吗?
  • 看起来您正在使用 AES 进行加密并尝试使用 3DES/TripleDES 进行解密......这是行不通的
  • @MarkC。你能给我一个例子吗?如何?请提供更多信息。谢谢
  • @DigiFriend 抱歉,复制粘贴错误

标签: c# sql database encryption windows-applications


【解决方案1】:

这是一个使用测试数据更新数据表的示例。您可以更新它以使用数据库中的任何列名:

    int rowCount = 5;
    DataTable table = new DataTable();
    table.Columns.Add(new DataColumn("id", typeof(String)));
    table.Columns.Add(new DataColumn("encrypted", typeof(String)));
    table.Columns.Add(new DataColumn("decrypted", typeof(String)));

    //Write test encrypted data to data table
    for(int i = 0; i < rowCount; i++) 
    {
        string clearText = "test" + i.ToString();
        string cipherText = Encrypt(clearText);
        table.Rows.Add(new object[] {i.ToString(), cipherText, ""});
    }

    //Decrypt each item, and assign result to decrypted column
    foreach (DataRow row in table.Rows) 
    {
        row["decrypted"] = Decrypt(row["encrypted"].ToString());
    }

因此,您的数据适配器会填充数据表。您可以在将其应用于数据源之前迭代行并解密:

da.Fill(dt);

foreach(DataRow row in dt.Rows)
{
    row["name of encrypted column"] = Decrypt(row["name of encrypted column"].ToString());
}

dataGridView1.DataSource = dt;

【讨论】:

  • 我通过 sqlcommand 更新数据库。这不是真正有用的代码,我只是想显示我在数据库中的所有内容(已解密),你知道吗?
  • 我使用了你的方法,但是我遇到了长度无效的base-64数组或字符串的问题,你知道是什么问题吗?
  • 一个 base 64 字符串的长度应该是 4 的倍数。末尾有等号填充以保持预期的长度。有可能您的部分过程正在剥离填充? stackoverflow.com/questions/2925729/…
  • 我找不到有用的代码,你能帮帮我吗,谢谢。
猜你喜欢
  • 2014-11-06
  • 2013-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多