gpt4 book ai didi

c# - Bouncy CaSTLe C# 中的 PBKDF2

转载 作者:可可西里 更新时间:2023-11-01 07:56:36 26 4
gpt4 key购买 nike

我一直在研究 C# Bouncy CaSTLe API 以了解如何执行 PBKDF2 key 派生。

我现在真的是一头雾水

我尝试通读 Pkcs5S2ParametersGenerator.cs 和 PBKDF2Params.cs 文件,但我真的不知道该怎么做。

根据我目前所做的研究,PBKDF2 需要一个字符串(或 char[]),它是密码、salt 和迭代计数。

到目前为止,我最有前途和最明显的是 PBKDF2Params 和 Pkcs5S2ParametersGenerator。

这些似乎都不接受字符串或 char[]。

有没有人用 C# 做过这个或者对此有任何线索?或者有人已经用 Java 实现了 BouncyCaSTLe 并且可以提供帮助?

提前致谢:)

更新:我已经在 Bouncy CaSTLe 中找到了执行此操作的方法。在下面寻找答案:)

最佳答案

经过数小时研究代码后,我发现最简单的方法是提取 Pkcs5S2ParametersGenerator.cs 中的部分代码并创建我自己的类,当然该类使用其他 BouncyCaSTLe API。这与 Dot Net Compact Framework (Windows Mobile) 完美配合。这相当于 Dot Net Compact Framework 2.0/3.5 中不存在的 Rfc2898DeriveBytes 类。好吧,也许不是完全等同的,但可以完成工作:)

这是 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;
}


}
}

那么如何使用这个功能呢?简单的! :)这是一个非常简单的示例,其中密码和盐由用户提供。

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/

我得到了一致的结果 :)如果有人得到不正确的结果,请报告 :)

希望这对某人有帮助:)

更新:确认使用此处提供的测试向量

https://datatracker.ietf.org/doc/html/draft-josefsson-pbkdf2-test-vectors-00

更新:或者,对于盐,我们可以使用 RNGCryptoServiceProvider。确保引用 System.Security.Cryptography 命名空间。

RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();        

byte[] salt = new byte[16];

rng.GetBytes(salt);

关于c# - Bouncy CaSTLe C# 中的 PBKDF2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3210795/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com