簡體   English   中英

使用C#加密和解密數據庫

[英]encrypt and decrypt a database with c#

我想將文本框中的數據存儲到數據庫中,但是應該對其進行加密,然后通過一些搜索鍵顯示這些數據,我使用了一種加密方法,其工作原理很好,主要問題是顯示已解密的數據

 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();

它實際上從數據庫讀取數據並顯示,但是我想知道如何顯示解密的數據?

這是我用於該項目的Decrypt類

 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);
            }
        }
    }

這是一個顯示使用測試數據更新數據表的示例。 您可以更新它以使用數據庫中的任何列名稱:

    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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM