using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptography; namespace Auto { class GetMD5 { /// <summary>返回 MD5 值</summary> /// <param name="myString">要转换的 MD5 值的字符串</param> /// 与web下System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile等同 public static string GetOf(string myString) { string md5str = ""; MD5 md5 = new MD5CryptoServiceProvider(); //byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(this.txtMD5Ori.Text.Trim())); // 中英文转换与web不同 byte[] result = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(myString.Trim())); // 中英文转换与web相同 string str = ""; for (int i = 0; i < result.Length; i++) { str += string.Format("{0:x2}", result[i]); // 此处的x2,和写x得出的结果不一样,应该用x2就对了。 } md5str = str.ToLower(); // this.lblMD5size.Text = str.Length.ToString(); return md5str; } } }