如何将.crt文件转换为C#中的.pem文件

发布于 2025-02-14 00:39:05 字数 137 浏览 0 评论 0原文

我有CRT文件,需要通过C#中的代码将其转换为PEM,该怎么办?

openssl中的命令:

openssl x509 -inform der -in file.crt -out file.pem

I have the crt file and I need to convert it to pem by code in C#, how can I do that?

command in openssl:

openssl x509 -inform der -in file.crt -out file.pem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

尸血腥色 2025-02-21 00:39:05

在.NET的现代版本上,您可以使用 pemencoding class:

.net 7:

string pem = PemEncoding.WriteString("CERTIFICATE", der);

.net 5:

string pem = new string(PemEncoding.Write("CERTIFICATE", der));

但是,在“石头衰老” .NET框架您必须编写相当简单的转换自己($” ------开始{label} ------ \ n {convert.tobase64string(der,base64form64form64form64formattingoptions.includenewlines)) } \ n -----结束{label} ----- \ n“是简短的表单)。

https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.certificateate.certificaterequest.c ReatesigningRequest?view = net-6.0#系统 - 安全性 - ryptography-x509certificates-certificateTificateRequest-CreateSigningRequestRequest :

private static string PemEncode(string label, byte[] der)
{
    StringBuilder builder = new StringBuilder();

    builder.Append("-----BEGIN ");
    builder.Append(label);
    builder.AppendLine("-----");

    string base64 = Convert.ToBase64String(pkcs10);

    int offset = 0;
    const int LineLength = 64;

    while (offset < base64.Length)
    {
        int lineEnd = Math.Min(offset + LineLength, base64.Length);
        builder.AppendLine(base64.Substring(offset, lineEnd - offset));
        offset = lineEnd;
     }

     builder.Append("-----END ");
     builder.Append(label);
     builder.AppendLine("-----");
     return builder.ToString();
}

On modern versions of .NET you can use the PemEncoding class:

.NET 7:

string pem = PemEncoding.WriteString("CERTIFICATE", der);

.NET 5:

string pem = new string(PemEncoding.Write("CERTIFICATE", der));

But, in "stone-aged" .NET Framework you have to write the fairly easy transform yourself ($"-----BEGIN {label}-----\n{Convert.ToBase64String(der, Base64FormattingOptions.IncludeNewLines)}\n-----END {label}-----\n" being the short form).

Borrowed/tweaked from the Remarks section of https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.certificaterequest.createsigningrequest?view=net-6.0#system-security-cryptography-x509certificates-certificaterequest-createsigningrequest:

private static string PemEncode(string label, byte[] der)
{
    StringBuilder builder = new StringBuilder();

    builder.Append("-----BEGIN ");
    builder.Append(label);
    builder.AppendLine("-----");

    string base64 = Convert.ToBase64String(pkcs10);

    int offset = 0;
    const int LineLength = 64;

    while (offset < base64.Length)
    {
        int lineEnd = Math.Min(offset + LineLength, base64.Length);
        builder.AppendLine(base64.Substring(offset, lineEnd - offset));
        offset = lineEnd;
     }

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