How to convert pfx to pem and via versa. The OpenSSL way

How to convert pfx to pem and via versa. The OpenSSL way

I am always asked how to handle the conversion of pfx, pem and key files.
Here are a few commands for you:

Convert PFX to combined PEM
To convert a PFX file into a combined PEM file – i.e. a PEM file that contains both the certificate and the private key – the following command can be used:

openssl pkcs12 -in filename.pfx -out combined.pem -nodes

Convert PFX into seperate PEM and KEY File
To convert a PFX file to a PEM and a KEY file, OpenSSL can be used with the following commands:

  1. Export the private key from the PFX file to a PEM file
openssl pkcs12 -in filename.pfx -nocerts -out key_enc.pem

2. Export only the certificate:

openssl pkcs12 -in filename.pfx -clcerts -nokeys -out certificate.pem

3. Remove password of key.pem file created in step 1

openssl rsa -in key_enc.pem -out my.key

At the end you have 3 new files:

  • key_enc.pem = encrypted private key file
  • certificate.pem = base64 coded certificate
  • my.key = base64 coded private key

Here you can download OpenSSL for Windows: Link