亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 Cert 和 Key 創建 X509Certificate2,無需創建 PFX 文件

從 Cert 和 Key 創建 X509Certificate2,無需創建 PFX 文件

C#
小唯快跑啊 2022-11-21 21:33:25
過去,我一直通過導出帶密碼的 PFX 證書來制作安全的 TcpListener,但我想知道是否可以跳過此步驟。我沒有使用商業 SSL 證書,并且有一個用于頒發服務器證書的根 CA。在 C# 中托管 TcpListener 時,這些服務器證書需要額外的步驟(我猜是因為沒有使用 CSR)...但是如果我有私鑰和 OpenSSL 生成/使用的證書怎么辦。sslCertificate = new X509Certificate2("myExportedCert.pfx", "1234");所以這很好,但是我必須發出一個 openssl 命令來從證書和私鑰創建一個 pfx 文件,然后輸入一些密碼。然后將此密碼包含在我的代碼中。我想知道這一步是否非常必要。有沒有辦法從證書中組成 X509Certificate2,然后應用私鑰。構造函數參數僅允許證書部分,但加密失敗,因為沒有私鑰。另外,我不想依賴 OpenSSL 或 IIS 來導出 pfx.... 看起來很笨拙。理想情況下,我想:sslCertificate = new X509Certificate2("myCert.crt");sslCertificate.ApplyPrivateKey(keyBytes) // <= or "private.key" or whateversslStream.AuthenticateAsServer(sslCertificate, false, SslProtocols.Default, false);
查看完整描述

2 回答

?
達令說

TA貢獻1821條經驗 獲得超6個贊

您要求的有幾件不同的事情,而且容易程度各不相同。


將私鑰附加到證書

從 .NET Framework 4.7.2 或 .NET Core 2.0 開始,您可以組合證書和密鑰。它不修改證書對象,而是生成一個知道密鑰的新證書對象。


using (X509Certificate2 pubOnly = new X509Certificate2("myCert.crt"))

using (X509Certificate2 pubPrivEphemeral = pubOnly.CopyWithPrivateKey(privateKey))

{

    // Export as PFX and re-import if you want "normal PFX private key lifetime"

    // (this step is currently required for SslStream, but not for most other things

    // using certificates)

    return new X509Certificate2(pubPrivEphemeral.Export(X509ContentType.Pfx));

}

在 .NET Framework(但不是 .NET Core)上,如果您的私鑰是RSACryptoServiceProvider或者DSACryptoServiceProvider您可以使用cert.PrivateKey = key,但是這有復雜的副作用并且不鼓勵。


加載私鑰

這個比較難,除非你已經解決了。


大多數情況下,答案是在不使用 BouncyCastle 的情況下使用 c# 中的數字簽名,但如果您可以遷移到 .NET Core 3.0,事情就會變得容易得多。


PKCS#8 私鑰信息

從 .NET Core 3.0 開始,您可以相對簡單地執行此操作:


using (RSA rsa = RSA.Create())

{

    rsa.ImportPkcs8PrivateKey(binaryEncoding, out _);

    // do stuff with the key now

}

(當然,如果您有 PEM,則需要通過提取 BEGIN 和 END 定界符之間的內容并運行它Convert.FromBase64String以獲取來“去 PEM”它binaryEncoding)。


PKCS#8 加密私鑰信息

從 .NET Core 3.0 開始,您可以相對簡單地執行此操作:


using (RSA rsa = RSA.Create())

{

    rsa.ImportEncryptedPkcs8PrivateKey(password, binaryEncoding, out _);

    // do stuff with the key now

}

(如上所述,如果它是 PEM,您需要先“去 PEM”)。


PKCS#1 RSAPprivateKey

從 .NET Core 3.0 開始,您可以相對簡單地執行此操作:


using (RSA rsa = RSA.Create())

{

    rsa.ImportRSAPrivateKey(binaryEncoding, out _);

    // do stuff with the key now

}

(如果是 PEM,則為相同的“de-PEM”)。


查看完整回答
反對 回復 2022-11-21
?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

最后我這樣做了,效果很好:


...

if (!File.Exists(pfx)) {

    // Generate PFX

    string arguments = "openssl pkcs12 -export -in " + certPath + "" + certFile + ".crt -inkey " + certPath + "" + certFile + ".key -out " + certPath + "" + certFile + ".pfx -passout pass:" + pfxPassword;

    ProcessStartInfo opensslPsi = new ProcessStartInfo("sudo", arguments);

    opensslPsi.UseShellExecute = false;

    opensslPsi.RedirectStandardOutput = true;

    using (Process p = Process.Start(opensslPsi)) {

        p.WaitForExit();

    }

    // Set Permission

    ProcessStartInfo chmodPsi = new ProcessStartInfo("sudo", "chmod 644 " + certPath + "" + certFile + ".pfx");

    chmodPsi.UseShellExecute = false;

    chmodPsi.RedirectStandardOutput = true;

    using (Process p = Process.Start(chmodPsi)) {

        p.WaitForExit();

    }

}

sslCertificate = new X509Certificate2(pfx, pfxPassword);

...


查看完整回答
反對 回復 2022-11-21
  • 2 回答
  • 0 關注
  • 340 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號