如何将 pem 转换为 pfx 文件?

red*_*888 3 windows openssl certificate ssl-certificate pfx

这让我感到困惑:

Convert pfx to PEM:
openssl pkcs12 -in certificatename.pfx -out certificatename.pem
Run Code Online (Sandbox Code Playgroud)

做到这一点转储出一个单一的纯文本文件。

现在如何将此纯文本 pem 转换回 pfx?

我看到转换为 pfx 的唯一命令需要单独文件中的 cer 和私钥:

Convert CER and Private Key to PFX:    
openssl pkcs12 -export -in certificatename.cer -inkey privateKey.key -out certificatename.pfx -certfile  cacert.cer
Run Code Online (Sandbox Code Playgroud)

小智 7

您可以使用以下命令将 PEM(.pem、.crt、.cer)转换为 PFX:

openssl pkcs12 -export -out **<your_new_file_name>**.pfx -inkey **<private_key_of_your_existing_certificate>**.key -in **<your_existing_certificate_file>**.crt
Run Code Online (Sandbox Code Playgroud)

这对于所有上述文件来说都是非常通用的。

  • 但他询问如何将包含证书和私钥的单个 PEM 文件(不是证书和私钥的单独文件)转换为 PFX。 (4认同)

dav*_*085 6

Although I concur this is not really programming and arguably offtopic, numerous similar Qs about commandline tools (openssl, keytool, certutil etc) for (crypto) keys and certs are apparently accepted by the community (upvoted) -- but none I've seen directly addresses this point.

不同的选项openssl pkcs12 -export允许您在不同的文件中提供片段,但这不是必需的。如果您在一个 PEM 文件中确实有私钥和证书链,作为默认输出pkcs12 [not -export],您可以从该文件中读取所有内容:

 openssl pkcs12 -export -in file -out p12
 # or ONLY IF the privatekey is first in the file
 openssl pkcs12 -export <file -out p12
Run Code Online (Sandbox Code Playgroud)

只要您将私钥放在首位,您甚至可以“即时”组合这些部分:

 cat privkey.pem mycert.pem chain.pem | openssl pkcs12 -export -out p12
Run Code Online (Sandbox Code Playgroud)