public class MD5Help { //此代碼示例通過創建哈希字元串適用於任何 MD5 哈希函數 (在任何平臺) 上創建 32 個字元的十六進位格式哈希字元串官網案例改編 /// <summary> ///獲取32位md5加密 /// </summary> /// <param name=... ...
public class MD5Help { //此代碼示例通過創建哈希字元串適用於任何 MD5 哈希函數 (在任何平臺) 上創建 32 個字元的十六進位格式哈希字元串官網案例改編 /// <summary> ///獲取32位md5加密 /// </summary> /// <param name="source">待解密的字元串</param> /// <returns></returns> public static string Get32MD5(string source) { using (MD5 md5Hash = MD5.Create()) { byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(source + MD5Key)); StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } string hash = sBuilder.ToString(); return hash.ToUpper(); } } static string MD5Key = "Y29RPm!*Af"; /// <summary> /// 獲取16位md5加密 /// </summary> /// <param name="source">待解密的字元串</param> /// <returns></returns> public static string Get16MD5(string source) { using (MD5 md5Hash = MD5.Create()) { byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(source + MD5Key)); //轉換成字元串,並取9到25位 string sBuilder = BitConverter.ToString(data, 4, 8); //BitConverter轉換出來的字元串會在每個字元中間產生一個分隔符,需要去除掉 sBuilder = sBuilder.Replace("-", ""); return sBuilder.ToString().ToUpper(); } } }