C# MD5加密字符串

2021-07-16 06:13

阅读:391

标签:coding   string   sub   c#   odi   span   字符   32位   默认   

/// 
/// 用MD5加密字符串,可选择生成16位或者32位的加密字符串
/// 
/// 待加密的字符串
/// 位数,一般取值16 或 32
/// 返回的加密后的字符串
public string MD5Encrypt(string password, int bit)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] hashedDataBytes;
hashedDataBytes = md5Hasher.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(password));
StringBuilder tmp = new StringBuilder();
foreach (byte i in hashedDataBytes)
{
tmp.Append(i.ToString("x2"));
}
if (bit == 16)
return tmp.ToString().Substring(8, 16);
else
if (bit == 32) return tmp.ToString();//默认情况
else return string.Empty;
}

 

/// 
/// 用MD5加密字符串
/// 
/// 待加密的字符串
/// 
public string MD5Encrypt(string password)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] hashedDataBytes;
hashedDataBytes = md5Hasher.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(password));
StringBuilder tmp = new StringBuilder();
foreach (byte i in hashedDataBytes)
{
tmp.Append(i.ToString("x2"));
}
return tmp.ToString();
}

 

命名空间:System.Security.Cryptography.MD5CryptoServiceProvider 

C# MD5加密字符串

标签:coding   string   sub   c#   odi   span   字符   32位   默认   

原文地址:https://www.cnblogs.com/xifengyeluo/p/8215062.html


评论


亲,登录后才可以留言!