ASP.NET中的DEC加密解密過程

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   使用   ar   for   

本文章分享自 青青果樹園的部落格,地址是:http://www.cnblogs.com/qqingmu/archive/2008/01/10/1034168.html

我們做網頁時經常會遇到URL傳輸(表單提交)參數加密。
例如:要進行一個使用者帳號編輯,要傳遞使用者的ID,URL如下:http://localhost/mysystem/editAccounts.aspx?ID=2
但又不想讓別人知道這個使用者的ID為2,惡意的使用者可能還會將2修改,改為別的使用者ID。加密傳遞的參數值可以解決問題。
1、以下是DEC加密、解密的函數。

加密過程:

 1  /**//// <summary> 2         /// DEC 加密過程 3         /// </summary> 4         /// <param name="pToEncrypt">被加密的字串</param> 5         /// <param name="sKey">密鑰(只支援8個位元組的密鑰)</param> 6         /// <returns>加密後的字串</returns> 7         public string Encrypt(string pToEncrypt, string sKey) 8         { 9             //訪問資料加密標準(DES)演算法的密碼編譯服務提供者 (CSP) 版本的封裝對象10             DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 11             des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //建立加密對象的密鑰和位移量12             des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);  //原文使用ASCIIEncoding.ASCII方法的GetBytes方法13 14             byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);//把字串放到byte數組中15 16             MemoryStream ms = new MemoryStream();//建立其支援儲存區為記憶體的流 17             //定義將資料流連結到加密轉換的流18             CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);19             cs.Write(inputByteArray, 0, inputByteArray.Length);20             cs.FlushFinalBlock();21             //上面已經完成了把加密後的結果放到記憶體中去22 23             StringBuilder ret = new StringBuilder();24             foreach (byte b in ms.ToArray())25             {26                 ret.AppendFormat("{0:X2}", b);27             }28             ret.ToString();29             return ret.ToString();30         }

解密過程:

 1  /**//// <summary> 2         /// DEC 解密過程 3        /// </summary> 4         /// <param name="pToDecrypt">被解密的字串</param> 5         /// <param name="sKey">密鑰(只支援8個位元組的密鑰,同前面的加密金鑰相同)</param> 6        /// <returns>返回被解密的字串</returns> 7         public string Decrypt(string pToDecrypt, string sKey) 8         { 9             DESCryptoServiceProvider des = new DESCryptoServiceProvider();10 11             byte[] inputByteArray = new byte[pToDecrypt.Length / 2];12             for (int x = 0; x < pToDecrypt.Length / 2; x++)13             {14                 int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));15                 inputByteArray[x] = (byte)i;16             }17 18             des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //建立加密對象的密鑰和位移量,此值重要,不能修改19             des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);20             MemoryStream ms = new MemoryStream();21             CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);22 23             cs.Write(inputByteArray, 0, inputByteArray.Length);24             cs.FlushFinalBlock();25 26             //建立StringBuild對象,createDecrypt使用的是流對象,必須把解密後的文本變成流對象27             StringBuilder ret = new StringBuilder(); 28 29             return System.Text.Encoding.Default.GetString(ms.ToArray());30         }

2、 具體在程式中使用加密解密演算法的例子如下:

1 在發送頁面2 Response.Redirect("~/GridView.aspx?ID=" + Encrypt("zlh","12345678"));3 4 在接受頁面5 string acceptStr;6 acceptStr = Decrypt(Request.QueryString["ID"],"12345678");

 

ASP.NET中的DEC加密解密過程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.