我有将字符串加密为 sha256 并紧邻 base64 的代码:
public static string Sha256encrypt(string phrase)
{
UTF8Encoding encoder = new UTF8Encoding();
SHA256Managed sha256hasher = new SHA256Managed();
byte[] hashedDataBytes = sha256hasher.ComputeHash(encoder.GetBytes(phrase));
return Convert.ToBase64String(hashedDataBytes);
}
如何在另一端解密我的密码?
您无法解密结果单向哈希 http://en.wikipedia.org/wiki/Cryptographic_hash_function。相反,您应该做的是将输入密码的哈希值与数据库中存储的哈希值进行比较。
Example:
var password = "1234";
var hashedPassword = Sha256encrypt(password);
var allowLogin = hashedPassword == storedPassword; //storedPassword from Database, etc.
但这只是非常基础的知识,当使用哈希算法时,您应该考虑使用Salt http://en.wikipedia.org/wiki/Salt_%28cryptography%29 too.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)