导出不带私钥的 X.509 证书
我以为这很简单,但显然并非如此。 我安装了一个证书,该证书具有可导出的私钥,并且我想仅使用公钥以编程方式导出它。 换句话说,我想要一个相当于在通过 certmgr 导出并导出到 .CER 时选择“不导出私钥”的结果。
似乎所有 X509Certificate2.Export 方法都会导出私钥(如果存在),如 PKCS #12,这与我想要的相反。
有什么方法可以使用 C# 来完成此任务,还是我需要开始深入研究 CAPICOM?
I thought this would be straightforward but apparently it isn't. I have a certificate installed that has a private key, exportable, and I want to programmatically export it with the public key ONLY. In other words, I want a result equivalent to selecting "Do not export the private key" when exporting through certmgr and exporting to .CER.
It seems that all of the X509Certificate2.Export methods will export the private key if it exists, as PKCS #12, which is the opposite of what I want.
Is there any way using C# to accomplish this, or do I need to start digging into CAPICOM?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于其他可能偶然发现这一点的人,我已经弄清楚了。 如果将
X509ContentType.Cert
指定为X509Certificate.Export
的第一个(也是唯一一个)参数,则它仅导出公钥。 另一方面,指定X509ContentType.Pfx
包含私钥(如果存在)。我可以发誓上周我看到了不同的行为,但我在测试时一定已经安装了私钥。 当我今天删除该证书并从头开始时,我看到导出的证书中没有私钥。
For anyone else who might have stumbled on this, I figured it out. If you specify
X509ContentType.Cert
as the first (and only) parameter toX509Certificate.Export
, it only exports the public key. On the other hand, specifyingX509ContentType.Pfx
includes the private key if one exists.I could have sworn that I was seeing different behaviour last week, but I must have already had the private key installed when I was testing. When I deleted that certificate today and started again from scratch, I saw that there was no private key in the exported cert.
我发现以下程序有助于让自己放心,证书的
RawData
属性仅包含公钥(MSDN 对此不清楚),并且上面关于X509ContentType.Cert
的答案code> 与X509ContentType.Pfx
按预期工作:它输出以下内容:
I found the following program helpful for reassuring myself that the
RawData
property of the certificate contains only the public key (MSDN is unclear on this), and that the answer above regardingX509ContentType.Cert
vs.X509ContentType.Pfx
works as expected:It outputs the following:
有一个 OpenSSL .NET 包装器,您可能会觉得有用。
There is an OpenSSL .NET wrapper you may find useful.