How-to guide · Convert file formats
How to convert between DER and PEM
In one sentence
You will have identified whether a file is DER or PEM and converted certificates, keys, CSRs, CRLs and bundles between the two, with proof that nothing changed.
- Platforms
- Linux
- macOS
- Windows
- Java
- Level
- Beginner · 8 steps
On this page
- Before you start
- Steps
- 1. Find out which one you have
- 2. Convert a certificate
- 3. Convert keys, CSRs and CRLs
- 4. Convert a PKCS #7 bundle (.p7b)
- 5. Convert an issuer certificate you downloaded
- 6. Decode a Base64 block without BEGIN and END lines
- 7. On Windows, with certutil
- 8. In Java, with keytool
- Check it worked
- If something goes wrong
- Why this works
- Go deeper
Before you start
- OpenSSL (Linux, macOS with Homebrew, or Git Bash on Windows). On Windows without Git Bash,
certutilhandles certificates (step 7); with a JDK,keytooldoes too (step 8). - The file to convert. The extension (
.cer,.crt,.der) does not tell you the format; the content does.
DER is the binary encoding of a certificate or key. PEM is the same bytes in Base64, between -----BEGIN …----- and -----END …----- lines. Windows, Java and many appliances often hand you DER; Linux servers usually want PEM.
Steps
1. Find out which one you have
head -1 www.cer
head -c 4 www.der | od -An -tx1
-----BEGIN CERTIFICATE-----
30 82 03 39
- Text that starts with
-----BEGINis PEM, and the word afterBEGINsays what it holds. - Binary that starts with the byte
30is DER: every certificate, key, CSR and CRL is an ASN.1SEQUENCE, and that byte opens it.
On Linux and in Git Bash, file -b often names it: Certificate, Version=3 for a DER certificate, PEM certificate for PEM. Do not rely on file for keys; it can mislabel them.
2. Convert a certificate
# DER to PEM
openssl x509 -inform DER -in www.der -out www.pem
# PEM to DER
openssl x509 -in www.pem -outform DER -out www.der
OpenSSL 3 detects DER input on its own for openssl x509, but -inform DER is harmless and needed on OpenSSL 1.1.1. A DER file holds exactly one object; if your PEM file contains several certificates, openssl x509 converts only the first.
3. Convert keys, CSRs and CRLs
Each object type has its own OpenSSL command, and the options are the same:
| Object | DER to PEM | PEM to DER |
|---|---|---|
| Private or public key | openssl pkey -inform DER -in key.der -out key.pem |
openssl pkey -in key.pem -outform DER -out key.der |
| CSR | openssl req -inform DER -in req.der -out req.pem |
openssl req -in req.pem -outform DER -out req.der |
| CRL | openssl crl -inform DER -in ca.crl -out ca-crl.pem |
openssl crl -in ca-crl.pem -outform DER -out ca.crl |
For a public key, add -pubin to the pkey commands. A DER private key is still a secret: converting it changes nothing about how carefully you must store it. CRLs downloaded from a CA are usually DER; after conversion the PEM file starts with -----BEGIN X509 CRL-----.
4. Convert a PKCS #7 bundle (.p7b)
A .p7b file carries several certificates, often as DER. Unpack it into one PEM file:
openssl pkcs7 -inform DER -in chain.p7b -print_certs -out chain.pem
Our test bundle held two certificates, and chain.pem then had two BEGIN CERTIFICATE blocks. To go the other way, openssl crl2pkcs7 -nocrl -certfile chain.pem -outform DER -out chain.p7b.
5. Convert an issuer certificate you downloaded
The “CA Issuers” address in a certificate’s Authority Information Access extension usually serves DER. For example, letsencrypt.org’s certificate points to its issuer:
openssl s_client -connect letsencrypt.org:443 -servername letsencrypt.org </dev/null 2>/dev/null \
| openssl x509 -noout -ext authorityInfoAccess
curl -sS -o ye2.der http://ye2.i.lencr.org/
openssl x509 -inform DER -in ye2.der -out ye2.pem
openssl x509 -in ye2.pem -noout -subject -issuer
Authority Information Access:
CA Issuers - URI:http://ye2.i.lencr.org/
subject=C=US, O=Let's Encrypt, CN=YE2
issuer=C=US, O=ISRG, CN=Root YE
The server sent the file as application/pkix-cert, which is DER. Plain HTTP is fine here: the certificate is verified by its signature, not by how it travelled. How to build the full chain file shows where such a file goes next.
6. Decode a Base64 block without BEGIN and END lines
Portals and APIs sometimes show a certificate as bare Base64. Decode it straight to DER:
openssl base64 -d -A -in body.txt -out cert.der # Base64 on one long line
openssl base64 -d -in body.txt -out cert.der # Base64 wrapped in lines of 64 characters
Choose the right one: on a single long line without -A, OpenSSL can write an empty or cut-off file without printing any error (in our tests, a 1,060-character line gave 0 or 25 bytes). Then convert cert.der as in step 2.
7. On Windows, with certutil
certutil -encode www.der www.pem
certutil -decode www.pem www.der
Input Length = 829
Output Length = 1200
CertUtil: -encode command completed successfully.
Input Length = 1200
Output Length = 829
CertUtil: -decode command completed successfully.
-encode writes a file with -----BEGIN CERTIFICATE----- lines, so OpenSSL and Linux servers read it as PEM. -decode reverses it. Both refuse to overwrite an existing output file; add -f to force it. In the Windows certificate export wizard, “DER encoded binary X.509 (.CER)” gives DER and “Base-64 encoded X.509 (.CER)” gives PEM, with the same extension.
8. In Java, with keytool
keytool -exportcert -alias shop -keystore shop.p12 -file shop.cer # DER
keytool -exportcert -rfc -alias shop -keystore shop.p12 -file shop.pem # PEM
keytool -importcert and keytool -printcert accept both formats.
Check it worked
A conversion must not change the certificate. Its SHA-256 fingerprint is computed over the DER bytes, so it is the same for both files, and equal to a plain SHA-256 of the DER file:
openssl x509 -in www.pem -noout -fingerprint -sha256
openssl x509 -inform DER -in www.der -noout -fingerprint -sha256
sha256sum www.der
sha256 Fingerprint=67:E1:E5:8B:37:05:E5:60:D5:61:B0:68:B2:A9:29:BF:87:15:FB:B1:22:F6:C8:01:8E:3A:22:2E:59:34:0C:7D
sha256 Fingerprint=67:E1:E5:8B:37:05:E5:60:D5:61:B0:68:B2:A9:29:BF:87:15:FB:B1:22:F6:C8:01:8E:3A:22:2E:59:34:0C:7D
67e1e58b3705e560d561b068b2a929bf8715fbb122f6c8018e3a222e59340c7d *www.der
You can also paste the PEM certificate into the decoder:
If something goes wrong
Could not read certificate from …(OpenSSL 3.0),Could not find certificate from …(3.2) orunable to load certificate(1.1.1): the file is not a certificate in the format you named. Check step 1: it may be a key, a CSR, a.p7b(step 4) or a PFX (How to convert PFX to PEM).- Only one certificate came out:
openssl x509reads one object. Use a.p7b(step 4) or keep the bundle in PEM. - An empty or truncated file after
openssl base64 -d: the input is on one line; add-A(step 6). CertUtil: -encode command FAILED: 0x80070050 (WIN32: 80 ERROR_FILE_EXISTS): the output file already exists. Add-f, or choose a new name.- A server still rejects the PEM file: check that it starts with the right
BEGINline and contains what the server expects, such as the full chain: How to build the full chain file.
Why this works
Certificates, keys, CSRs and CRLs are ASN.1 structures, and DER is their one canonical binary encoding. PEM, defined in RFC 7468, is those exact DER bytes in Base64 with a label. Converting therefore changes only the wrapping. A signature covers the DER bytes of the signed data, so it stays valid in either form, and the fingerprint stays the same. The file formats appendix compares DER, PEM, PKCS #7, PKCS #12 and the Java keystores, and Anatomy of an X.509 certificate shows the ASN.1 structure inside.
Go deeper
- RFC 7468: PEM encodings and the labels after
BEGIN. - OpenSSL format options:
-inform,-outformand how input formats are detected. - OpenSSL
x509,pkcs7andcrl2pkcs7manuals. - RFC 5280, section 4.2.2.1: the Authority Information Access extension and its “CA Issuers” method.
- Microsoft Learn: certutil:
-encodeand-decode. - Oracle: keytool:
-exportcertand-rfc.
Early edition. Checked against the standards; an independent expert review is in progress. Found a mistake? Tell us.