如何在c# asp.net中解密数据库中的加密密钥,并使用它从数据库中获取数据.加密是用AES完成的
本文关键字:数据库 加密 获取 AES 数据 密钥 asp net 解密 | 更新日期: 2023-09-27 18:05:43
这里的事情是,我使用AES加密加密一个名为KeyValue的值,我需要使用该键值从数据库获取数据。当我在btnHide_Click和btnSearch_Click中使用KeyValue时,它没有被解密,我无法使用它从数据库中获取图像和文本字段。请帮助我如何编写解密代码和在哪里写它。我是初学者。所以一个小小的解释会对我理解它是如何完成的很有帮助。提前谢谢。
我已经使用了这个链接中给出的加密方法
http://www.aspsnippets.com/Articles/Encrypt-and-Decrypt-Username-or-Password-stored-in-database-in-ASPNet-using-C-and-VBNet.aspxpublic partial class _Default : System.Web.UI.Page
{
InsertValue ivo = new InsertValue();
ImageEncryptEntities ctx = new ImageEncryptEntities();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Tab1.CssClass = "Clicked";
MainView.ActiveViewIndex = 0;
}
}
protected void Upload(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
FileUpload1.SaveAs(MapPath("~/Images/" + FileUpload1.FileName));
Image1.ImageUrl = "~/Images/" + FileUpload1.FileName;
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
ivo.Insert(Image1.ImageUrl, TextBox1.Text, txtKeyValue.Text);
Response.Write("Records Saved");
}
protected void btnHide_Click(object sender, EventArgs e)
{
var keystrike = (from p in ctx.tblPhotos
where p.KeyValue.Equals(txtValue.Text)
select p).SingleOrDefault();
try
{
Image1.ImageUrl = keystrike.Picture;
lblMessage.Text = keystrike.Text;
}
catch (NullReferenceException)
{
Image1.ImageUrl = "";
lblMessage.Text = "No key match";
}
}
protected void Tab1_Click(object sender, EventArgs e)
{
Tab1.CssClass = "Clicked";
Tab2.CssClass = "Initial";
MainView.ActiveViewIndex = 0;
}
protected void Tab2_Click(object sender, EventArgs e)
{
Tab1.CssClass = "Initial";
Tab2.CssClass = "Clicked";
MainView.ActiveViewIndex = 1;
}
protected void btnSearch_Click(object sender, EventArgs e)
{
var keystrike = (from p in ctx.tblPhotos
where p.KeyValue.Equals(txtKey.Text)
select p).SingleOrDefault();
try
{
Image2.ImageUrl = keystrike.Picture;
}
catch (NullReferenceException)
{
Image2.ImageUrl = "";
lblMessage.Text = "No key match";
}
}
这是两个实体框架类GetValue和InsertValue
public class GetValue
{
SqlConnection cnn = new SqlConnection("Data Source=.;Initial Catalog=ImageEncrypt;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework");
public SqlDataReader Get(string mstrKeyValue)
{
cnn.Open();
SqlCommand cmd = new SqlCommand("spGetValues", cnn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@KeyValue",mstrKeyValue);
SqlDataReader dr = cmd.ExecuteReader();
return dr;
}
public class InsertValue
{
SqlConnection cnn = new SqlConnection("DataSource=.;InitialCatalog=ImageEncrypt;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework");
public void Insert(string mstrPicture, string mstrText, string mstrKeyValue)
{
cnn.Open();
SqlCommand cmd = new SqlCommand("spInsertValues", cnn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Picture", mstrPicture);
cmd.Parameters.AddWithValue("@Text", mstrText);
cmd.Parameters.AddWithValue("@KeyValue", Encrypt(mstrKeyValue.Trim()));
cmd.ExecuteNonQuery();
}
private string Encrypt(string clearText)
{
string EncryptionKey = "MAKV2SPBNI99212";
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
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.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
return clearText;
}
请出示答案
插入值
cmd.Parameters.AddWithValue("@KeyValue", Encrypt(mstrKeyValue.Trim()));
然后是value
Datatable dt=new Datatable();
string dyc_val=decrypt(dt.rows[0]["encryptvalue"].tostring());