.NET 中的MD5及hash加密以及密文輸出(C#)

來源:互聯網
上載者:User

背景: ASP.NET的使用者驗證方法,及加密處理

在網上搜羅了一下,搜到很多雷同文章,摘引如下:

public static string MD5(string Sourcein)  {      MD5CryptoServiceProvider MD5CSP = new MD5CryptoServiceProvider();   byte[] MD5Source = System.Text.Encoding.UTF8.GetBytes(Sourcein);   byte[] MD5Out = MD5CSP.ComputeHash(MD5Source);   return Convert.ToBase64String(MD5Out);  }

而如此做的話,Convert.ToBase64String出來的會是一串包含字母數字以及符合的字串,例如:afw1fw3j=-

這樣是與儲存在資料庫中的加密後字串不相同的,自然無法比較。資料庫中儲存的是類似BABA327D241746EE0829E7E88117D4D5這樣的字串。

Google許久,未曾找到在ASP.NET中是怎麼實現上述樣子的輸出的。於是自己動手,查閱MSDN,無數次實踐,終於找到方法,就是用.NET中的BitConverter類,廢話不多說,把代碼貼出來,

private void btnMd5_Click( object sender, EventArgs e ) {
if (string.IsNullOrEmpty(txtSrc.Text)) {
txtSrc.Text = "Please Input your string to Encrype";
return;
}
MD5CryptoServiceProvider md5Encrypter = new MD5CryptoServiceProvider();
byte[] theSrc = Encoding.UTF8.GetBytes( txtSrc.Text );
byte[] theResBytes = md5Encrypter.ComputeHash( theSrc );
txtResult.Text = BitConverter.ToString( theResBytes ).Replace( "-", "" );
}

private void btnHash_Click( object sender, EventArgs e ) {
if (string.IsNullOrEmpty( txtSrc.Text )) {
txtSrc.Text = "Please Input your string to Encrype";
return;
}
HashAlgorithm hashEncrypter = new SHA1Managed();
byte[] theSrc = Encoding.UTF8.GetBytes( txtSrc.Text );
byte[] theResBytes = hashEncrypter.ComputeHash( theSrc );
txtResult.Text = BitConverter.ToString( theResBytes ).Replace( "-", "" );
}

BitConverter.ToString出來的是類似BA-BA-32-7D-24-17-46-EE這樣結果,滑鼠放到BitConverter.ToString上發現它的summary:Converts the numeric value of each element of a specified array of bytes to its equivalent hexadecimal string representation.

把string中的‘-’分離出來的方法應該是有很多種,鄙人實驗過之後感覺上述方法比較簡單,至於執行效率沒有比較O(∩_∩)O

需要此工具的可以下載收藏之:MD5及hash加密工具 (右鍵另存新檔)

 由此可見,漢字也是可以加密的,沒有亂碼。

 耐心看到最後的有福啦,其實在System.Web.Security.FormsAuthentication 中有一個專門用於加密密碼的方法,它就是HashPasswordForStoringInConfigFile   此法需要兩個參數,自己MSDN吧,遺憾的是此法只實現了MD5和SHA1兩種加密方法。

在此推薦一個線上加密工具,此站有很多比較實用的工具。http://www.aspnetresources.com/tools/pwdhash.aspx

如有問題,請看家不吝賜教~

 

附,剛剛看了一下 HashPasswordForStoringInConfigFile的內部實現,把代碼貼上來了~

代碼

public static string HashPasswordForStoringInConfigFile(string password, string passwordFormat)
{
HashAlgorithm algorithm;
if (password == null)
{
throw new ArgumentNullException("password");
}
if (passwordFormat == null)
{
throw new ArgumentNullException("passwordFormat");
}
if (StringUtil.EqualsIgnoreCase(passwordFormat, "sha1"))
{
algorithm = SHA1.Create();
}
else
{
if (!StringUtil.EqualsIgnoreCase(passwordFormat, "md5"))
{
throw new ArgumentException(SR.GetString("InvalidArgumentValue", new object[] { "passwordFormat" }));
}
algorithm = MD5.Create();
}
return MachineKeySection.ByteArrayToHexString(algorithm.ComputeHash(Encoding.UTF8.GetBytes(password)), 0);
}

 

聯繫我們

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