从 BouncyCastle 导出 EC 私钥并导入到 CngKey 或 ECDsaCng 中?
我已经使用 BouncyCastle 创建了椭圆曲线 DSA 签名的密钥对,并设法使用 XMLString 根据 RFC4050。现在我也想移动私钥,但还没有找到解决方案。我得到的最接近的是使用 CngKey.Import。
CngKey.Import 支持 PKCS#8 格式,因此如果您可以将密钥放入有效的 pkcs8 中,那么它应该可以工作。不幸的是,下面的代码不太有效。
var privatekey = (ECPrivateKeyParameters) keyPair.Private;
var pkinfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privatekey);
byte[] pkcs8Blob = pkinfo.GetDerEncoded();
var importedKey = CngKey.Import(pkcs8Blob, CngKeyBlobFormat.Pkcs8PrivateBlob);
这会引发异常:
System.Security.Cryptography.CryptographicException:满足 ASN1 错误标记值。
据我所知,GetDerEncoded 应该返回一个有效的 Pkcs8 blob。
如何在 ECDsaCng 对象中使用 BouncyCastle 创建的私钥?
I have created key pairs for elliptic curve DSA signatures using BouncyCastle and managed to import the public key into ECDsaCng using an XMLString accoding to RFC4050. Now I want to also move the private key and have not managed to find a solution. The closest I have got is using CngKey.Import.
CngKey.Import supports PKCS#8 format so if you can get your keys into valid pkcs8 then it should work. Unfortunately the following code does not quite work.
var privatekey = (ECPrivateKeyParameters) keyPair.Private;
var pkinfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privatekey);
byte[] pkcs8Blob = pkinfo.GetDerEncoded();
var importedKey = CngKey.Import(pkcs8Blob, CngKeyBlobFormat.Pkcs8PrivateBlob);
This throws an exception:
System.Security.Cryptography.CryptographicException: ASN1 bad tag value met.
GetDerEncoded should return a valid Pkcs8 blob as far as I can tell.
How can I use the private key created with BouncyCastle in a ECDsaCng object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番绞尽脑汁、阅读 RFC 并研究 BouncyCastle 和 CngKey.Export 生成的字节数组后,我找到了答案。
问题在于 BouncyCastle 如何将 EC 密钥编码为 DER/Pkcs8 格式。与此特定问题相关的两个 RFC 是 RFC5915(不是标准,而是共识文件)和 RFC5480。他们指出必须使用 RFC5480 中引用的命名曲线来指定曲线参数。当您使用错误的生成器参数创建 AsymmetryCipherKeyPair 时,PKCS8/DER 导出的 BouncyCastle 实现将导出不符合这两个规范的整个曲线规范(隐式曲线)。您必须使用指定命名曲线的 ECKeyGenerationParameters。
在 BouncyCastle 中创建可互操作的密钥时,必须使用以下内容(据我所知):
可以通过使用 Der 编码字节导入密钥来创建 CngKey:
After much hair-pulling, reading RFCs and studying the byte arrays generated by BouncyCastle and CngKey.Export I have found the answer.
The issue lies in how BouncyCastle encodes the EC key to DER/Pkcs8 format. The two RFCs which are relevant to this particular issue are RFC5915 (which is not a standard but instead a consensus document) and RFC5480. They state that curve parameters must be specified using named curves referenced in RFC5480. The BouncyCastle implementation of PKCS8/DER exporting will export the entire curve specification (implicit curve) which is not compliant with these two specs when you create the AsymmetricCipherKeyPair using the wrong generator parameters. You must use the ECKeyGenerationParameters which specify a named curve.
The following must be used (as far as I can tell) when creating interoperable keys in BouncyCastle:
The CngKey can be created by importing the key using the Der encoded bytes: