徹底解決ASP.NET MD5加密中文結果和ASP不一致的問題

來源:互聯網
上載者:User

當要進行MD5加密的字串不含中文時,那麼ASP.NET的加密結果和ASP是一致的:

1 Response.Write(FormsAuthentication.HashPasswordForStoringInConfigFile("www.mzwu.com", "MD5")); 
2  //結果:D66E1F138689B9B5AA4C520D9EAFFB61

 

Response.Write(MD5("www.mzwu.com",32))
'結果:d66e1f138689b9b5aa4c520d9eaffb61

當要進行MD5加密的字串含中文時,兩者的加密結果就不一致了:

Response.Write(FormsAuthentication.HashPasswordForStoringInConfigFile("木子屋", "MD5")); 
//結果:34D9CBD5164C47058DFA3AF832E2D1DC

 

Response.Write(MD5("木子屋",32))
'結果:0a40a90190da023ae7aa17771663a41e

 

我們知道,ASP.NET預設使用utf-8編碼格式,而ASP使用的是gb2312編碼格式,正是由於這編碼格式不同,才導致了兩者對中文加密結果的不同。下邊我們看看怎麼讓ASP.NET的編碼結果和ASP一樣,那也就意味著要讓ASP.NET採用gb2312編碼格式,這點FormsAuthentication.HashPasswordForStoringInConfigFile()方法是辦不到的,我們得使用System.Security.Cryptography.MD5CryptoServiceProvider對象的ComputeHash方法來進行加密:s

代碼

1 MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
2 Response.Write(BitConverter.ToString(MD5.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes("木子屋"))).Replace("-", ""));
3  //結果:0A40A90190DA023AE7AA17771663A41E

若要再使用utf-8加密也非常容易:

代碼

1 MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
2 Response.Write(BitConverter.ToString(MD5.ComputeHash(Encoding.GetEncoding("utf-8").GetBytes("木子屋"))).Replace("-", ""));
3  //結果:34D9CBD5164C47058DFA3AF832E2D1DC

問題似乎是比較完美的解決了,我們再來完善一下:當要加密的字串是從其他頁面傳進來時,其他頁面採用的編碼格式可能是gb2312,可能是utf-8,還可能是其他的編碼格式,怎麼解決呢?你可能會覺得很簡單啊,使用它先前的編碼格式進行加密不就行了?實際測試中你會發現兩個很嚴重的問題:

  1. 我們無從知道參數傳過來時是使用什麼編碼格式;

  2. 如果兩個頁面使用的編碼方式不一樣,那麼Request接收到的參數值會亂碼,那就不要談加密了;

  問題1比較好解決,要求對方傳參數的同時必須多加一個參數說明採用的編碼格式,問題2的解決方案是不使用Request直接接收參數值,廢話不多說了,看下邊的函數:

代碼

 1 /**//// <summary>
2 /// 對字串進行MD5加密
3 /// </summary>
4 /// <param name="text">要加密的字串</param>
5 /// <param name="charset">字串編碼格式</param>
6 /// <example>str = MD5("木子屋","gb2312");</example>
7 /// <returns></returns>
8 public string MD5(string text, string charset)
9 {
10     return (MD5(text, charset, false));
11 }
12
13 /**//// <summary>
14 /// 對字串或參數值進行MD5加密
15 /// </summary>
16 /// <param name="text">要加密的字串或參數名稱</param>
17 /// <param name="charset">字串編碼格式</param>
18 /// <param name="isArg">加密字串類型 true:參數值 false:字串</param>
19 /// <returns></returns>
20 public string MD5(string text, string charset, bool isArg)
21 {
22     try
23     {
24         MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
25
26         if (isArg)
27         {
28             NameValueCollection Collect = HttpUtility.ParseQueryString(Request.Url.Query, Encoding.GetEncoding(charset));//使用Collect接收參數值
29             if (Collect[text] != null)
30             {
31                 return BitConverter.ToString(MD5.ComputeHash(Encoding.GetEncoding(charset).GetBytes(Collect[text].ToString()))).Replace("-", "");
32             }
33         }
34         else
35         {
36             return BitConverter.ToString(MD5.ComputeHash(Encoding.GetEncoding(charset).GetBytes(text))).Replace("-", "");
37         }
38     }
39     catch { }
40
41     return string.Empty;
42 }

說明1:上邊代碼需要引入的命名空間

using System.Text;
using System.Web.Security;
using System.Security.Cryptography;
using System.Collections.Specialized;

說明2:32位密文如何轉化成16位?

  16位密文是32位密文的9到24位字元。如:"0a40a90190da023ae7aa17771663a41e"→"90da023ae7aa1777"

聯繫我們

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