[英]How to decrypt an encrypted key from database and use it for fetching data from database in C# asp.net . Encryption is done with AES
Here the thing is that I used AES encryption to encrypt a value named KeyValue and I need to use that key value to get data from the database . 事情是,我使用AES加密来加密名为KeyValue的值,并且需要使用该键值从数据库中获取数据。 when i am using the KeyValue in btnHide_Click and btnSearch_Click it is not getting decrypted and i am not able to use it for getting the image and text field from database. 当我在btnHide_Click和btnSearch_Click中使用KeyValue时,它没有被解密,并且我无法使用它从数据库中获取图像和文本字段。 please help me how to write the code for decryption and where to write it. 请帮助我如何编写解密代码以及在哪里编写。 I am a beginner . 我是初学者。 So a little explanation will be a great thing for me to understand how it was done. 因此,对我而言,了解一下它的完成方式将是一件很棒的事情。 thanks in advance . 提前致谢 。
I have used the encryption method given in this link 我已经使用了此链接中给出的加密方法
http://www.aspsnippets.com/Articles/Encrypt-and-Decrypt-Username-or-Password-stored-in-database-in-ASPNet-using-C-and-VBNet.aspx http://www.aspsnippets.com/Articles/Encrypt-and-Decrypt-Username-or-Password-stored-in-database-in-ASPNet-using-C-and-VBNet.aspx
public 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";
}
}
These are the two entity framework classes GetValue and InsertValue 这是两个实体框架类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;
}
Please show this answer 请显示此答案
when you insert value. 当您插入值时。
cmd.Parameters.AddWithValue("@KeyValue", Encrypt(mstrKeyValue.Trim()));
and then you get value 然后你得到价值
Datatable dt=new Datatable();
string dyc_val=decrypt(dt.rows[0]["encryptvalue"].tostring());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.