譯:在ASP.NET中如何對cookies進行加密和解密

來源:互聯網
上載者:User

標籤:

譯文地址: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);

輸出:

 

譯:在ASP.NET中如何對cookies進行加密和解密

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.