您可以使用散列算法一样MD5,SHA1,SHA265,SHA512,...哈希密码.例如:
public string Hash(string password)
{
var bytes = new UTF8Encoding().GetBytes(password);
var hashBytes = System.Security.Cryptography.MD5.Create().ComputeHash(bytes);
return Convert.ToBase64String(hashBytes);
}
Run Code Online (Sandbox Code Playgroud)
然后将密码的哈希值存储在数据库中,当您想要将输入的密码与数据库存储值进行比较时,将输入值的哈希值与数据库值进行比较.
编辑
public string Hash(string password)
{
var bytes = new UTF8Encoding().GetBytes(password);
byte[] hashBytes;
using (var algorithm = new System.Security.Cryptography.SHA512Managed())
{
hashBytes = algorithm.ComputeHash(bytes);
}
return Convert.ToBase64String(hashBytes);
}
Run Code Online (Sandbox Code Playgroud)
这只是一个简单的例子:在实际场景中,你也应该使用a salt作为哈希.你可以在这里阅读更多关于盐渍的信息.