简体   繁体   English

使用Bouncy Castle生成用于DH密钥交换的64字节公钥

[英]Generating 64-byte public key for DH key exchange using Bouncy Castle

I need to generate a key pair for Diffie-Hellman key exchange using the secp256r1 curve using Bouncy Castle in C#. 我需要使用C#中的Bouncy Castle使用secp256r1曲线为Diffie-Hellman密钥交换生成密钥对。 The public key should be 64 bytes (I'm not sure which format this is (compressed?), but I can find out). 公钥应该是64字节(我不确定这是哪种格式(压缩?),但我可以找到)。

I have the following code, but the lack of Bouncy Castle documentation has me lost. 我有以下代码,但缺乏Bouncy Castle文档让我输了。

X9ECParameters ecParams = SecNamedCurves.GetByName("secp256r1");
var spec = new ECDomainParameters(ecParams.Curve, ecParams.G, ecParams.N, ecParams.H, ecParams.GetSeed());
var keyGenParams = new ECKeyGenerationParameters(spec, new SecureRandom());

var keyGen = new ECKeyPairGenerator("ECDH");
keyGen.Init(keyGenParams);
AsymmetricCipherKeyPair keyPair = keyGen.GenerateKeyPair();
var pubKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyPair.Public);

// pubKeyInfo.GetDerEncoded()
// pubKeyInfo.GetEncoded()
// pubKeyInfo.PublicKey
// pubKeyInfo.ToAsn1Object()

From here, I have access to a lot of different export formats (seen above), but most values end up being 335 bytes long. 从这里开始,我可以访问许多不同的导出格式(如上所示),但大多数值最终都是335字节长。 pubKeyInfo.PublicKeyData.GetBytes() gives 65 bytes, but I'm not sure why. pubKeyInfo.PublicKeyData.GetBytes()给出了65个字节,但我不知道为什么。

I must have some basic misunderstanding here. 我必须在这里有一些基本的误解。 What am I missing? 我错过了什么?

If it helps, another client uses the uECC_make_key function to generate the key pair. 如果有帮助,另一个客户端使用uECC_make_key函数生成密钥对。

I believe the data is provided by pubKeyInfo.PublicKeyData.GetBytes() . 我相信数据是由pubKeyInfo.PublicKeyData.GetBytes()提供的。 It's 65 bytes long because it has a prefix 0x04 byte indicating that the remaining 64 bytes describe the public key in uncompressed format (vs. 0x02 indicating compressed format). 它长65个字节,因为它有一个前缀0x04字节,表示剩余的64个字节以未压缩格式描述公钥(而0x02表示压缩格式)。

See here for some discussion. 请参阅此处进行讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM