导出证书作为BASE

导出证书作为BASE

本文介绍了导出证书作为BASE-64连接codeD .CER的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想没有私钥导出证书是为BASE-64连接codeD文件,与从窗户将其导出。当从窗户远销我可以在记事本中打开.cer文件。

当我尝试以下方法和记事本,我得到的二进制数据开放...我认为这是......无法读取。

  X509Certificate2证书=新X509Certificate2(C:\\ myCert.pfx​​,测试,X509KeyStorageFlags.Exportable);

File.WriteAllBytes(C:\\ testcer.cer,cert.Export(X509ContentType.Cert));
 

我试图消除X509KeyStorageFlags.Exportable但是,这并不工作。我失去了一些东西?

编辑 - 我试图

<$p$p><$c$c>File.WriteAllText("c:\\testcer.cer",Convert.ToBase64String(cert.Export(X509ContentType.Cert)))

这似乎然而工作,缺少----- BEGIN CERTIFICATE -----和----- END CERTIFICATE -----

解决方案

也许

  ///&LT;总结&gt;
///导出证书为PEM格式字符串
///&LT; /总结&gt;
///&LT; PARAM NAME =证书&gt;至导出证书&LT; /参数&GT;
///&LT;返回&gt;将PEM连接codeD字符串&LT; /回报&GT;
公共静态字符串ExportToPEM(x509证书证书)
{
    StringBuilder的建设者=新的StringBuilder();

    builder.AppendLine(----- BEGIN CERTIFICATE -----);
    builder.AppendLine(Convert.ToBase64String(cert.Export(X509ContentType.Cert),Base64FormattingOptions.InsertLineBreaks));
    builder.AppendLine(----- END CERTIFICATE -----);

    返回builder.ToString();
}
 

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-----"

解决方案

Perhaps

/// <summary>
/// Export a certificate to a PEM format string
/// </summary>
/// <param name="cert">The certificate to export</param>
/// <returns>A PEM encoded string</returns>
public static string ExportToPEM(X509Certificate cert)
{
    StringBuilder builder = new StringBuilder();

    builder.AppendLine("-----BEGIN CERTIFICATE-----");
    builder.AppendLine(Convert.ToBase64String(cert.Export(X509ContentType.Cert), Base64FormattingOptions.InsertLineBreaks));
    builder.AppendLine("-----END CERTIFICATE-----");

    return builder.ToString();
}

这篇关于导出证书作为BASE-64连接codeD .CER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:03