/// <summary> /// MD5编码 /// </summary> /// <param name="value">明文</param> /// <param name="bit">位长</param> /// <returns></returns> public static string Get(string value, MD5Bit bit) { // Create a new Stringbuilder to collect the bytes // and create a string. StringBuilder sBuilder = new StringBuilder(); if (bit == MD5Bit.L32) { // Create a new instance of the MD5CryptoServiceProvider object. System.Security.Cryptography.MD5 md5Hasher = System.Security.Cryptography.MD5.Create(); // Convert the input string to a byte array and compute the hash. byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(value)); // 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")); } } if (bit == MD5Bit.L16) { // This is one implementation of the abstract class MD5. System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); string temp = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(value)), 4, 8); temp = temp.Replace("-", ""); sBuilder.Append(temp); } // Return the hexadecimal string. return(sBuilder.ToString()); }
/// <summary> /// 生成不可逆的32位MD5哈希密码 /// </summary> /// <param name="strSource">需要加密的字符串</param> /// <param name="md5Bit">MD5加密的位数</param> /// <returns>加密后的MD5哈希密码</returns> public static string MD5Encrypt(string strSource, MD5Bit md5Bit) { if (string.IsNullOrEmpty(strSource)) { throw new SourceIsNullOrEmptyException(); } string MD5EncryptString = string.Empty; switch (md5Bit) { case MD5Bit.Bit16: MD5EncryptString = FormsAuthentication.HashPasswordForStoringInConfigFile(strSource, "MD5").ToUpper().Substring(8, 16); break; default: MD5EncryptString = FormsAuthentication.HashPasswordForStoringInConfigFile(strSource, "MD5").ToUpper(); break; } return(MD5EncryptString); }
/// <summary> /// MD5加密 /// </summary> /// <param name="data">需要加密的字符串</param> /// <param name="encode">字符的编码</param> /// <param name="bit">md5位数,16位|32位,默认用32位</param> /// <returns></returns> public static string Encrypt(string data, Encoding encode, MD5Bit bit = MD5Bit.Bit32) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] hash; if (bit == MD5Bit.Bit16) { hash = md5.ComputeHash(encode.GetBytes(data), 4, 8); } else { hash = md5.ComputeHash(encode.GetBytes(data)); } StringBuilder sb = new StringBuilder(32); for (int i = 0; i < hash.Length; i++) { sb.Append(hash[i].ToString("x").PadLeft(2, '0')); } return(sb.ToString()); }
/// <summary> /// 生成不可逆的32位MD5哈希密码 /// </summary> /// <param name="strSource">需要加密的字符串</param> /// <param name="md5Bit">MD5加密的位数</param> /// <returns>加密后的MD5哈希密码</returns> public static string MD5Encrypt(string strSource, MD5Bit md5Bit) { if (string.IsNullOrEmpty(strSource)) { throw new SourceIsNullOrEmptyException(); } string MD5EncryptString = string.Empty; switch (md5Bit) { case MD5Bit.Bit16: MD5EncryptString = FormsAuthentication.HashPasswordForStoringInConfigFile(strSource, "MD5").ToUpper().Substring(8, 16); break; default: MD5EncryptString = FormsAuthentication.HashPasswordForStoringInConfigFile(strSource, "MD5").ToUpper(); break; } return MD5EncryptString; }
/// <summary> /// MD5加密 /// </summary> /// <param name="data">需要加密的字符串</param> /// <param name="bit">md5位数,16位|32位,默认用32位</param> /// <returns></returns> public static string Encrypt(string data, MD5Bit bit = MD5Bit.Bit32) { return(Encrypt(data, new UTF8Encoding())); }