本文实例讲述了C#实现的字符串转MD5码函数。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/* 测试环境:WinXP SP3、Visual Studio 2008 SP1、Visual Studio 2010 SP1 更新日期:2014-04-23 */ public string CalculateMD5Hash( string input) { MD5 md5 = System.Security.Cryptography.MD5.Create(); byte [] inputBytes = System.Text.Encoding.UTF8.GetBytes(input); byte [] hash = md5.ComputeHash(inputBytes); // step 2, convert byte array to hex string StringBuilder sb = new StringBuilder(); for ( int i = 0; i < hash.Length; i++) { sb.Append(hash[i].ToString( "X2" )); } return sb.ToString(); } //end func |
希望本文所述对大家C#程序设计有所帮助。