PEM and DER: the same bytes, two wrappings
Certificates, keys and certificate requests are ASN.1 structures. DER (Distinguished Encoding Rules, from ITU-T X.690) turns them into exact binary bytes, one encoding per value. DER is what gets hashed and signed.
PEM is a text wrapper around DER: the bytes in Base64, broken into 64-character lines, between a BEGIN and an END line that names what is inside. RFC 7468 describes the format. PEM exists because text survives email, config files, terminals and copy and paste, and binary often does not.
Converting between them never changes the underlying bytes. A certificate has the same fingerprint as PEM and as DER.
How the conversion works
Going from PEM to DER means removing the BEGIN and END lines, joining the Base64 lines and decoding them. Going from DER to PEM means encoding the bytes as Base64, wrapping at 64 characters and adding the lines with the right label.
-----BEGIN CERTIFICATE-----
MIIFDzCCA/egAwIBAgIJAK59SfdKnnZnMA0GCSqGSIb3DQEBCwUAMHsxCzAJBgNV
...
-----END CERTIFICATE-----
DER (hex): 30 82 05 0f 30 82 03 f7 a0 03 02 01 02 02 09 00 ae 7d ...Almost every DER certificate or key starts with the byte 30, an ASN.1 SEQUENCE, which is why PEM Base64 for these objects nearly always starts with MI. That is a useful hint when you are unsure what a blob is.
PEM labels and what they mean
The label is not decoration. Software reads it to decide how to parse the bytes, and rejects a mismatch.
| Label | Contents | Standard |
|---|---|---|
CERTIFICATE | An X.509 certificate | RFC 5280 |
CERTIFICATE REQUEST | A PKCS#10 signing request | RFC 2986 |
PUBLIC KEY | SubjectPublicKeyInfo: any public key with its algorithm | RFC 5280 |
PRIVATE KEY | PKCS#8: any private key with its algorithm | RFC 5958 |
ENCRYPTED PRIVATE KEY | PKCS#8, encrypted with a passphrase | RFC 5958 |
RSA PRIVATE KEY | PKCS#1: an RSA private key only | RFC 8017 |
RSA PUBLIC KEY | PKCS#1: an RSA public key only | RFC 8017 |
EC PRIVATE KEY | SEC1: an elliptic curve private key | RFC 5915 |
X509 CRL | A certificate revocation list | RFC 5280 |
Changing the label does not convert the contents. A PKCS#1 key relabelled PRIVATE KEY is still PKCS#1 inside and will fail to parse. Converting between key formats needs a tool that rewrites the structure, such as openssl pkey.
File extensions do not tell you the format
Extensions are conventions, and they overlap. The same .crt file can be PEM on one system and DER on another.
| Extension | Usually | But may be |
|---|---|---|
.pem | PEM, any contents | Several blocks, such as a key and a chain |
.crt, .cer | A certificate | PEM or DER |
.der | DER, any contents | - |
.key | A private key in PEM | PKCS#1, PKCS#8 or SEC1 |
.csr | A PEM signing request | DER on some systems |
.p12, .pfx | PKCS#12, a password-protected bundle of key and certificates | Not PEM or DER of a single object; this tool does not read it |
The quick test: if the file starts with -----BEGIN, it is PEM. If it is binary and starts with the byte 0x30, it is probably DER.
What this converter does and does not do
- PEM to DER: decodes the first PEM block, checks it parses as DER, and shows it as Base64 and hex, with a download of the
.derfile. - DER to PEM: reads a DER file from your disk, or DER pasted as Base64 or hex, and wraps it as PEM.
- It works out the label from the structure for certificates, requests, public keys and common private key formats, and lets you choose one when it cannot tell.
- Files are read in your browser. Nothing is uploaded.
- It does not convert between key formats such as PKCS#1 and PKCS#8, decrypt encrypted keys, or read PKCS#12 bundles.
How to convert
- Choose PEM to DER or DER to PEM.
- For PEM to DER, paste the PEM block. For DER to PEM, choose the file or paste its bytes as Base64 or hex.
- For DER to PEM, keep the label worked out from the data, or choose one if the tool cannot tell.
- Copy the result, or download it as a file.
# Certificate: PEM to DER and back
openssl x509 -in cert.pem -outform der -out cert.der
openssl x509 -in cert.der -inform der -out cert.pem
# Private key: PEM to DER (PKCS#8)
openssl pkey -in key.pem -outform der -out key.der
# Certificate request
openssl req -in req.pem -outform der -out req.derWhere you meet both formats in identity work
Identity systems mix the two formats freely, which is why conversion comes up so often.
Keycloak configuration
Keycloak reads PEM files for its own TLS certificate and key, and accepts PEM for imported realm keys. Truststores and keystores in Java formats are another option, but PEM is the simplest. If a CA sends a DER certificate, convert it to PEM first.
Bare Base64 in admin screens and metadata
Keycloak’s realm keys page and SAML metadata show certificates as Base64 without the BEGIN and END lines. That is DER in Base64. Paste it into DER to PEM with Base64 selected to get a proper PEM file.
Windows and Java tools
Windows certificate dialogs often export DER .cer files, and some Java tools expect DER. Convert PEM to DER for them, and back again for Linux servers and most other software.
JWKs and the x5c field
A JWK can carry a certificate chain in x5c: each certificate as standard Base64 DER, without line breaks or PEM lines. To read one, paste it into DER to PEM or straight into the certificate decoder.
Common mistakes
- Renaming a PEM label to make software accept a key. The structure must match the label.
- Concatenating PEM blocks in the wrong order. For a TLS chain, the server certificate comes first, then each intermediate.
- Leaving Windows line endings or stray spaces in PEM files that strict parsers reject.
- Treating Base64url as Base64. JWK values use Base64url;
x5cand PEM use standard Base64. - Assuming a
.crtfile is PEM because others were.
Security notes
Certificates and public keys are public. Private keys are not, in any format: DER is no more protected than PEM, it is only binary. If you convert a private key here, it stays in your browser, but treat the downloaded file with the same care as the original and delete copies you do not need.
An unencrypted private key file should be readable only by the service that uses it. On Linux that usually means mode 600 and the service’s own user.
Files with several PEM blocks
One PEM file can hold several blocks one after another. That is how certificate chains and bundles are stored, and it is something DER cannot do: a DER file holds exactly one object.
| File | Typical contents | Order |
|---|---|---|
| Chain or full chain | Server certificate and intermediates | Server certificate first, then each issuer |
| CA bundle | Trusted root certificates | Any order |
| Combined key and certificate | Private key and certificate | Depends on the software; some expect the key first |
This converter takes the first block of a PEM file. To convert a chain to DER, split it into one file per certificate and convert each. To check a chain’s order, paste it into the certificate decoder, which decodes every block and warns when the order is wrong.