简体   繁体   English

如何在C#asp.net中从数据库解密加密密钥并将其用于从数据库中获取数据。 使用AES加密

[英]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.

相关问题 在ASP.NET C#中使用AES加密时,要解密的数据长度无效 - Length of the data to decrypt is invalid when using AES encryption in ASP.NET C# 用户通过登录控件ASP.NET C#登录时如何解密存储在数据库中的加密密码 - How to decrypt encrypted password that is stored in database when user logins through login control asp.net c# C#,ASP.NET-从数据库收集数据 - C#, ASP.NET - Gathering data from a database 在ASP.NET MVC 5中从数据库中获取字节数据中的图像 - Fetching images in byte data from database in asp.net mvc 5 AjAX Rating Control使用asp.net c#i中的转发器从数据库中存储和获取 - AjAX Rating Control storing and fetching from database using repeater in asp.net c# i 使用自动完成功能通过c#从ASP.NET Web窗体的数据库中获取数据? - Use autocomplete to fetch data from Database in ASP.NET Web Forms with c#? ASP.Net如何从gridview C#更新数据库 - ASP.Net How to Update a database from a gridview C# ASP.NET C#如何从数据库中填充表 - ASP.NET C# How to fill table from database 从数据库ASP中获取数据时从表中跳过行。 净C# - skipping rows from table while fetching data from database asp. net C# 如何在 Python 中解密 C# 的 AES 加密数据? - How to decrypt a AES encrypted data of C# in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM