There are two major encoding schemes for X.509 certificates and keys: PEM (Base64 ASCII), and DER (binary).
- DER (Distinguished Encoding Rules) is a data object encoding schema that can be used to encode certificate objects into binary files.
- PEM (Privacy Enhanced Mail) is an encrypted email encoding schema that can be borrowed to encode certificate DER files into text files.
We can’t always tell what kind of file we are working with just from looking at the filename. We may need to open it in a text editor and take a look for ourselves. We will share more about this later.
Understanding PEM
PEM (originally “Privacy Enhanced Mail”) is the most common format for X.509 certificates, CSRs, and cryptographic keys.
A PEM file is a text file containing one or more items in Base64 ASCII encoding, each with plain-text headers and footers (e.g. —–BEGIN CERTIFICATE—– and —–END CERTIFICATE—–).
Example of PEM
Here is an example of PEM format certificate.
# more certificate.pem
-----BEGIN CERTIFICATE-----
MIIDZTCCAk2gAwIBAgIUYWbWmYiNaGtLhEIhAcBtWO7NubkwDQYJKoZIhvcNAQEL
BQAwQjELMAkGA1UEBhMCVVMxFTATBgNVBAcMDERlZmF1bHQgQ2l0eTEcMBoGA1UE
.......
KtsNSEGDFdAFK7xh/L91l5eHSDSL0OApegcu2AhfUgSOnUBtUxa41yAtdb82Lvow
/deh1GDjgei5H7CKZwIruvN6rYWdfqpnaynAXS+AjRL145FwovHbJjjr/TYENCm0
ewRvyGJyUkJO
-----END CERTIFICATE-----
What is DER Format
DER (Distinguished Encoding Rules) is a binary encoding for X.509 certificates and private keys. Unlike PEM, DER-encoded files do not contain plain text statements such as —–BEGIN CERTIFICATE—–. DER files are most commonly seen in Java contexts.
Those certificate DER files are binary files, which can not be viewed with text editors. But they can be processed by application without any problems.
DER-encoded certificate files are supported by almost all applications.
Difference between PEM and DER
If the certificate is in text format, then it is in PEM format.
We can read the contents of a PEM certificate (cert.cer) using the ‘openssl’ command on Linux or Windows as follows:
- openssl x509 -in cert.cer -text
If the file content is binary, the certificate could be DER. To find out the format, run the following ‘openssl’ commands to open the certificate:
- openssl x509 -in cert.cer -inform DER -text
What is SSL Cer file
A file with .cer extension is a security certificate file that is used by secure websites to establish secure connections from web server to a browser.
If we open a secure website, we see a “lock” icon in the address bar. If we click on it, we can view the details of the installed certificate.
We need to figure out which format is using for this cer file based on the rules we shared above.
Convert Cer certificate to PEM
If our cer certificate is in PEM format, we can use cp cert.cer cert.pem to convert.
- openssl x509 -in cert.cer -out cert.pem
If our cer certificate is in DER format, we need to use the following command to convert to pem.
- openssl x509 -inform der -in cert.cer -out cert.pem