Convert PEM to DER
To convert a PEM certificate to DER, run `openssl x509 -in certificate.pem -outform der -out certificate.der`. DER is the raw binary encoding of the same data; PEM is that binary Base64-encoded and wrapped in `-----BEGIN CERTIFICATE-----` header and footer lines. Nothing is added or lost in either direction. Use a different subcommand for other object types: `openssl req` for a CSR and `openssl pkey` for a private key, because each parses a different ASN.1 structure.
Certificates #
openssl x509 -in certificate.pem -outform der -out certificate.der Replace these values
| Placeholder | What to put there |
|---|---|
certificate.pem | The Base64 input, starting with -----BEGIN CERTIFICATE-----. |
-outform der | Tells OpenSSL to write raw binary rather than PEM. |
certificate.der | The binary output file. |
The input format is detected automatically, so -inform pem is not needed here. Being explicit does no harm in a script where the input might vary.
Certificate signing requests #
A CSR is a different ASN.1 structure, so openssl x509 will not parse it. Use openssl req:
openssl req -in request.csr -outform der -out request.der Private keys #
openssl pkey handles every key algorithm, which makes it the one to remember. The older
openssl rsa and openssl ec subcommands still work but only for their own key type:
openssl pkey -in private.key -outform der -out private.der For a public key, add -pubin and -pubout:
openssl pkey -pubin -in public.pem -outform der -pubout -out public.der What each subcommand handles #
| PEM header in the file | Subcommand | Object |
|---|---|---|
BEGIN CERTIFICATE |
openssl x509 |
An X.509 certificate |
BEGIN CERTIFICATE REQUEST |
openssl req |
A CSR |
BEGIN PRIVATE KEY |
openssl pkey |
PKCS#8 private key, any algorithm |
BEGIN RSA PRIVATE KEY |
openssl rsa or openssl pkey |
PKCS#1 RSA private key |
BEGIN EC PRIVATE KEY |
openssl ec or openssl pkey |
SEC1 EC private key |
BEGIN PKCS7 |
openssl pkcs7 |
A PKCS#7 / P7B bundle |
BEGIN X509 CRL |
openssl crl |
A certificate revocation list |
If you are not sure which you have, read the first line:
head -n1 mystery.pem Verify the conversion #
The most convincing check is that the fingerprint is unchanged, because that hashes the actual certificate bytes:
openssl x509 -in certificate.pem -noout -fingerprint -sha256
openssl x509 -inform der -in certificate.der -noout -fingerprint -sha256 Expected output
sha256 Fingerprint=A1:B2:C3:... sha256 Fingerprint=A1:B2:C3:...
Going the other way #
See convert DER to PEM.
Frequently asked questions
- Does converting to DER change the certificate?
- No. PEM is DER that has been Base64-encoded and given header and footer lines. Converting between them re-encodes the same bytes, so the signature stays valid and the fingerprint is unchanged.
- Why does openssl x509 fail on my PEM file?
- Because the file is not a certificate.
openssl x509only parses certificates. If the file begins-----BEGIN CERTIFICATE REQUEST-----useopenssl req, and if it begins-----BEGIN PRIVATE KEY-----useopenssl pkey. - Can a DER file hold more than one certificate?
- Not usefully. DER has no delimiter between objects, so a concatenation of two DER certificates cannot be reliably split. Chains are why PKCS#7 and PKCS#12 exist. PEM can hold many certificates in one file precisely because its BEGIN and END lines act as delimiters.
- What extension should a DER file have?
- There is no enforced convention.
.derand.cerare both common, and Windows tooling often uses.cerfor DER while other platforms use.cerfor PEM. Never infer the encoding from the extension; check the first byte instead.
References
- openssl-x509(1) — -outform der
- RFC 7468: textual encodings of PKIX structures — defines the PEM armour and what may sit between the BEGIN and END lines