第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

從 Cert 和 Key 創(chuàng)建 X509Certificate2,無(wú)需創(chuàng)建 PFX 文件

從 Cert 和 Key 創(chuàng)建 X509Certificate2,無(wú)需創(chuàng)建 PFX 文件

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

2 回答

?
達(dá)令說(shuō)

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超6個(gè)贊

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


將私鑰附加到證書(shū)

從 .NET Framework 4.7.2 或 .NET Core 2.0 開(kāi)始,您可以組合證書(shū)和密鑰。它不修改證書(shū)對(duì)象,而是生成一個(gè)知道密鑰的新證書(shū)對(duì)象。


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,但是這有復(fù)雜的副作用并且不鼓勵(lì)。


加載私鑰

這個(gè)比較難,除非你已經(jīng)解決了。


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


PKCS#8 私鑰信息

從 .NET Core 3.0 開(kāi)始,您可以相對(duì)簡(jiǎn)單地執(zhí)行此操作:


using (RSA rsa = RSA.Create())

{

    rsa.ImportPkcs8PrivateKey(binaryEncoding, out _);

    // do stuff with the key now

}

(當(dāng)然,如果您有 PEM,則需要通過(guò)提取 BEGIN 和 END 定界符之間的內(nèi)容并運(yùn)行它Convert.FromBase64String以獲取來(lái)“去 PEM”它binaryEncoding)。


PKCS#8 加密私鑰信息

從 .NET Core 3.0 開(kāi)始,您可以相對(duì)簡(jiǎn)單地執(zhí)行此操作:


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 開(kāi)始,您可以相對(duì)簡(jiǎn)單地執(zhí)行此操作:


using (RSA rsa = RSA.Create())

{

    rsa.ImportRSAPrivateKey(binaryEncoding, out _);

    // do stuff with the key now

}

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


查看完整回答
反對(duì) 回復(fù) 2022-11-21
?
翻翻過(guò)去那場(chǎng)雪

TA貢獻(xiàn)2065條經(jīng)驗(yàn) 獲得超14個(gè)贊

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


...

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);

...


查看完整回答
反對(duì) 回復(fù) 2022-11-21
  • 2 回答
  • 0 關(guān)注
  • 352 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)