簡體   English   中英

X509Certificate 中的公鑰和私鑰

[英]Public and private key in X509Certificate

我正在使用 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 文件?

使用 .NET 核心 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 可以從僅公鑰實例中工作)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM