簡體   English   中英

如何在Windows上生成.pfx和.cer文件

[英]How to generate a .pfx and .cer file on Windows

多年前,我參與了這個 .NET 標准項目,該項目使用私鑰(在密碼 protected.pfx 文件中)對一些內容進行簽名,並使用其相應的公鑰(in.cer 文件)來驗證簽名。

在代碼中,我使用 .NET 的X509Certificate2獲取相應的密鑰來簽署和驗證簽名。

現在,我不記得我在 Windows 上用什么工具來拯救我的生命。有人能給我指出正確的方向嗎? 查看openssl ,我可以生成公鑰/私鑰對,但不是X509Certificate2可以讀取的格式。

X.509 證書包含公鑰,但與公鑰不同。 X509Certificate2 可以讀取證書或包含證書的 PKCS7,或包含證書匹配的私鑰以及可能的任何相關“鏈”證書的 PFX/PKCS12,但它無法讀取(通常不能讀取)不存在) 公鑰本身,或私鑰本身。 要簽名,您需要第二種選擇(證書和私鑰)。

openssl程序可以通過三個基本步驟的不同組合,以多種方式創建 PFX/PKCS12:

  1. 在文件中生成實際的密鑰對(私鑰和公鑰)

  2. 為公鑰創建或獲取證書。 這本身可以通過更小的步驟完成,具體取決於您是否使用來自“真實”(外部)CA 的證書,例如 LetsEncrypt、GoDaddy、Digicert; 或來自私人或個人 CA,如 Windows AD/CS,或您自己; 或“自簽名”證書(不是來自任何 CA),它在大多數情況下不受信任,但可用於測試,或用於僅將證書用作公鑰容器格式而不用於信任的應用程序,這聽起來像是你的情況。

  3. 將來自 1 的私鑰和來自 2 的證書以及任何需要/需要的“鏈”證書組合到 PFX/pkcs12 中

對於自簽名證書(最簡單的情況,正如我所說,這聽起來很適合您), openssl可以在一次操作中執行 1 和 2

openssl req [-config conffile] -newkey $spec -keyout keyfile [-nodes] -x509 $otherfields -out certfile
# $spec can be rsa:$size or dsa:$paramfile or ecdsa:$paramfile
# $otherfields can include subject name (else it is prompted or set per the config)
# and/or -days $num -addext $type:$value -days $num -$hash; see the man page
# and/or many existing questions, or at least ask more specifically,
# but if you're not using the cert for trust probably none of these matter
#
# for all openssl commands except genpkey the flags may be given in any order
# but I show a sequence-based order that can be more easily compared 

或者可以使用幾種方法中的一種執行 1,然后使用一個或兩個部分執行 2

# 1: old methods
openssl genrsa $size [-$cipher] >keyfile
openssl gendsa $size >keyfile
openssl ecparam -curve $name -genkey [-noout] >keyfile
# or 1: new method
openssl genpkey {-algorithm rsa | -paramfile $dsa_or_ecdsa_file} [-pkeyopt $name:$value]... >keyfile
#
# then 2: single step
openssl req [-config conffile] -new -key keyfile -x509 $otherfields >certfile
# or 2: two steps
openssl req [-config conffile] -new -key keyfile $someotherfields >reqfile
openssl x509 -req <reqfile -signkey keyfile [-extfile $file [-extsect $sect]] -days $num -$hash >certfile

或者它可以將 1 與 2 的第一部分組合,然后是 2 的第二部分

openssl req [-config conffile] -newkey $spec -keyout keyfile [-nodes] $someotherfields >reqfile
openssl x509 -req <reqfile -signkey keyfile [-extfile $file [-extsect $sect]] -days $num $hash >certfile 

對於 CA 頒發的證書,第 1 步和第 2 步的第一部分 ( req -new[key] ) 單獨或組合是相同的,但第 2 步的第二部分 ( x509 -req -signkey ) 被替換為向 CA 提交 reqfile 並取回證書的過程; 這反過來又千差萬別,一個答案無法解決太多,所以我不會嘗試。

在上述任何openssl步驟 3 之后(總是且簡單):

openssl pkcs12 -export -in certfile -inkey keyfile -out pfxfile
# add -certfile chaincerts if and as needed

比較How to generate a self-signed SSL certificate using OpenSSL? 它顯示了過去十年這一領域的發展,包括更多考慮使證書特別受瀏覽器信任,但沒有像我上面那樣系統地列出選項,也不包括第 3 步(盡管許多其他 Qs 涵蓋了分別搜索“將 PEM 轉換為 PKCS12”或“將 PEM 轉換為 PFX”)。 如果您直接在 Windows(不是 WSL)上使用 OpenSSL,例如來自 ShiningLight,手冊頁可在線訪問https://www.openssl.org/docs/manpages.html

最后,作為替代方案,powershell 至少可以使用New-SelfSignedCertificate執行此操作; 是在七點,但現在無法檢查。 它生成與步驟 1 和 2 對應的密鑰對證書,但(兩者)在 Windows 存儲而不是文件中,因此您可以使用Export-PFXCertificate將它們獲取到 PFX 文件中(或者您可以使用 MMC/certmgr 中的導出任務.msc,但如果你已經很豪華了,為什么還要費心呢?)。

暫無
暫無

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

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