Ruby - 如何从ruby上的.pfx文件中提取public,rsa私钥和CA密钥

时间:2015-07-24 14:14:50

标签: ruby-on-rails ruby ssl

我有.pfx格式的证书,我需要使用ruby提取公共,私有和CA证书。

使用shell我可以这样做:

# Extract Public Key (ask for password)
openssl pkcs12 -in file.pfx -out file_public.pem -clcerts -nokeys

# Extract Certificate Authority Key (ask for password)
openssl pkcs12 -in file.pfx -out file_ca.pem -cacerts -nokeys

# Extract Private Key (ask for password)
openssl pkcs12 -in file.pfx -out file_private.pem -nocerts -nodes

# Extract RSA Private Key
openssl rsa -in file_private.pfx -out file_private_rsa.key

# Create Combo file with Public and RSA Private Keys
cat file_private_rsa.key file_public.pem > file_combo.pem

On this post DMKE展示了如何将密钥转换为.PFX,但如何反过来呢?

2 个答案:

答案 0 :(得分:5)

pkcs = OpenSSL::PKCS12.new(File.read("xyz.p12"), "password_for_xyz.p12")
key = OpenSSL::PKey::RSA.new(pkcs.key.to_pem)
cert = OpenSSL::X509::Certificate.new(pkcs.certificate.to_pem)

答案 1 :(得分:3)

pkcs = OpenSSL::PKCS12.new(File.read("file.pfx"), "password")

pkcs.key.to_pem

pkcs.certificate.to_pem