zoukankan      html  css  js  c++  java
  • 译:在ASP.NET中如何对cookies进行加密和解密

    译文地址:http://www.codeproject.com/Tips/872826/Encrypt-Decrypt-Cookies-in-ASP-NET

    源代码:http://files.cnblogs.com/files/yplong/ShanuBasicCSharpOOPConceptV1.4.zip

    简介:

    在这个话题中,我将说明如何加密和解密cookies的值。cookies是一个在浏览器端存储值的text文件。作为cookies存储在一个简单的text文件中,很容易被读取和修改cookies内容。

    然而你可以对cookies进行加密和解密来达到一定的安全性。本文中我们将使用"MachineKey.Protect” 和 “MachineKey.Unprotect”两个方法来加密和解密。

    MachineKey.Protect() MachineKey.Unprotect() 是应用在ASP.NET4.5中。这两个方法需要2个参数,第一个参数就是要进行加密和解密的内容文件的字节形式,第二个参数就是目的。目的就像一个键(key),可以是字符串类型的值。我们需要通过相同的目的值来对值进行加保护和解保护。

    源码设计:

     1 <div>
     2     <asp:TextBox ID="txtvalue" runat="server" 
     3     placeholder="Enter Some Text" Width="250">
     4     </asp:TextBox><br />
     5     <asp:Label runat="server" ID="lblmsg" ForeColor="Green" 
     6     Font-Bold="true"></asp:Label><br />
     7     <asp:Button ID="btnEncrypt"
     8                 runat="server" Text="Encrypt" 
     9                 OnClick="btnEncrypt_Click" />  
    10     <asp:Button ID="btnDecrypt" runat="server" Text="Decrypt" 
    11     OnClick="btnDecrypt_Click" Style="height: 26px" />
    12 </div>

    代码的实际操作:
    使用命名空间:

     1 //using System.Text;
     2 //using System.Web.Security;
     3 
     4 protected void btnEncrypt_Click(object sender, EventArgs e)
     5     {
     6         var cookieText = Encoding.UTF8.GetBytes(txtvalue.Text);
     7         var encryptedValue = Convert.ToBase64String(MachineKey.Protect(cookieText, "ProtectCookie"));
     8 
     9         //--- Create cookie object and pass name of the cookie and value to be stored.
    10         HttpCookie cookieObject = new HttpCookie("NameOfCookie", encryptedValue);
    11 
    12         //---- Set expiry time of cookie.
    13         cookieObject.Expires.AddDays(5);
    14 
    15         //---- Add cookie to cookie collection.
    16         Response.Cookies.Add(cookieObject);
    17         lblmsg.Text = encryptedValue;
    18     }
    19     protected void btnDecrypt_Click(object sender, EventArgs e)
    20     {
    21         var bytes = Convert.FromBase64String(Request.Cookies["NameOfCookie"].Value);
    22         var output = MachineKey.Unprotect(bytes, "ProtectCookie");
    23         string result = Encoding.UTF8.GetString(output);
    24         lblmsg.Text = result;
    25     }

     ASP.NET 4.0中:

    加密:

    1 var plaintextBytes = Encoding.UTF8.GetBytes("Jitendra Gangwar");
    2 var encryptedValue = MachineKey.Encode(plaintextBytes, MachineKeyProtection.All);
    3 Response.Write(encryptedValue.ToString());

    解密:

    1 var decryptedBytes = MachineKey.Decode(encryptedValue, MachineKeyProtection.All);
    2 var decryptedValue = Encoding.UTF8.GetString(decryptedBytes);
    3 Response.Write(decryptedValue);

    输出:

  • 相关阅读:
    P2679 子串
    线段树优化建边
    P2444 [POI2000]病毒
    P3966 [TJOI2013]单词
    4327: JSOI2012 玄武密码
    UVA1449 Dominating Patterns
    P1250 种树
    P2255 [USACO14JAN]记录奥林比克
    SP283 NAPTIME
    P3436 [POI2006]PRO-Professor Szu
  • 原文地址:https://www.cnblogs.com/yplong/p/4274229.html
Copyright © 2011-2022 走看看