ASP.NET中MD5和SHA1加密的幾種方法

來源:互聯網
上載者:User

MD5的全稱是Message-Digest Algorithm 5(資訊-摘要演算法),在90年代初由Mit Laboratory for Computer Science和Rsa data security inc的Ronald l. rivest開發出來,經md2、md3和md4發展而來。它的作用是讓大容量資訊在用數位簽章軟體簽署私人密匙前被"壓縮"成一種保密的格式(就是把一個任意長度的位元組串變換成一定長的大整數)。不管是md2、md4還是md5,它們都需要獲得一個隨機長度的資訊併產生一個128位的資訊摘要。

加密雜湊函數將任意長度的二進位字串映射為固定長度的小型二進位字串。加密雜湊函數有這樣一個屬性:在計算上不大可能找到散列為相同的值的兩個不同的輸入;也就是說,兩組資料的雜湊值僅在對應的資料也匹配時才會匹配。資料的少量更改會在雜湊值中產生不可預知的大量更改。所以你很難從加密後的文字中找到蛛絲馬跡。

SHA1的全稱是Secure Hash Algorithm(安全雜湊演算法)

MD5 演算法的雜湊值大小為128位。而SHA1 演算法的雜湊值大小為160位。兩種演算法都是無法復原。

雖說2004年8月17日的美國加州聖巴巴拉的國際密碼學會議(Crypto’2004)上,來自中國山東大學的王小雲教授做了破譯MD5、HAVAL-128、 MD4和RIPEMD演算法的報告,公布了MD系列演算法的破解結果。宣告了固若金湯的世界通行密碼標準MD5的堡壘轟然倒塌,引發了密碼學界的軒然大波。但是我覺得對於我們做普通的軟體來說,這個加密安全程度已經足夠使用了。

我們平常用的最多的無非就是加密使用者密碼,把加密好的密碼儲存到資料庫中,進行密碼比較的時候,把使用者輸入的密碼再進行加密,然後與資料庫中的密文進行比較。至於ASP.net類中是如何?密碼編譯演算法的,這個我們不需要關心,會用就行了。

下面就是ASP.NET中幾種加密方法。密碼編譯演算法有兩種,也就是上面提到的MD5和SHA1,這裡我舉的例子是以MD5為例,SHA1大致相同,只是使用的類不一樣。

MD5 相關類:

System.Security.Cryptography.MD5

System.Security.Cryptography.MD5CryptoServiceProvider()

System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSource, "MD5")

SHA1相關類:

System.Security.Cryptography.SHA1

System.Security.Cryptography.SHA1CryptoServiceProvider()

System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSource, "SHA1")

方法如下:(用的vs2005)

1/**//// <summary>
2 /// 方法一:通過使用 new 運算子建立對象
3 /// </summary>
4 /// <param name="strSource">需要加密的明文</param>
5 /// <returns>返回16位加密結果,該結果取32位加密結果的第9位到25位</returns>
6 public string Get_MD5_Method1(string strSource)
7 {
8 //new
9  System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
10
11  //擷取密文位元組數組
12  byte[] bytResult = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strSource));
13
14  //轉換成字串,並取9到25位
15  string strResult = BitConverter.ToString(bytResult, 4, 8);
16  //轉換成字串,32位
17  //string strResult = BitConverter.ToString(bytResult);
18
19  //BitConverter轉換出來的字串會在每個字元中間產生一個分隔字元,需要去除掉
20  strResult = strResult.Replace("-", "");
21  return strResult;
22 }
23
24 /**//// <summary>
25 /// 方法二:通過調用特定密碼編譯演算法的抽象類別上的 Create 方法,建立實現特定密碼編譯演算法的對象。
26 /// </summary>
27 /// <param name="strSource">需要加密的明文</param>
28 /// <returns>返回32位加密結果</returns>
29 public string Get_MD5_Method2(string strSource)
30 {
31  string strResult = "";
32
33  //Create
34  System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
35
36  //注意編碼UTF8、UTF7、Unicode等的選擇
37  byte[] bytResult = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strSource));
38
39  //位元組類型的數群組轉換為字串
40  for (int i = 0; i < bytResult.Length; i++)
41  {
42   //16進位轉換
43   strResult = strResult + bytResult[i].ToString("X");
44  }
45  return strResult;
46 }
47
48 /**//// <summary>
49 /// 方法三:直接使用HashPasswordForStoringInConfigFile產生
50 /// </summary>
51 /// <param name="strSource">需要加密的明文</param>
52 /// <returns>返回32位加密結果</returns>
53 public string Get_MD5_Method3(string strSource)
54 {
55  return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSource, "MD5");
56 }

這些加密函數都是在伺服器端執行,也就是說,當使用者輸入密碼後,從用戶端到伺服器端傳輸時,使用者的密碼沒有任何保護,很危險。銀行的做法是在用戶端安裝ActiveX控制項,在用戶端就把一些重要訊息進行加密,再發送。這個偶就不會拉,很希望能學習學習做這種ActiveX控制項。

聯繫我們

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