[英]How to create a PBKDF2-SHA256 password hash in C# / Bouncy Castle
[英]PBKDF2 in Bouncy Castle C#
我正在弄亂C#Bouncy Castle API以找到如何進行PBKDF2密鑰派生。
我現在真的很無能為力。
我嘗試通過Pkcs5S2ParametersGenerator.cs和PBKDF2Params.cs文件閱讀,但我真的無法弄清楚如何做到這一點。
根據我迄今為止所做的研究,PBKDF2需要一個字符串(或char []),它是密碼,鹽和迭代計數。
到目前為止,迄今為止最有前途和最明顯的是PBKDF2Params和Pkcs5S2ParametersGenerator。
這些似乎都不接受字符串或char []。
有沒有人用C#做過這個或者對此有任何線索? 或者也許有人在Java中實現了BouncyCastle並可以提供幫助?
Thanx提前很多:)
更新:我在Bouncy Castle找到了如何做到這一點。 看下面的答案:)
經過幾個小時的代碼后,我發現最簡單的方法是在Pkcs5S2ParametersGenerator.cs中獲取代碼的一部分並創建我自己的類,當然使用其他BouncyCastle API。 這與Dot Net Compact Framework(Windows Mobile)完美配合。 這相當於Rfc2898DeriveBytes類,它不存在於Dot Net Compact Framework 2.0 / 3.5中。 好吧,也許不是完全等同但是做的工作:)
這是PKCS5 / PKCS#5
使用的PRF(偽隨機函數)將是HMAC-SHA1
首先,第一件事。 從http://www.bouncycastle.org/csharp/下載Bouncy Castle編譯程序集,添加BouncyCastle.Crypto.dll
作為項目的參考。
之后,使用下面的代碼創建新的類文件。
using System;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Macs;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Security;
namespace PBKDF2_PKCS5
{
class PBKDF2
{
private readonly IMac hMac = new HMac(new Sha1Digest());
private void F(
byte[] P,
byte[] S,
int c,
byte[] iBuf,
byte[] outBytes,
int outOff)
{
byte[] state = new byte[hMac.GetMacSize()];
ICipherParameters param = new KeyParameter(P);
hMac.Init(param);
if (S != null)
{
hMac.BlockUpdate(S, 0, S.Length);
}
hMac.BlockUpdate(iBuf, 0, iBuf.Length);
hMac.DoFinal(state, 0);
Array.Copy(state, 0, outBytes, outOff, state.Length);
for (int count = 1; count != c; count++)
{
hMac.Init(param);
hMac.BlockUpdate(state, 0, state.Length);
hMac.DoFinal(state, 0);
for (int j = 0; j != state.Length; j++)
{
outBytes[outOff + j] ^= state[j];
}
}
}
private void IntToOctet(
byte[] Buffer,
int i)
{
Buffer[0] = (byte)((uint)i >> 24);
Buffer[1] = (byte)((uint)i >> 16);
Buffer[2] = (byte)((uint)i >> 8);
Buffer[3] = (byte)i;
}
// Use this function to retrieve a derived key.
// dkLen is in octets, how much bytes you want when the function to return.
// mPassword is the password converted to bytes.
// mSalt is the salt converted to bytes
// mIterationCount is the how much iterations you want to perform.
public byte[] GenerateDerivedKey(
int dkLen,
byte[] mPassword,
byte[] mSalt,
int mIterationCount
)
{
int hLen = hMac.GetMacSize();
int l = (dkLen + hLen - 1) / hLen;
byte[] iBuf = new byte[4];
byte[] outBytes = new byte[l * hLen];
for (int i = 1; i <= l; i++)
{
IntToOctet(iBuf, i);
F(mPassword, mSalt, mIterationCount, iBuf, outBytes, (i - 1) * hLen);
}
//By this time outBytes will contain the derived key + more bytes.
// According to the PKCS #5 v2.0: Password-Based Cryptography Standard (www.truecrypt.org/docs/pkcs5v2-0.pdf)
// we have to "extract the first dkLen octets to produce a derived key".
//I am creating a byte array with the size of dkLen and then using
//Buffer.BlockCopy to copy ONLY the dkLen amount of bytes to it
// And finally returning it :D
byte[] output = new byte[dkLen];
Buffer.BlockCopy(outBytes, 0, output, 0, dkLen);
return output;
}
}
}
那么如何使用這個功能呢? 簡單! :)這是一個非常簡單的示例,其中密碼和salt由用戶提供。
private void cmdDeriveKey_Click(object sender, EventArgs e)
{
byte[] salt = ASCIIEncoding.UTF8.GetBytes(txtSalt.Text);
PBKDF2 passwordDerive = new PBKDF2();
// I want the key to be used for AES-128, thus I want the derived key to be
// 128 bits. Thus I will be using 128/8 = 16 for dkLen (Derived Key Length) .
//Similarly if you wanted a 256 bit key, dkLen would be 256/8 = 32.
byte[] result = passwordDerive.GenerateDerivedKey(16, ASCIIEncoding.UTF8.GetBytes(txtPassword.Text), salt, 1000);
//result would now contain the derived key. Use it for whatever cryptographic purpose now :)
//The following code is ONLY to show the derived key in a Textbox.
string x = "";
for (int i = 0; i < result.Length; i++)
{
x += result[i].ToString("X");
}
txtResult.Text = x;
}
如何檢查這是否正確? 有一個PBKDF2的在線JavaScript實現http://anandam.name/pbkdf2/
我得到了一致的結果:)請報告是否有人得到錯誤的結果:)
希望這有助於某人:)
更新:確認使用此處提供的測試向量
http://tools.ietf.org/html/draft-josefsson-pbkdf2-test-vectors-00
更新:或者,對於鹽,我們可以使用RNGCryptoServiceProvider
。 確保引用System.Security.Cryptography
命名空間。
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] salt = new byte[16];
rng.GetBytes(salt);
我自己就遇到了這個問題,並找到了一種更直接的方法。 至少從Bouncy Castle 1.7開始,你可以這樣做(在VB中使用Org.BouncyCastle.Crypto):
Dim bcKeyDer As New Generators.Pkcs5S2ParametersGenerator()
bcKeyDer.Init(password, salt, keyIterations)
Dim bcparam As Parameters.KeyParameter = bcKeyDer.GenerateDerivedParameters("aes256", 256)
Dim key1() As Byte = bcparam.GetKey()
我已經針對.Net的System.Security.Cryptography進行了測試,它確實有效!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.