【发布时间】: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