从 BouncyCastle X509 证书获取私钥? C#
通常,当我从密钥库中获取 X509Certificate2
时,我可以调用 .PrivateKey
来检索证书的私钥作为 AmetryAlgorithm
。但是,我决定使用 Bouncy Castle,其 X509Certificate
实例只有一个 getPublicKey();
我看不到从证书中获取私钥的方法。有什么想法吗?
我从我的 Windows-MY 密钥库获取 X509Certificate2,然后使用:
//mycert is an X509Certificate2 retrieved from Windows-MY Keystore
X509CertificateParser certParser = new X509CertificateParser();
X509Certificate privateCertBouncy = certParser.ReadCertificate(mycert.GetRawCertData());
AsymmetricKeyParameter pubKey = privateCertBouncy.GetPublicKey();
//how do i now get the private key to make a keypair?
是否有办法将 AmetryAlgorithm
(C# 私钥)转换为 AmetryKeyParameter
(bouncycastle 私钥)?
Normally when I grab an X509Certificate2
out of my keystore I can call .PrivateKey
to retrieve the cert's private key as an AsymmetricAlgorithm
. However I have decided to use Bouncy Castle and its instance of X509Certificate
only has a getPublicKey();
I cannot see a way to get the private key out of the cert. Any ideas?
I get the an X509Certificate2 from my Windows-MY keystore then use:
//mycert is an X509Certificate2 retrieved from Windows-MY Keystore
X509CertificateParser certParser = new X509CertificateParser();
X509Certificate privateCertBouncy = certParser.ReadCertificate(mycert.GetRawCertData());
AsymmetricKeyParameter pubKey = privateCertBouncy.GetPublicKey();
//how do i now get the private key to make a keypair?
Is there anyway to convert a AsymmetricAlgorithm
(C# private key) to a AsymmetricKeyParameter
(bouncycastle private key)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对 BouncyCastle 不太了解,但在我看来,最简单的事情就是根据关键参数重新创建密钥。
您可以使用以下方式调用代码:
显然,这假设证书包含 RSA 密钥,但使用
DSACryptoServiceProvider
和DSAParameters
对于 DSA 可以实现相同的结果。Don't know BouncyCastle that much but it seems to me that the simple thing to do is to recreate the key based on the key parameters.
You can call the code by using
Obviously this assumes that the certificate includes a RSA Key but the same result can be achieved for DSA with
DSACryptoServiceProvider
andDSAParameters
.找到.NET X509Certificate2:
将其解析为BouncyCastle证书并使用X509Certificate2Signature获取签名:
Find .NET X509Certificate2:
Parse it to BouncyCastle certificate and use X509Certificate2Signature to get signature: