Key File Formats: DER, PEM and PKCS #12 Explained (2024)

Public key cryptography (asymmetric cryptography) is the foundation of the Internet and it is used for a variety of purposes.

Public and private keys can be stored in several different types of files. Each of these types can have its own encoding. The overall format of a file can be quite complex. It is important, however, to understand the purpose of these formats, and how they’re used.

This document can be used as a primer for understanding these file/encoding formats.

The actual structure (objects and fields) of public/private keys, including X.509 certificates, is specified in various RFCs using the ASN.1 notation.

For example, an RSA private key contains the following fields:

RSAPrivateKey ::= SEQUENCE { version Version, modulus INTEGER, -- n publicExponent INTEGER, -- e privateExponent INTEGER, -- d prime1 INTEGER, -- p prime2 INTEGER, -- q exponent1 INTEGER, -- d mod (p-1) exponent2 INTEGER, -- d mod (q-1) coefficient INTEGER, -- (inverse of q) mod p otherPrimeInfos OtherPrimeInfos OPTIONAL}

The most widely used format is X.509 and it’s full syntax is defined by the RFC5280. X.509 provides the support for the “chain of trust” to verify a public key, as well as various extensions, primarily concerning the key’s usage. RFC5280 also documents formats for CSR, CLR, etc.

PKCS #8 specification defines the structure of private keys (PKCS stands for Public-Key Cryptography Standards).

These specifications only stipulate the structure (fields and objects), we still need to decide how to “encode” these fields when we want to save them on disk.

Security considerations aside, it would be nice if everything was stored in some sort of a structured format that we’re all used to, such as JSON or YAML, but this not the case for the majority of crypto formats, with the exception of JWK as explained later.

“DER” encoding, which is a variation of a more general X.690 encoding, is the de-facto standard for encoding ASN.1 structures. DER is a purely binary format, DER-encoded content can’t be inspected with a text editor. The basic idea of DER is to represent each field as a type+length+value triplet.

DER format is the one used for sending certificates over the wire — e.g., when a server presents its certificate to the client as part of the TLS handshake, the certificate bits are serialized using DER (theoretically, a certificate can be stored in a completely different format on disk).

DER can be further encoded using Base64, which creates an ASCII text that could be copied-pasted, sent over email and so forth. Base64-encoded DER files are usually saved with the “pem” or “crt” extension. PEM files also contain a header and footer to give an idea of the content of the file (sort of poor man metadata). X.509 files use “—–BEGIN CERTIFICATE—–“/”—–END CERTIFICATE—–“, but various other headers/footers can be used as well, such as “—–BEGIN PUBLIC KEY—–“/”—–END PUBLIC KEY—–“. In this last case, the file contains only the public key information and none of the additional X.509 fields.

Private keys can be saved in the PEM format as well, “—–BEGIN PRIVATE KEY—–“/”—–END PRIVATE KEY—–” is used to denote such files. All these headers as well the detailed PEM-encoding rules are documented in this specification.

ASN.1/DER/PEM is mostly used for TLS implementation and whenever X.509 certificates are involved.

Other public-key cryptography implementations can use different formats. Irrespective of the format, the underlying implementation and algorithms remain the same. E.g., an RSA public key must contain modulus and exponent fields, the only question is how to pass these fields around or how to store them in a file.

OAuth2 and JWT use JSON to exchange public/private keys.
Here is an example of a public key in the JWK format used by OAuth2 authorization servers:

{ "kty": "RSA", "alg": "RS256", "kid": "oViynWdKmd9m43BihjrQH9bHlp22fto0Nu-zwaBzUAs", "use": "sig", "e": "AQAB", "n": "q8BD_0q9JQRnpZ5vLnBMEA03nUWmxE56nGvKFY8K0fOAHojFPExI0Il67NEv6TCPZaXiifT5p9N9DIQl-JaWNaQmDCvd5Hbeugqn05QGJ14E_ghTXA6iXsONnavri5qlgc5rPmAS9zkm755ID7mHnuskEMXJy929LlxFKHzDRTkN8Lf1hSVXG8Mdy0f1QW-01VNRE8ZW0Ar5vLLuGrDb8bg9fCZXA6CK7oVJHXzo6ajIgzpa86kpdvWOhhtYPCL9P9wNjt4kfX3LBb6_sl9s8lI0C0OWtoMyNtAbE4wFc08o0ZsW1UGQin5eFFBuH_zbaPwc7wvYw40bBw35U_V9Sw"}

SSH keys (the ones that usually start with “ssh-rsa”) use their own form of encoding documented in the RFC4253 — each component of a key is stored as length+data and the entire key is Base64 encoded. Note that this is different from DER encoding, which is type+length+value. This post provides all the details.

It would obviously be a good idea to somehow encrypt and password-protect private keys. There are several formats that can be used for this purpose.

PKCS #8 files (usually encoded as PEM) files can be encrypted with a passphrase and various cyphers, in which case these file start with “—–BEGIN ENCRYPTED PRIVATE KEY—–” header.

The most widely used format for storing keys and certificates in an encrypted format is PKCS #12, defined by RFC7292. It can be used for storing certificates, public/private keys, and even arbitrary passwords. These files have “p12” or “pfx” extension (“pfx” is a PKCS #12 predecessor). Modern versions of Java use PKCS #12 as the default keystore format, although these files can still have the legacy “jks” extension.

Internally, PKCS #12 uses DER/ASN.1 structures for storing certificates and private keys.

For more recommendations on how to properly secure PKCS #12/JKS files, please refer to our keystore best practices post and the e-book.

Key File Formats: DER, PEM and PKCS #12 Explained (2024)

FAQs

What is the difference between PKCs12 and PEM? ›

As of Java 9, PKCS #12 is the default keystore format. A simpler, alternative format to PKCS #12 is PEM which just lists the certificates and possibly private keys as Base 64 strings in a text file.

What is PEM file and key file? ›

PEM, initially invented to make e-mail secure, is now an Internet security standard. HPE Service Manager uses OpenSSL libraries to encrypt and decrypt SOAP messages over HTTP and requires certificates and keys in PEM format. The typical PEM files are: key. pem contains the private encryption key.

What key formats are PEM? ›

Privacy-Enhanced Mail (PEM) is a de facto file format for storing and sending cryptographic keys, certificates, and other data, based on a set of 1993 IETF standards defining “privacy-enhanced mail.” So any private key/public key/certificate, etc that we generate can be stored in a PEM file format (.

What is PEM and DER? ›

Note: The PEM format is the most common format used for certificates. Extensions used for PEM certificates are cer, crt, and pem. They are Base64 encoded ASCII files. The DER format is the binary form of the certificate.

How to convert PEM to PKCS12? ›

How do I convert my PEM format certificate to PKCS12?
  1. Open a command prompt and navigate to the directory that contains the cert_key_pem. txt file.
  2. Execute the following OpenSSL command to create a PKCS12 (.p12) file: ...
  3. After you enter the command, you'll be prompted to enter an Export Password. ...
  4. A file called cert_key.

Is PEM a SSL certificate? ›

PEM files help store SSL certificates and their associated private key. Likewise, you can store multiple credentials with an entire SSL chain. It's the file used in servers like Nginx and Apache for encrypting HTTPS.

Why do we need PEM files? ›

pem files (concatenated certificate container files), are frequently required for certificate installations when multiple certificates are being imported as one file. This article contains multiple sets of instructions that walk through various . pem file creation scenarios for certificate installation.

What is the difference between a certificate file and a key file? ›

Essentially, it's a certificate that the server serves to the connecting users to prove that they are what they claim to be. On the other hand, the private key file is a file containing the private part of the public-private key pair.

How do I convert a key to PEM format? ›

Convert your user key and certificate files to PEM format.
  1. Get the . key. pem file. For example: openssl pkcs12 -nocerts -in my.p12 -out .key.pem.
  2. Get the . cert. pem file. ...
  3. Remove the passphrase from the key. For example: openssl rsa -in .key.pem -out key_nopass.pem mv key_nopass.pem .key.pem.

How do I know if a file is in PEM format? ›

Run the following commands to check if your files are already in the required PEM format:
  1. Check to see if your Private Key is in PEM format: openssl rsa -inform PEM -in /tmp/ssl.key.
  2. Check to see if your Main/Server Certificate is in PEM format: openssl x509 -inform PEM -in /tmp/certificate.crt.
Jul 26, 2023

Do PEM files have passwords? ›

Inspect PEM file

If your key section reports BEGIN ENCRYPTED PRIVATE KEY instead, this means the key is encrypted and you must use the password to open or inspect the key. You may have specified the password when requesting the key, or the password could be assigned by your site's network managers.

How to create a PEM key? ›

Creating a PEM file from a private key and Base64 encoded certificates
  1. Concatenate the certificate and private key files. In this example, the Linux/UNIX cat command is used. ...
  2. Confirm that the PEM file has the required key and certificate information as described in PEM file format.

Does PKCS12 contain a private key? ›

A PKCS#12 or . pfx file is a file which contains both private key and X. 509 certificate, ready to be installed by the customer into servers such as IIS, Apache Tomcat or Exchange. Certificate Signing Request (CSR) generation remains one of the consistent problem areas faced by customers wishing to secure their server.

What does a PEM file look like? ›

A PEM encoded file includes Base64 data. The private key is prefixed with a "-----BEGIN PRIVATE KEY-----" line and postfixed with an "-----END PRIVATE KEY-----". Certificates are prefixed with a "-----BEGIN CERTIFICATE-----" line and postfixed with an "-----END CERTIFICATE-----" line.

What is the file extension for PKCS12? ›

PKCS#12 (also known as PKCS12 or PFX) is a binary format for storing a certificate chain and private key in a single, encryptable file. PKCS#12 files are commonly used to import and export certificates and private keys on Windows and macOS computers, and usually have the filename extensions . p12 or . pfx .

Is PKCS7 the same as PEM? ›

PKCS #7 files may be stored both as raw DER format or as PEM format. PEM format is the same as DER format but wrapped inside Base64 encoding and sandwiched in between ‑‑‑‑‑BEGIN PKCS7‑‑‑‑‑ and ‑‑‑‑‑END PKCS7‑‑‑‑‑ . Windows uses the . p7b file name extension for both these encodings.

What is a PKCS12 certificate? ›

A PKCS#12 or . pfx file is a file which contains both private key and X. 509 certificate, ready to be installed by the customer into servers such as IIS, Apache Tomcat or Exchange. Certificate Signing Request (CSR) generation remains one of the consistent problem areas faced by customers wishing to secure their server.

Is pkcs8 same as PEM? ›

PKCS#8 is a format for cryptographic private keys, often containing pairs of private and public keys. PKCS#8 private keys can also be serialized in an ASN. 1-based binary format. The PEM text encoding is a Base64 representation of this format.

What is the difference between x509 and PEM file? ›

509 is a series of standards, while PEM is just X. 509 object representation in a file (encoding). Literally any data can be represented in PEM format. Anything that can be converted to a byte array (and anything can be, because RAM is a very large byte array) can be represented in PEM format.

Top Articles
Latest Posts
Article information

Author: Mr. See Jast

Last Updated:

Views: 6061

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Mr. See Jast

Birthday: 1999-07-30

Address: 8409 Megan Mountain, New Mathew, MT 44997-8193

Phone: +5023589614038

Job: Chief Executive

Hobby: Leather crafting, Flag Football, Candle making, Flying, Poi, Gunsmithing, Swimming

Introduction: My name is Mr. See Jast, I am a open, jolly, gorgeous, courageous, inexpensive, friendly, homely person who loves writing and wants to share my knowledge and understanding with you.