使用OpenSSL将PKCS#12证书转换为PEM

时间:2013-02-28 19:30:22

标签: openssl command pkcs#12

我在Windows 7上安装了OpenSSL x64,我是从openssl-for-windows on Google Code下载的。我正试图跑:

openssl pkcs12 -export -in "path.p12" -out "newfile.pem" 

但是我收到了错误。

unable to load private key

如何使用OpenSSL从PKCS#12商店中提取PEM中的证书?

7 个答案:

答案 0 :(得分:418)

尝试:

openssl pkcs12 -in path.p12 -out newfile.crt.pem -clcerts -nokeys
openssl pkcs12 -in path.p12 -out newfile.key.pem -nocerts -nodes

之后你有:

  • newfile.crt.pem中的证书
  • newfile.key.pem中的私钥

要将证书和密钥放在同一文件中,请使用以下

openssl pkcs12 -in path.p12 -out newfile.pem

如果您需要直接从命令行输入PKCS#12密码(例如脚本),只需添加-passin pass:${PASSWORD}

openssl pkcs12 -in path.p12 -out newfile.crt.pem -clcerts -nokeys -passin 'pass:P@s5w0rD'

答案 1 :(得分:18)

您只需提供密码即可。您可以使用以下语法在同一命令行中执行此操作:

openssl pkcs12 -export -in "path.p12" -out "newfile.pem" -passin pass:[password]

然后,系统将提示您输入密码以加密输出文件中的私钥。如果要导出未加密的私钥(纯文本),请在上面的行中包含“nodes”选项:

openssl pkcs12 -export -in "path.p12" -out "newfile.pem" -passin pass:[password] -nodes

更多信息:http://www.openssl.org/docs/apps/pkcs12.html

答案 2 :(得分:14)

如果您可以使用Python,那么拥有pyopenssl模块会更容易。这是:

from OpenSSL import crypto

# May require "" for empty password depending on version

with open("push.p12", "rb") as file:
    p12 = crypto.load_pkcs12(file.read(), "my_passphrase")

# PEM formatted private key
print crypto.dump_privatekey(crypto.FILETYPE_PEM, p12.get_privatekey())

# PEM formatted certificate
print crypto.dump_certificate(crypto.FILETYPE_PEM, p12.get_certificate())

答案 3 :(得分:2)

我有一个PFX文件,需要为NGINX创建KEY文件,所以我这样做了:

openssl pkcs12 -in file.pfx -out file.key -nocerts -nodes

然后我必须编辑KEY文件并删除最多-----BEGIN PRIVATE KEY-----的所有内容。之后NGINX接受了KEY文件。

答案 4 :(得分:2)

有一个免费的开源 GUI 工具 KeyStore Explorer 可用于处理加密密钥容器。使用它,您可以将证书或私钥导出到单独的文件中或将容器转换为另一种格式(jks、pem、p12、pkcs12 等)

enter image description here

答案 5 :(得分:0)

#!/usr/bin/env python3
from optparse import Option
from OpenSSL import crypto
import os
import warnings
from getpass import getpass
warnings.filterwarnings("ignore", category=DeprecationWarning) 

def sanitize_path(path):
    return os.path.expandvars(os.path.expanduser(path))

def main(in_file, out_file, passphrase=None):
    if not passphrase:
        passphrase = getpass(prompt=("SSL Private Key Passphrase: "))
   
    in_file = sanitize_path(in_file)
    out_file = sanitize_path(out_file)
    
    with open(in_file, "rb") as input_file:
        p12 = crypto.load_pkcs12(input_file.read(), passphrase)
        pem = crypto.dump_privatekey(crypto.FILETYPE_PEM, p12.get_privatekey())
    with open(out_file, "w") as output_file:
        output_file.write(pem.decode('utf-8'))

if __name__ == '__main__':
    from optparse import OptionParser
    usage = "usage: %prog  input_file output_file [passphrase]"
    p = OptionParser(usage=usage)
    opt, args = p.parse_args()
    main(*args)

答案 6 :(得分:-1)

如果您需要没有任何密码的PEM文件,则可以使用this解决方案。

只需将私钥和证书复制并粘贴到同一文件中,然后另存为.pem。

文件如下所示:

-----BEGIN PRIVATE KEY-----
............................
............................
-----END PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
...........................
...........................
-----END CERTIFICATE-----

这是我发现将证书上传到Cisco设备以进行HTTPS的唯一方法。