/// <summary> /// 获取字符串MD5值 /// </summary> /// <param name="source">源数据</param> /// <returns>计算后的MD5字符串</returns> public static string GetMD5Value(string source) { //将字符串编码为字节序列 byte[] bt = Encoding.UTF8.GetBytes(source); return GetMD5Value(bt); } /// <summary> /// 获取byte[] MD5值 /// </summary> /// <param name="source">源数据</param> /// <returns>计算后的MD5字符串</returns> public static string GetMD5Value(byte[] source) { //创建默认实现的实例 MD5 md5 = MD5.Create(); //计算指定字节数组的哈希值。 Byte[] md5bt = md5.ComputeHash(source); //将byte数组转换为字符串 StringBuilder builder = new StringBuilder(); foreach (var item in md5bt) { builder.Append(item.ToString("x2")); } string md5Str = builder.ToString(); return md5Str; }
本文来自www.luofenming.com