.net md5 加密方式很多,有些已经淘汰
这里列一些常件的md5加密方式.
1. 最常用,简单
/// <summary>
/// MD5加密。
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string MD5(string value)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(value));
StringBuilder str = new StringBuilder();
for (int i = 0; i < data.Length; i++) { str.Append(data[i].ToString("x2")); }
return str.ToString();
}
2.需要传多个参数
/// <summary>
/// MD5加密
/// </summary>
/// <param name="value"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static string MD5To32(string value, string encoding)
{
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] t = md5.ComputeHash(System.Text.Encoding.GetEncoding(encoding).GetBytes(value));
var sb = new System.Text.StringBuilder(32);
for (int i = 0; i < t.Length; i++)
{
sb.Append(t[i].ToString("x").PadLeft(2, '0'));
}
return sb.ToString();
}
3.16位MD5加密方式
public static string MD5To16(string str)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(str)), 4, 8);
t2 = t2.Replace("-", "");
t2 = t2.ToLower();
return t2;
}
4.将方法汇总后,写成通用
#region MD5相关加密
public static string MD5_16Small(string str)
{
return MD5(str, "utf-8", true, true);
}
public static string MD5_16Case(string str)
{
return MD5(str, "utf-8", true, false);
}
/// <summary>
/// 32位MD5 大写加密 默认UTF-8加密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string MD5_32Small(string str)
{
return MD5(str, "utf-8", true);
}
/// <summary>
/// 32位MD5 小写加密
/// </summary>
/// <param name="str"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static string MD5_32Small(string str, string encoding)
{
return MD5(str, "utf-8", true);
}
/// <summary>
/// 32位MD5 大写加密 默认UTF-8加密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string MD5_32Case(string str)
{
return MD5(str, "utf-8", false);
}
public static string MD5_32Case(string str, string encoding)
{
return MD5(str, "utf-8", true);
}
/// <summary>
/// 32位MD5 大写加密
/// </summary>
/// <param name="str">需加密字符串</param>
/// <param name="encoding">编码</param>
/// <returns></returns>
public static string MD5(string str, string encoding, bool issmall)
{
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] t = md5.ComputeHash(System.Text.Encoding.GetEncoding(encoding).GetBytes(str));
var sb = new System.Text.StringBuilder(32);
for (int i = 0; i < t.Length; i++)
{
sb.Append(t[i].ToString(issmall ? "x" : "X").PadLeft(2, '0'));
}
return sb.ToString();
}
public static string MD5(string str, string encoding, bool issixteen, bool issmall)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] byData = System.Text.Encoding.GetEncoding(encoding).GetBytes(str);
byte[] result = md5.ComputeHash(byData);
string md5str = issixteen ? BitConverter.ToString(result, 4, 8) : BitConverter.ToString(result);
md5str = md5str.Replace("-", "");
return issmall ? md5str.ToLower() : md5str;
}
/// <summary>
/// MD5 16位小写2
/// </summary>
/// <param name="str">需加密字符串</param>
/// <returns></returns>
public static string MD5_32SmallTwo(string str)
{
return MD5_Two(str, true);
}
/// <summary>
/// MD5 16位大写2
/// </summary>
/// <param name="str">需加密字符串</param>
/// <returns></returns>
public static string MD5_32CaseTwo(string str)
{
return MD5_Two(str, false);
}
public static string MD5_Two(string str, bool issmall)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(str)));
t2 = t2.Replace("-", "");
return issmall ? t2.ToLower() : t2;
}
#endregion