一、md5加密
using System.Security.Cryptography;
public string EncrytPasswordMD5(string password)
{
using (MD5 md5Hash = MD5.Create())
{
byte[] inputBytes = Encoding.ASCII.GetBytes(password);
byte[] hashBytes = md5Hash.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString();
}
}
二、SHA256加密
using System.Security.Cryptography;
public string EncrytPassword(string password)
{
using (SHA256 sHA256 = SHA256.Create())
{
byte[] inputBytes = Encoding.ASCII.GetBytes(password);
byte[] hashBytes = sHA256.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString();
}
}

本文介绍了在C#中使用System.Security.Cryptography库实现MD5和SHA256加密算法对密码进行哈希处理的方法,展示了如何将字符串转换为密文并以十六进制格式存储。
最低0.47元/天 解锁文章
677

被折叠的 条评论
为什么被折叠?



