C# PasswordDeriveBytes 混乱
我在 C# 中有以下代码,
PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
byte[] KeyBytes = DerivedPassword.GetBytes(32);
我使用“SHA1”哈希算法。
根据SHA1定义,其生成160位(20字节)密钥。我的问题是 GetBytes 方法如何从 DerivedPassword 获取 32 个字节,GetBytes 方法背后使用什么算法?
I have following code in C#
PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
byte[] KeyBytes = DerivedPassword.GetBytes(32);
I am using "SHA1" hashing algorithm.
According to SHA1 definition, its generate 160 bits (20 bytes) key. My question is how GetBytes method get 32 bytes from DerivedPassword, what algorithm used behind GetBytes method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Microsoft 的原始 PKCS#5(又名 PBKDF1)的实现包括不安全扩展,以提供比哈希函数可以提供的更多字节(请参阅错误报告 此处和此处)。
即使它没有错误,您也应该避免未记录的、专有的标准扩展(否则您将来可能永远无法解密您的数据 - 至少在 Windows 之外无法解密。)
我强烈建议您使用较新的
Rfc2898DeriveBytes
实现了自 .NET 2.0 起可用的 PBKDF2 (PKCS#5 v2)。Microsoft's implementation of original PKCS#5 (aka PBKDF1) include insecure extensions to provide more bytes than the hash function can provide (see bug reports here and here).
Even if it was not buggy you should avoid undocumented, proprietary extensions to standards (or you might never be able to decrypt your data in the future - at least not outside Windows.)
I strongly suggest you to use the newer
Rfc2898DeriveBytes
which implements PBKDF2 (PKCS#5 v2) which is available since .NET 2.0.它使用算法 PBKDF1,该算法稍作修改以允许任意密钥长度。替换类,
Rfc2898DeriveBytes
< /a> 使用 PBKDF2。您可以阅读有关 PBKDF2 的维基百科文章,大致了解推动这项工作的基本概念。
It uses the algorithm PBKDF1, which is slightly modified to allow arbitrary key length. A replacement class,
Rfc2898DeriveBytes
uses PBKDF2.You can read the Wikipedia Article on PBKDF2 for a general idea of what underlying concepts are making this work.
密钥派生函数使用称为密钥延伸的功能。 (不用费心在维基百科上查找它,因为当前的文章将这个概念与密钥强化混淆了,这是完全不同的东西。)
密钥拉伸通常是通过在以下任一中应用 PRF(例如哈希函数或密码)来完成的: CTR 模式,或者通过迭代并连接中间输出。
例如,如果您使用 CTR 过程、SHA-1 作为 PRF,并且需要 32 字节的伪随机输出,则可以将 SHA1(keymaterial,0) 与 SHA1(keymaterial,1) 的前 12 字节连接起来。
The key derivation function uses a feature called Key Stretching. (Don't bother looking it up on Wikipedia, because the current article confuses the concept with Key Strengthening, which is something completely different.)
Key stretching is commonly done by applying a PRF (such as a hash function or a cipher) in either CTR mode, or by iterating it and concatenating the intermediate outputs.
For instance, if you use the CTR procedure, SHA-1 as PRF, and want 32 bytes of pseudo random output, you concatenate SHA1(keymaterial,0) with the first 12 bytes of SHA1(keymaterial,1).