我在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;
}