Ctrl + K
Security17 min read

PEM Files Explained

Understand PEM files, their structure, common certificate and private key types, BEGIN and END markers, certificate chains, conversion and security considerations.

Published: 2026-09-02

PEM is one of the most common text-based formats used to store and exchange cryptographic material. PEM files are frequently encountered when configuring HTTPS, TLS certificates, private keys, certificate signing requests and certificate chains on web servers, reverse proxies and other network services.

Despite the name, a PEM file is not necessarily a certificate. PEM describes a textual encoding and container format that can represent different types of binary data. Depending on the file and its BEGIN and END markers, a PEM document may contain a certificate, private key, public key, certificate signing request or another encoded object.

What Is a PEM File?

PEM stands for Privacy-Enhanced Mail. The original Privacy-Enhanced Mail standards were designed for secure email, but the textual encoding format became widely used for certificates and cryptographic keys.

A PEM file stores binary data as Base64 text surrounded by recognizable header and footer lines. This makes the content easier to transfer through systems that were originally designed to handle text rather than arbitrary binary data.

-----BEGIN CERTIFICATE-----
MIID...
...Base64 encoded data...
...more encoded data...
-----END CERTIFICATE-----

The Base64 section is an encoded representation of binary data. The BEGIN and END lines identify what type of object is contained between them.

PEM Is a Format, Not a Specific Certificate Type

One of the most important things to understand is that PEM does not mean certificate. A PEM file is simply a textual representation of encoded data with a defined structure. The actual object can be identified from its PEM label and decoded content.

PEM LabelCommon Object
CERTIFICATEX.509 certificate
PRIVATE KEYPKCS#8 private key
RSA PRIVATE KEYTraditional RSA private key
EC PRIVATE KEYTraditional EC private key
PUBLIC KEYSubjectPublicKeyInfo public key
RSA PUBLIC KEYTraditional RSA public key
CERTIFICATE REQUESTPKCS#10 CSR

PEM File Structure

A typical PEM object consists of a BEGIN line, Base64-encoded content and an END line. The label in the BEGIN and END markers describes the object type.

-----BEGIN OBJECT TYPE-----
BASE64 ENCODED DATA
-----END OBJECT TYPE-----

For example, an X.509 certificate normally uses the CERTIFICATE label, while a PKCS#8 private key uses PRIVATE KEY.

-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----

-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----

What Is Base64 Encoding?

Base64 converts binary data into a sequence of printable characters. PEM uses Base64 so that binary cryptographic objects can be represented as text.

Base64 is an encoding mechanism, not encryption. Anyone who has a PEM certificate can decode its Base64 content. Likewise, a PEM private key is not protected simply because its contents are Base64 encoded.

⚠️ Base64 does not make a private key secret. A Base64-encoded private key can be decoded easily, so private keys must be protected using proper file permissions, encryption and secure storage.

PEM Certificates

X.509 certificates are commonly distributed in PEM format. A certificate contains information such as the subject, issuer, validity period, public key and digital signature.

-----BEGIN CERTIFICATE-----
MIIC...
...
-----END CERTIFICATE-----

A certificate does not contain the corresponding private key. The certificate normally contains a public key and information binding that public key to an identity according to the certificate's fields and issuer.

PEM Private Keys

Private keys can also be stored in PEM format. They are used by cryptographic systems to prove possession of a private key and to perform operations that must remain under the control of the key owner.

-----BEGIN PRIVATE KEY-----
MIIE...
...
-----END PRIVATE KEY-----

The PRIVATE KEY label commonly represents a PKCS#8 private key. Other PEM labels can indicate algorithm-specific formats such as RSA PRIVATE KEY or EC PRIVATE KEY.

PRIVATE KEY vs RSA PRIVATE KEY

The difference between PRIVATE KEY and RSA PRIVATE KEY is primarily the structure used to represent the private key. PRIVATE KEY commonly indicates PKCS#8, a general-purpose private key container, while RSA PRIVATE KEY traditionally represents an RSA-specific PKCS#1 structure.

PEM LabelTypical FormatAlgorithm Scope
PRIVATE KEYPKCS#8General-purpose
RSA PRIVATE KEYPKCS#1RSA
EC PRIVATE KEYSEC1Elliptic Curve
ENCRYPTED PRIVATE KEYEncrypted PKCS#8General-purpose

Encrypted PEM Private Keys

A private key can be stored in an encrypted form. An encrypted private key requires a password or another secret to decrypt the key before it can be used.

-----BEGIN ENCRYPTED PRIVATE KEY-----
MII...
...
-----END ENCRYPTED PRIVATE KEY-----

Encryption is particularly important when private key files are stored on systems where unauthorized users or processes might otherwise be able to read them.

PEM Public Keys

Public keys can also be represented using PEM. A public key is not secret and can be distributed to systems that need to verify signatures or otherwise use the public portion of a key pair.

-----BEGIN PUBLIC KEY-----
MIIB...
...
-----END PUBLIC KEY-----

PUBLIC KEY commonly represents a SubjectPublicKeyInfo structure. RSA public keys can also appear using the RSA PUBLIC KEY label when stored in an algorithm-specific structure.

PEM Certificate Signing Requests

A Certificate Signing Request, or CSR, is commonly stored in PEM format. A CSR contains information that is submitted to a certificate authority when requesting a certificate.

-----BEGIN CERTIFICATE REQUEST-----
MIIC...
...
-----END CERTIFICATE REQUEST-----

A CSR typically contains a subject, a public key and a signature created using the corresponding private key. It does not itself represent the final TLS certificate issued by a certificate authority.

PEM and DER

DER and PEM often contain the same underlying type of cryptographic object but represent it differently. DER is a binary encoding, while PEM wraps Base64-encoded DER data in text markers.

PropertyPEMDER
RepresentationTextBinary
Base64YesNo
BEGIN/END markersYesNo
Human-readablePartiallyNo
Common extension.pem, .crt, .cer, .key.der, .cer, .crt

The file extension alone does not always tell you whether a file contains PEM or DER data. The actual encoding and object type should be inspected when there is uncertainty.

PEM vs CRT vs CER vs KEY

File extensions used with certificates and keys can be confusing because there is no universal rule that maps every extension to exactly one encoding. A .crt or .cer file may contain a certificate encoded as PEM or DER, while .key is commonly used for private keys.

ExtensionCommon UseEncoding
.pemCertificates, keys and other PEM objectsUsually PEM
.crtCertificatePEM or DER
.cerCertificatePEM or DER
.keyPrivate keyOften PEM
.csrCertificate signing requestOften PEM
⚠️ Do not identify a certificate or key format solely from its filename extension. Inspect the file contents and PEM markers when necessary.

Multiple PEM Objects in One File

A single PEM file can contain multiple PEM blocks. This is common when a file contains a certificate together with intermediate certificates forming a certificate chain.

-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----

-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----

-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----

Software that supports PEM bundles can process multiple objects in sequence. The order can matter when the file is being used as a certificate chain, so the expected chain structure should be followed.

PEM Certificate Chains

TLS servers commonly need more than the leaf certificate. A certificate chain can contain the server certificate followed by one or more intermediate certificates that help clients build a path to a trusted root certificate.

Server Certificate
        ↓
Intermediate CA
        ↓
Root CA

The root certificate is commonly already available in a client's trust store, while the server typically provides the necessary intermediate certificates during the TLS handshake.

Certificate Bundle vs Private Key

A certificate bundle and a private key are different objects and should normally be stored separately. A certificate bundle contains public certificate information, while the private key must remain secret.

ObjectContainsSecret?
CertificateIdentity and public key informationNo
Intermediate certificateCA certificate informationNo
Certificate chainMultiple certificatesNo
Private keyPrivate cryptographic keyYes
CSRCertificate request informationNo

PEM File Line Wrapping

Traditional PEM representations wrap Base64 data across multiple lines. Implementations should follow the relevant format requirements rather than assuming that arbitrary line wrapping is always accepted by every parser.

When manually copying PEM data, preserving the BEGIN and END markers and the complete Base64 content is important. Missing characters, accidental spaces or truncated blocks can cause parsing failures.

Common PEM Errors

  • Missing BEGIN or END markers.
  • Mismatched BEGIN and END labels.
  • Truncated Base64 data.
  • Copying only part of a certificate.
  • Using the wrong certificate or private key.
  • Mixing certificate and private key files incorrectly.
  • Providing a DER file to software expecting PEM.
  • Using an incomplete certificate chain.
  • Incorrect file permissions on private keys.
  • Accidentally exposing a private key.

Mismatched PEM Markers

The BEGIN and END labels should identify the same object type. A certificate should have matching CERTIFICATE markers, while a private key should have matching markers appropriate to its structure.

-----BEGIN CERTIFICATE-----
...
-----END PRIVATE KEY-----
⚠️ Mismatched PEM markers indicate malformed content and should not be treated as a valid certificate or key.

PEM Certificate Validation

A PEM certificate can be decoded to inspect its subject, issuer, validity dates, public key information, extensions and signature details. Decoding helps identify what the certificate contains, while validation also requires checking whether the certificate is trusted and appropriate for the intended use.

openssl x509 -in certificate.pem -text -noout

This OpenSSL command displays human-readable information about an X.509 certificate stored in PEM format.

Inspecting a PEM Private Key

OpenSSL can also inspect private keys. The exact command depends on the key type and OpenSSL version.

openssl pkey -in private-key.pem -text -noout
⚠️ Be careful when displaying private key information in terminals, logs or shared environments. Commands that print key details should only be used in secure administrative contexts.

Converting PEM to DER

PEM and DER can often be converted when software requires a different encoding. For example, OpenSSL can convert a PEM certificate to DER format.

openssl x509 -in certificate.pem -outform DER -out certificate.der

The reverse conversion can also be performed by specifying the appropriate input and output formats.

openssl x509 -inform DER -in certificate.der -out certificate.pem

PEM and OpenSSL

OpenSSL is one of the most widely used command-line tools for working with certificates, keys, CSRs and other cryptographic objects. It supports many PEM-related operations, including inspection, conversion, generation and validation.

TaskExample OpenSSL Command
Inspect certificateopenssl x509 -in cert.pem -text -noout
Inspect private keyopenssl pkey -in key.pem -text -noout
Inspect CSRopenssl req -in request.csr -text -noout
Convert certificate to DERopenssl x509 -in cert.pem -outform DER

PEM Private Key Security

Private keys require substantially more protection than certificates or public keys. Anyone who obtains an active private key may be able to impersonate the associated service or perform cryptographic operations as the key owner, depending on the system and protocol.

  • Restrict filesystem permissions.
  • Avoid committing private keys to Git repositories.
  • Do not send private keys through public channels.
  • Use encrypted private-key storage when appropriate.
  • Rotate compromised keys immediately.
  • Remove secrets from logs and debugging output.
  • Use dedicated secret-management systems for production credentials.

PEM Files in Web Server Configuration

Web servers and reverse proxies frequently use PEM certificates and keys when configuring HTTPS. A typical TLS configuration requires a certificate and its corresponding private key, while some deployments also provide an intermediate certificate chain.

TLS configuration
      ↓
Server certificate
      +
Private key
      +
Certificate chain
      ↓
HTTPS connection

The exact configuration syntax depends on the server software. Common systems include Nginx, Apache HTTP Server, reverse proxies, load balancers and cloud infrastructure.

Certificate and Private Key Must Match

A TLS certificate contains a public key that corresponds to a private key. The server must have the matching private key available to successfully use the certificate for its intended cryptographic operations.

A certificate can be valid and trusted while still being unusable with a particular private key. This is why TLS configuration problems sometimes occur even when the certificate itself appears correct.

Checking a Certificate and Key Pair

One way to verify a certificate and private key pair is to compare the public key information derived from both objects. Modern OpenSSL commands can extract public key information without exposing the private key itself.

openssl x509 -in certificate.pem -pubkey -noout

openssl pkey -in private-key.pem -pubout

PEM File Extensions

PEM content can use different file extensions depending on the application. A file named .pem may contain a certificate or key, while files ending in .crt, .cer, .key or .csr may also contain PEM-encoded objects.

For reliable automation, applications should inspect the content or parse the object rather than assuming that a particular extension guarantees a specific encoding.

PEM vs PKCS#12

PEM files and PKCS#12 files are different approaches to storing cryptographic objects. PEM is text-based and commonly stores individual certificates or keys, while PKCS#12 is a binary container that can package certificates and private keys together and is often password protected.

FeaturePEMPKCS#12
EncodingText/Base64Binary
Multiple objectsPossibleDesigned as a container
Common extension.pem, .crt, .key.p12, .pfx
Private key protectionDepends on formatCan be password protected
Human-readable structureVisible markersNo

PEM vs P7B

P7B, commonly associated with PKCS#7 certificate structures, is another certificate packaging format. It can contain certificate chains but generally does not contain a private key. It may be represented using binary DER or text-based PEM encoding.

When to Use PEM

  • Configuring HTTPS certificates.
  • Storing TLS private keys.
  • Exchanging certificate chains.
  • Working with OpenSSL.
  • Creating and processing CSRs.
  • Providing certificates to web servers.
  • Inspecting cryptographic objects with command-line tools.
  • Integrating certificate data into systems that expect PEM.

When PEM May Not Be the Right Format

PEM is not universally required. Some applications expect PKCS#12, Java KeyStore, DER or another specific format. The correct format depends on the software consuming the certificate or key.

💡 Before converting a certificate or key, check the documentation of the application that will consume it. The file extension alone does not determine the required encoding.

Common PEM Best Practices

  • Keep private keys separate from public certificates when appropriate.
  • Protect private key files with restrictive permissions.
  • Never treat Base64 encoding as encryption.
  • Use encrypted private keys when the deployment environment supports them appropriately.
  • Keep complete certificate chains when required by the TLS server.
  • Verify that certificates match their private keys.
  • Inspect certificates before deploying them.
  • Do not commit private keys to public repositories.
  • Use secure secret management for production private keys.
  • Choose the format expected by the target application.

Common Mistakes

  • Assuming every .pem file is a certificate.
  • Confusing PEM encoding with encryption.
  • Using a certificate with the wrong private key.
  • Removing intermediate certificates from a required chain.
  • Mixing PEM and DER without converting the encoding.
  • Copying incomplete PEM blocks.
  • Exposing private keys in logs or source control.
  • Relying only on file extensions to identify formats.
  • Using an unsupported key format with a server.
  • Forgetting to renew certificates before expiration.

Frequently Asked Questions

What is a PEM file?

A PEM file is a text-based representation of encoded cryptographic data. It commonly contains certificates, private keys, public keys, certificate signing requests or certificate chains.

Does PEM mean certificate?

No. PEM describes a textual encoding and container style. The actual object can be identified from its PEM label and decoded structure.

Is PEM encrypted?

Not by default. PEM commonly uses Base64 encoding, which is not encryption. Private keys can be stored in encrypted PEM structures, but encryption must be explicitly used.

What does BEGIN CERTIFICATE mean?

It identifies the following Base64 data as a PEM-encoded certificate object. The corresponding END CERTIFICATE marker closes the object.

What is the difference between PRIVATE KEY and RSA PRIVATE KEY?

PRIVATE KEY commonly represents a PKCS#8 private key, while RSA PRIVATE KEY traditionally represents an RSA-specific PKCS#1 private key structure.

Can a PEM file contain multiple certificates?

Yes. A PEM file can contain multiple certificate blocks and is often used to store a certificate chain or bundle.

Can PEM contain a private key?

Yes. Private keys are commonly stored in PEM format using labels such as PRIVATE KEY, RSA PRIVATE KEY, EC PRIVATE KEY or ENCRYPTED PRIVATE KEY.

What is the difference between PEM and DER?

PEM is a text representation that typically contains Base64-encoded DER data with BEGIN and END markers. DER is the binary encoding itself.

Can I convert PEM to DER?

Yes. Tools such as OpenSSL can convert compatible certificates and other cryptographic objects between PEM and DER representations.

Can I open a PEM file in a text editor?

Yes. PEM files are text based, so their BEGIN and END markers and Base64 content can normally be viewed in a text editor. The encoded data itself is not intended to be human-readable.

Are PEM certificates safe to share?

Public certificates are generally designed to be distributed, but private keys are secret and must never be shared publicly.

Helpful Security Tools

A PEM Certificate Viewer helps inspect certificate information stored in PEM format, a CSR Decoder displays the contents of certificate signing requests, a CSR Generator creates new certificate signing requests, a Certificate Chain Viewer helps inspect certificate chains, and a TLS Version Checker identifies supported TLS protocol versions.

Conclusion

PEM is a widely used text-based format for representing certificates, private keys, public keys, CSRs and certificate chains. Its recognizable BEGIN and END markers surround Base64-encoded data, making binary cryptographic objects convenient to store and exchange through text-based systems.

Understanding PEM becomes much easier once the format is separated from the object it contains. A PEM certificate, PEM private key and PEM CSR are different cryptographic objects even though they use the same general textual representation. When working with PEM files, always verify the object type, encoding, certificate chain and key relationship, and protect private keys with the same care as any other sensitive credential.

Found an issue?

Found an error, outdated information, or something missing from this article? Let me know through the Contact page.

Your feedback helps improve our articles and keep them accurate and useful.