将证书导出为 BASE-64 编码的 .cer
我正在尝试导出没有私钥的证书作为 BASE-64 编码文件,与从 Windows 导出它相同。从 Windows 导出时,我可以在记事本中打开 .cer 文件。
当我尝试以下操作并在记事本上打开时,我得到二进制数据......我认为它......不可读。
X509Certificate2 cert = new X509Certificate2("c:\\myCert.pfx", "test", X509KeyStorageFlags.Exportable);
File.WriteAllBytes("c:\\testcer.cer", cert.Export(X509ContentType.Cert));
我尝试删除“X509KeyStorageFlags.Exportable”,但这不起作用。我错过了什么吗?
编辑 - 我尝试过
File.WriteAllText("c:\\testcer.cer",Convert.ToBase64String(cert.Export(X509ContentType.Cert)))
,这似乎有效,但是缺少“-----BEGIN CERTIFICATE-----”和“-----结束证书-----”
I am trying to export a cert without the private key as as BASE-64 encoded file, same as exporting it from windows. When exported from windows I am able to open the .cer file in notepad.
When I try the following and open on notepad I get binary data...I think it is...not readable.
X509Certificate2 cert = new X509Certificate2("c:\\myCert.pfx", "test", X509KeyStorageFlags.Exportable);
File.WriteAllBytes("c:\\testcer.cer", cert.Export(X509ContentType.Cert));
I tried removing the 'X509KeyStorageFlags.Exportable" but that doesn't work. Am I missing something?
Edit - I tried
File.WriteAllText("c:\\testcer.cer",Convert.ToBase64String(cert.Export(X509ContentType.Cert)))
and that seems to work, however, missing the "-----BEGIN CERTIFICATE-----" and "-----END CERTIFICATE-----"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许
Perhaps
试试这个:
try this:
对于那些在 .NET Core 中实现类似功能的人,这里是基于 tyranid 所做的代码。 .NET Core 中不存在 Base64FormattingOptions.InsertLineBreaks,因此我必须实现自己的方式来进行换行。
For those implementing something similar in .NET Core, here's the code, based on what tyranid did. Base64FormattingOptions.InsertLineBreaks doesn't exist in .NET Core, so I had to implement my own way to do line breaking.
//然而,缺少“-----BEGIN CERTIFICATE-----”和“-----END CERTIFICATE-----”
这些缺少的行是可选的。 CA 可能会生成或不生成它们,具体取决于设置。出于所有实际原因,可以将它们从 Base64 编码文件中删除。
//however, missing the "-----BEGIN CERTIFICATE-----" and "-----END CERTIFICATE-----"
These missing lines are optional. CA may generate them or not depending on settings. For all practical reasons they can be removed from the Base64 encoded file.