使用方法:
新建c#控制台应用程序,将下面的代码拷贝进去就可以使用了。代码解释见注释。
例1
using System; using System.Security.Cryptography; using System.Text; class Example { // Hash an input string and return the hash as // a 32 character hexadecimal string. static string getMd5Hash(string input) { // Create a new instance of the MD5CryptoServiceProvider object. MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider(); // Convert the input string to a byte array and compute the hash. byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input)); // Create a new Stringbuilder to collect the bytes // and create a string. StringBuilder sBuilder = new StringBuilder(); // Loop through each byte of the hashed data // and format each one as a hexadecimal string. for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } // Return the hexadecimal string. // return sBuilder.ToString();//返回32位的MD5值 return sBuilder.ToString().Substring(8, 16);//返回16位的MD5值 /*String.SubString(int index,int length) * index:开始位置,从0开始 * length:你要取的子字符串的长度 */ } // Verify a hash against a string. static bool verifyMd5Hash(string input, string hash) { // Hash the input. string hashOfInput = getMd5Hash(input); // Create a StringComparer an comare the hashes. StringComparer comparer = StringComparer.OrdinalIgnoreCase; if (0 == comparer.Compare(hashOfInput, hash)) { return true; } else { return false; } } static void Main() { string source = "Hello World!"; string hash = getMd5Hash(source); Console.WriteLine("The MD5 hash of " + source + " is: " + hash + "."); Console.WriteLine("Verifying the hash..."); if (verifyMd5Hash(source, hash)) { Console.WriteLine("The hashes are the same."); } else { Console.WriteLine("The hashes are not same."); } } } // This code example produces the following output: // // The MD5 hash of Hello World! is: ed076287532e86365e841e92bfc50d8c. // Verifying the hash... // The hashes are the same. /* * Encoding.ASCII.GetBytes(inputString)用于以ASCII方式将一个字符串转换成一个字节数组, * 原因是ComputeHash方法只接收Byte[]参数,后面的内容就是将加密后的Byte[]连成一个字符串, * AppendFormat中的格式字符串{0:x2}是指将数组中每一个字符格式化为十六进制,精度为2 */
例2:和例1基本相同
using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptography; namespace ConsoleApplication1 { class Program { public static void Main(string[] args) { Console.WriteLine("hello world !"); string str = Encryption.StringToMD5Hash("xuwei");//静态方法可以直接通过类名来调用 Console.WriteLine(str); } } class Encryption//32为的md5加密 { public static string StringToMD5Hash(string inputString) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] encryptedBytes = md5.ComputeHash(Encoding.ASCII.GetBytes(inputString)); StringBuilder sb = new StringBuilder(); for (int i = 0; i < encryptedBytes.Length; i++) { sb.AppendFormat("{0:x2}", encryptedBytes[i]); } return sb.ToString(); } } }
3月8号更新
整理博客的时候发现MD5的求法其实已经自带了,代码如下:
//注:自加求md5密码的静态方法 public static string GetMd5(string str)//求MD5 { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(8,16); }