首页 文章

将pfx转换为pem

提问于
浏览
-1

我知道有很多命令(openssl)将pfx导出到pem但是我需要一个不同的东西:我需要将公钥导出到pem文件,将私钥导出到另一个文件 . 大多数命令和站点(某些站点将pfx格式转换为我需要的任何人)只会生成一个* .pem文件 .

谢谢 .

1 回答

  • 0

    Meta:这不是一个编程或开发问题,很可能会作为offtopic关闭 .

    如果你想要私钥和证书(其中包含公钥但不是公钥),这是在其他Stack中的几个问题,它是ontopic,至少包括:
    https://security.stackexchange.com/questions/3779/how-can-i-export-my-private-key-from-a-java-keytool-keystore/
    https://serverfault.com/questions/715827/how-to-generate-key-and-crt-file-from-jks-file-for-httpd-apache-server
    https://serverfault.com/questions/806141/is-the-alert-ssl3-read-bytessslv3-alert-bad-certificate-indicating-that-the-s(披露:我的回答)

    或者,由于PEM文件是结构化文本,因此您可以通过任意数量的文本处理程序(如awk)解析单个 pkcs12 命令的输出:

    openssl pkcs12 <p12 | awk '/-BEGIN ENC/,-END ENC/{print >"privkey"} \
       /-BEGIN CERT/,/-END CERT/{if(!n)print >"cert"} /-END CERT/{n++}'
     # for unencrypted privatekey add -nodes and select BEGIN/END PRIV
    

    如果您真的需要publickey,可以使用privatekey或证书以algorithm-generic X.509 SubjectPublicKeyInfo形式创建它:

    # from the certificate
     openssl x509 <certfile -noout -pubkey >pubkey
     openssl pkcs12 <p12file -nokeys -clcerts | openssl x509 -noout -pubkey >pubkey
    
     # from the privatekey
     openssl pkey <privkey -pubout >pubkey
     openssl pkcs12 <p12file -nocerts -nodes | openssl pkey -pubout >pubkey
    

    某些OpenSSL函数(将其称为PUBKEY以区分几种特定于算法的PublicKey)和低级Java(称之为 X509EncodedKeySpec )使用此格式,而几乎没有其他任何东西 . 使用裸公钥的注意系统通常是不安全的;这正是大多数系统将公钥嵌入证书的原因 .

    如果密钥是RSA并且您想要特定于算法(PKCS1 RSAPublicKey)格式,则在OpenSSL 1.1.0中(并且可能是up)然后使用:

    # from the SPKI publickey as above
     openssl rsa <RSApub_spki [-inform DER] -pubin -RSAPublicKey_out [-outform DER] >RSApub_pkcs1 
     # from the privatekey 
     openssl rsa <RSAprivate [-inform DER] -RSAPublicKey_out [-outform DER] >RSApub_pkcs1
    

    一些OpenSSL函数和AFAIK很少使用它;见上面的警告 .

相关问题