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

art*_*ega 5 ruby ssl ruby-on-rails

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

使用外壳,我可以这样:

# 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
Run Code Online (Sandbox Code Playgroud)

在这篇文章中, DMKE显示了如何将密钥转换为.PFX,但是如何反过来呢?

小智 6

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)
Run Code Online (Sandbox Code Playgroud)


Ste*_*hen 4

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

pkcs.key.to_pem

pkcs.certificate.to_pem
Run Code Online (Sandbox Code Playgroud)