DEV Community

Cover image for PEM vs. PKCS#12 (P12/PFX): Understanding the Difference Between Certificate Formats
Pascal Reitermann
Pascal Reitermann

Posted on

PEM vs. PKCS#12 (P12/PFX): Understanding the Difference Between Certificate Formats

When working with TLS, HTTPS, OAuth, API gateways, or modern messaging platforms, you’ll eventually encounter two very common certificate formats: PEM and PKCS#12 (P12/PFX).

They both deal with keys and certificates – but they are not the same thing, and using the wrong one can cause frustrating errors.

This post explains what each format is, what problems they solve, how they differ, and when to use which.

What problem do certificate files solve?

Digital certificates are used to:

  • Prove identity (who am I?)
  • Enable encryption
  • Establish trust between systems

To do that, we need:

  • A private key
  • A public certificate
  • Sometimes: intermediate + root certificates
  • Sometimes: credentials to protect them

Different tools and ecosystems expect these packaged in different ways — that’s where PEM and P12 come in.

What is a PEM Certificate?

PEM stands for Privacy Enhanced Mail, but today it’s a general-purpose certificate format used almost everywhere.

PEM files are:

  • Text-based (Base64 encoded)
  • Human-readable
  • Easy to copy, paste, and version
  • Very common on Linux, cloud environments, and DevOps tooling

They look like this:

-----BEGIN CERTIFICATE-----
MIID2zCCAsOgAwIBAgIUWlK...
-----END CERTIFICATE-----
Enter fullscreen mode Exit fullscreen mode

PEM files can contain:

  • Public certificates (.crt\, .cer\, .pem\)
  • Private keys (.key\, sometimes .pem\)
  • Certificate chains (multiple certs in one file)

Advantages of PEM

  • Easy to inspect
  • Git- and diff-friendly
  • Supported by OpenSSL, Kubernetes, NGINX, Apache, Keycloak, Solace, Prometheus, etc.
  • Flexible: multiple certificates can be stored together

Disadvantages

  • Private keys may be unencrypted
  • Not ideal for securely bundling multiple credentials
  • Lacks built-in password protection

What is a PKCS#12 / P12 / PFX File?

PKCS#12 (also known as P12 or PFX) is a binary, standardized container format defined in Public Key Cryptography Standards.

A P12 file is basically a secure bundle that can contain:

  • Private key
  • Public certificate
  • Certificate chain (intermediate + root)
  • Optional trust store
  • Protected with a password

This is why Windows, macOS Keychain, browsers, enterprise IAM tools, and hardware security devices love it.

Advantages of P12

  • Designed for secure transport of identities
  • Password protected
  • One file contains everything
  • Great for importing identities into:
    • Browsers
    • Windows Certificate Store
    • Java KeyStores
    • Enterprise systems

Disadvantages

  • Binary → harder to inspect
  • Not Git-friendly
  • Requires tooling to open
  • Slightly heavier operationally

PEM vs P12 — Quick Comparison

Feature PEM PKCS#12 (P12/PFX)
Encoding Text (Base64) Binary
Typical OS Linux / Unix Windows + enterprise tools
Can store private key Yes Yes
Can store cert chain Yes Yes
Stores multiple items Sometimes Yes (by design)
Password protection No (unless manually encrypted) Yes
Human readable Yes No
Typical Extensions .pem\, .crt\, .cer\, .key\ .p12\, .pfx\

Converting Between PEM and P12

Since tools often disagree on formats, conversion is very common.

Convert PEM → P12

Combine certificate + private key into a secure bundle:

openssl pkcs12 -export \
  -in cert.pem \
  -inkey private.key \
  -certfile chain.pem \
  -out identity.p12
Enter fullscreen mode Exit fullscreen mode

You’ll be asked for a password — this protects the .p12 file.

Convert P12 → PEM

Extract the private key:

openssl pkcs12 \
  -in identity.p12 \
  -nocerts \
  -out private.key
Enter fullscreen mode Exit fullscreen mode

Extract the certificate:

openssl pkcs12 \
  -in identity.p12 \
  -clcerts \
  -nokeys \
  -out cert.pem
Enter fullscreen mode Exit fullscreen mode

Extract the full chain:

openssl pkcs12 \
  -in identity.p12 \
  -nodes \
  -out fullchain.pem
Enter fullscreen mode Exit fullscreen mode

When Should You Use Which?

Use PEM when:

  • configuring servers or infrastructure
  • using Linux / DevOps tooling
  • working with Kubernetes, NGINX, Apache, Envoy
  • certificates are long-lived and versioned
  • human readability is useful

Use P12 when:

  • you need to securely transport an identity
  • importing into Windows, browsers, Java, Keycloak, etc.
  • bundling key + cert + chain in one file
  • password protection is required

Summary

PEM and PKCS#12 aren’t competitors — they solve different problems:

  • PEM = simple, readable, DevOps & Linux friendly
  • P12 = secure, portable identity container

You’ll encounter both, and understanding their roles saves a lot of confusion when systems refuse to accept your certificates.

Top comments (0)