問題描述
我在 C# 中有以下代碼
I have following code in C#
PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
byte[] KeyBytes = DerivedPassword.GetBytes(32);
我正在使用SHA1"哈希算法.
I am using "SHA1" hashing algorithm.
根據 SHA1 定義,它生成 160 位(20 字節)的密鑰.我的問題是 GetBytes 方法如何從 DerivedPassword 中獲取 32 個字節,GetBytes 方法背后使用了什么算法?
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?
推薦答案
Microsoft 對原始 PKCS#5(又名 PBKDF1)的實現包括 insecure 擴展,以提供比散列函數所能提供的更多的字節(參見錯誤報告此處和這里).
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).
即使它沒有錯誤,您也應該避免對標準進行未記錄的專有擴展(否則您將來可能永遠無法解密您的數據 - 至少不能在 Windows 之外.)
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.)
我強烈建議您使用更新的 Rfc2898DeriveBytes
,它實現了自 .NET 2.0 起可用的 PBKDF2 (PKCS#5 v2).
I strongly suggest you to use the newer Rfc2898DeriveBytes
which implements PBKDF2 (PKCS#5 v2) which is available since .NET 2.0.
這篇關于C# PasswordDeriveBytes 混淆的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!