【问题标题】:Public and private key in X509CertificateX509Certificate 中的公钥和私钥
【发布时间】:2019-09-25 13:09:42
【问题描述】:

我正在使用 C# System.Security.Cryptography 库为 apache 创建证书。我已经有一个 CA 证书,我尝试使用它来签署服务器证书。

我正在使用 CertificateRequest.Create 方法来创建证书。不幸的是,它没有提供我需要的 apache 私钥(.pem/.crt 和 .key)。如何保存证书并获取 apache 所需的两个文件?

OpenSSL 不是我的解决方案。

X509Certificate2 signedCert = request.Create(issuerCert, DateTimeOffset.Now, 
    DateTimeOffset.Now.AddYears(5), new byte[] { 1, 2, 3, 4 });

我可以通过这样做来保存签名证书的公钥:

File.WriteAllText(path + "cert.pem",
            "-----BEGIN CERTIFICATE-----\r\n"
            + Convert.ToBase64String(signedCert.Export(X509ContentType.Cert), Base64FormattingOptions.InsertLineBreaks)
            + "\r\n-----END CERTIFICATE-----");

如何获取私钥并将其保存为 .key 文件?

【问题讨论】:

    标签: c# apache x509certificate


    【解决方案1】:

    使用 .NET Core 3.0:

    File.WriteAllText(path + "cert.pem",
        "-----BEGIN PRIVATE KEY-----\r\n"
            + Convert.ToBase64String(
                key.ExportPkcs8PrivateKey(),
                Base64FormattingOptions.InsertLineBreaks)
            + "\r\n-----END PRIVATE KEY-----");
    

    key 是任何 AsymmetricAlgorithm 类型(您需要...有某个地方 - 可能已传递到 CertificateRequest 构造函数中,但由于您创建了链签名证书,CertificateRequest 本来可以从公钥工作- 仅实例)。

    【讨论】:

      猜你喜欢
      • 2013-07-09
      • 1970-01-01
      • 2011-07-19
      • 1970-01-01
      • 2016-03-27
      • 2016-02-03
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      相关资源
      最近更新 更多