Public Key File Extension: Common Formats Explained
The most common public key file extensions are .pub (used by OpenSSH for public keys) and .pem (used for PEM-encoded keys, including public keys). However, public keys appear under many extensions depending on the tool and format, including .crt, .cer, .key, .asc, and .der.
Why Public Keys Have Different Extensions
A public key is just a mathematical value, but the file that holds it can be encoded and labeled in several ways. The extension often reflects the encoding format, the intended use, or the software that created it. For example, OpenSSH generates a public key file ending in .pub by default, while OpenSSL typically uses .pem for PEM-encoded keys. Certificate files use .crt or .cer because they contain a public key wrapped in an X.509 certificate. The extension is a convention, not a strict rule—renaming a file does not change its contents, but it can confuse tools that rely on extensions.
Common Public Key File Extensions and Their Formats
- .pub – OpenSSH public key format. A single line of text starting with
ssh-rsa,ssh-ed25519, or similar, followed by the base64-encoded key and an optional comment. Used for SSH authentication. - .pem – Privacy-Enhanced Mail format. Base64-encoded data between
-----BEGIN PUBLIC KEY-----and-----END PUBLIC KEY-----lines. Can hold a raw public key or a certificate. Widely used by OpenSSL and many TLS libraries. - .crt / .cer – X.509 certificate files. These contain a public key plus identity information, signed by a certificate authority. They can be PEM or DER encoded. Often used for TLS/SSL.
- .key – Sometimes used for public keys (though more commonly for private keys). When used for public keys, it is usually PEM-encoded. Context matters: check the file contents.
- .der – Distinguished Encoding Rules. A binary format for certificates and keys. Less human-readable but compact. Often used in Java environments.
- .asc – ASCII-armored format, typically associated with PGP/GnuPG public keys. Text-based with
-----BEGIN PGP PUBLIC KEY BLOCK-----headers. - .ssh – Rare, but sometimes used for SSH public keys as an alternative to .pub.
How to Identify a Public Key File Without Relying on the Extension
Because extensions are not standardized, the most reliable way to identify a public key file is to inspect its contents. Open the file in a text editor. If it begins with ssh- (e.g., ssh-rsa, ssh-ed25519), it is an OpenSSH public key. If it contains -----BEGIN PUBLIC KEY----- or -----BEGIN CERTIFICATE-----, it is PEM-encoded. If it starts with -----BEGIN PGP PUBLIC KEY BLOCK-----, it is a PGP public key. Binary files that do not display as text are likely DER-encoded; use OpenSSL to inspect them: openssl x509 -in file.der -inform der -text -noout for certificates, or openssl pkey -in file.der -inform der -text -noout for keys.
How to Convert Between Public Key Formats
Different systems expect different formats, so conversion is common. OpenSSL is the standard tool for converting between PEM, DER, and other encodings.
- To convert a PEM public key to DER:
openssl pkey -in key.pem -pubin -outform der -out key.der - To convert a DER public key to PEM:
openssl pkey -in key.der -pubin -inform der -out key.pem - To extract a public key from a certificate:
openssl x509 -in cert.crt -pubkey -noout > pubkey.pem - To convert an OpenSSH public key to PEM:
ssh-keygen -f id_rsa.pub -e -m pem > id_rsa.pem
Always verify the output with a tool that can read the new format. For example, after converting to PEM, run openssl pkey -in newkey.pem -pubin -text -noout to confirm the key is valid.
Common Problems and Misconceptions
One frequent mistake is assuming that a file with a .key extension is always a private key. While .key is often used for private keys, it can also hold a public key. Always check the contents. Another misconception is that the extension determines the format—it does not. A .pem file could contain a certificate, a public key, or even a private key. Tools like OpenSSL often ignore the extension and rely on the -----BEGIN header to determine the format.
A related issue: copying a public key to a server for SSH authentication. The file must be in OpenSSH format (one line starting with ssh-) and placed in ~/.ssh/authorized_keys. If you have a PEM-encoded public key, you must convert it first using ssh-keygen -i or a similar tool. Also, beware of line endings: Windows-style CRLF can break OpenSSH keys; ensure Unix line endings (LF).
Choosing the Right Extension for Your Use Case
The extension should match the format and the tool you are using. For SSH keys, use .pub. For TLS certificates, use .crt or .cer (PEM-encoded is most common). For raw public keys used in OpenSSL operations, .pem is a safe default. For PGP, use .asc for ASCII-armored keys or .gpg for binary. When in doubt, stick to .pem for PEM-encoded keys and .der for DER-encoded ones. Avoid using .key for public keys to prevent confusion with private keys.
Frequently Asked Questions
What is the standard file extension for a public key?
There is no single standard. The most common are .pub for OpenSSH public keys and .pem for PEM-encoded public keys. Certificates containing public keys often use .crt or .cer.
Can a public key be stored in a .key file?
Yes, but it is less common. The .key extension is frequently used for private keys, so using it for a public key can cause confusion. Always inspect the file contents to be sure.
How do I know if a .pem file contains a public key or a private key?
Open the file in a text editor. A public key PEM file contains -----BEGIN PUBLIC KEY-----. A private key contains -----BEGIN PRIVATE KEY----- or -----BEGIN RSA PRIVATE KEY-----. Certificates contain -----BEGIN CERTIFICATE-----.
What is the difference between .pub and .pem?
.pub typically denotes an OpenSSH public key in a single-line format. .pem denotes a PEM-encoded key or certificate with header and footer lines. They are not interchangeable without conversion.
Can I rename a public key file to a different extension?
Yes, renaming does not change the file contents. However, some tools may rely on the extension to determine the format, so it is best to keep the extension consistent with the format or use tools that auto-detect.