roo*_*kie -3 c# encryption cryptography aes password-protection
我搜索了一些代码来做到这一点,但发现了一些预先定义的盐.我想为每个用户自动生成salt并将salt值存储在表中.谢谢我是编程新手,请帮忙
(正如所建议的那样,我用一些应该更安全的东西取代了我以前的盐生成方法)
要生成随机盐:
public static string GenerateRandomSalt(RNGCryptoServiceProvider rng, int size)
{
var bytes = new Byte[size];
rng.GetBytes(bytes);
return Convert.ToBase64String(bytes);
}
var rng = new RNGCryptoServiceProvider();
var salt1 = GenerateRandomSalt(rng, 16);
var salt2 = GenerateRandomSalt(rng, 16);
// etc.
Run Code Online (Sandbox Code Playgroud)
RNGCryptoServiceProvider
用于生成"加密强随机值",使其比标准Random
类更适合使用.但是,您生成salt,然后可以使用您选择的算法将其附加到您的密码和哈希:
var salt = GenerateRandomSalt(rng, 16);
var hashedPassword = DoPasswordHashing(password + salt);
Run Code Online (Sandbox Code Playgroud)
但是,值得指出的是,正确进行用户身份验证可能是一个比看起来更难的问题.Eric Lippert几年前撰写了一系列博客文章:http: //blogs.msdn.com/b/ericlippert/archive/2005/01/28/you-want-salt-with-that-part-one-安全-VS-obscurity.aspx
归档时间: |
|
查看次数: |
8474 次 |
最近记录: |