将证书导出为 BASE-64 编码的 .cer

发布于 2024-10-13 00:44:12 字数 614 浏览 3 评论 0原文

我正在尝试导出没有私钥的证书作为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

兮子 2024-10-20 00:44:12

也许

/// <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();
}

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();
}
衣神在巴黎 2024-10-20 00:44:12

试试这个:

X509Certificate2 cerifikata = new X509Certificate2("C://certificate.pfx");
File.WriteAllBytes("D://Test.cer",cerifikata.Export(X509ContentType.Cert));

try this:

X509Certificate2 cerifikata = new X509Certificate2("C://certificate.pfx");
File.WriteAllBytes("D://Test.cer",cerifikata.Export(X509ContentType.Cert));
固执像三岁 2024-10-20 00:44:12

对于那些在 .NET Core 中实现类似功能的人,这里是基于 tyranid 所做的代码。 .NET Core 中不存在 Base64FormattingOptions.InsertLineBreaks,因此我必须实现自己的方式来进行换行。

    // Certificates content has 64 characters per lines
    private const int MaxCharactersPerLine = 64;

    /// <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(this X509Certificate2 cert)
    {
        var builder = new StringBuilder();
        var certContentBase64 = Convert.ToBase64String(cert.Export(X509ContentType.Cert));
        // Calculates the max number of lines this certificate will take.
        var certMaxNbrLines = Math.Ceiling((double)certContentBase64.Length / MaxCharactersPerLine);

        builder.AppendLine("-----BEGIN CERTIFICATE-----");
        for (var index = 0; index < certMaxNbrLines; index++)
        {
            var maxSubstringLength = index * MaxCharactersPerLine + MaxCharactersPerLine > certContentBase64.Length
                ? certContentBase64.Length - index * MaxCharactersPerLine
                : MaxCharactersPerLine;
            builder.AppendLine(certContentBase64.Substring(index * MaxCharactersPerLine, maxSubstringLength));
        }
        builder.AppendLine("-----END CERTIFICATE-----");

        return builder.ToString();
    }

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.

    // Certificates content has 64 characters per lines
    private const int MaxCharactersPerLine = 64;

    /// <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(this X509Certificate2 cert)
    {
        var builder = new StringBuilder();
        var certContentBase64 = Convert.ToBase64String(cert.Export(X509ContentType.Cert));
        // Calculates the max number of lines this certificate will take.
        var certMaxNbrLines = Math.Ceiling((double)certContentBase64.Length / MaxCharactersPerLine);

        builder.AppendLine("-----BEGIN CERTIFICATE-----");
        for (var index = 0; index < certMaxNbrLines; index++)
        {
            var maxSubstringLength = index * MaxCharactersPerLine + MaxCharactersPerLine > certContentBase64.Length
                ? certContentBase64.Length - index * MaxCharactersPerLine
                : MaxCharactersPerLine;
            builder.AppendLine(certContentBase64.Substring(index * MaxCharactersPerLine, maxSubstringLength));
        }
        builder.AppendLine("-----END CERTIFICATE-----");

        return builder.ToString();
    }
二智少女 2024-10-20 00:44:12

//然而,缺少“-----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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文