How do I convert a .cer certificate to .pem?

Solution 1:

Convert a DER file (.crt .cer .der) to PEM

openssl x509 -inform der -in certificate.cer -out certificate.pem

Source

Solution 2:

convert a .cer file in .pem

open a terminal and run the following command

openssl x509 -inform der -in certificate.cer -outform pem -out certificate.pem

Where certificate.cer is the source certificate file you want to convert and certificate.pem is the name of the converted certificate.


Solution 3:

When openssl is not available on your system you could alternatively convert certificates with the java keytool.

However you have to create a java keystore (JKS) first. The certificates can then be imported and exported in different formats.

keytool -genkey -alias test -keystore <key store file>
keytool -delete -alias test -keystore <key store file>

Converting from DER to PEM:

keytool -import -trustcacerts -alias test -file <der certificate file> -keystore test.keystore 
keytool -exportcert -alias test -file <pem certificate file> -rfc -keystore test.keystore

This blog post explains how to convert certificate formats in detail


Solution 4:

Answer

  • If your certificate is exported with DER encoding, then use the accepted answer:

    openssl x509 -inform der -in certificate.cer -out certificate.pem
    
  • If your certificate is exported with Base64 encoding, then rename the extension .cer to .pem. The file is already in .pem format.

How to tell that your .cer file is in .pem format?

See this stack-o answer, quoted here:

A .pem format certificate will most likely be ASCII-readable. It will have a line -----BEGIN CERTIFICATE-----, followed by base64-encoded data, followed by a line -----END CERTIFICATE-----. There may be other lines before or after.

For example, a .pem certificate (shortened):

-----BEGIN CERTIFICATE-----
MIIG6DCCBNCgAwIBAgITMgAAGCeh8HZoCVDcnwAAAAAYJzANBgkqhkiG9w0BAQsF
ADBAMRUwEwYKCZImiZPyLGQBGRYFbG9jYWwxEzARBgoJkiaJk/IsZAEZFgNkb3Ix
EjAQBgNVBAMTCURPUi1TVUJDQTAeFw0yMDA1MDExNTI0MTJaFw0yMjA1MDExNTI0
MTJaMBYxFDASBgNVBAMTC3dwZG9yd2VibDE2MIIBIjANBgkqhkiG9w0BAQEFAAOC
...
-----END CERTIFICATE-----