我在 NodeJS 中有这个函数,我需要一些帮助才能转换为 C#,我的问题是从 randomBytes 获取“buf”
NodeJS 功能:
function generateKeys() {
return new Promise((resolve, reject) => {
const dh = crypto.createECDH('prime256v1');
dh.generateKeys();
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err);
}
return resolve({
privateKey : escape(dh.getPrivateKey('base64')),
publicKey : escape(dh.getPublicKey('base64')),
authSecret : escape(buf.toString('base64')),
});
});
});
}
到目前为止的 C# 代码:
private static AsymmetricCipherKeyPair GenerateKeyPairEcc()
{
var random = new Org.BouncyCastle.Security.SecureRandom();
var oid = X962NamedCurves.GetOid("prime256v1");
var generator = new ECKeyPairGenerator();
var genParam = new ECKeyGenerationParameters(oid, random);
generator.Init(genParam);
AsymmetricCipherKeyPair ackp = generator.GenerateKeyPair();
return ackp;
}
public byte[] RandomBytes()
{
byte[] random = new byte[16];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(random);
return random;
}