SSD Nodes Learn Hosting plans →
Guides Matt ConnorBy Matt Connor

SSH Key Formats and How to Convert Them

Read the first line of a key file to learn its format, then convert between OpenSSH, PEM, PKCS#8 and PuTTY .ppk, and prove the fingerprint still matches.

Read the first line before you convert

SSH key formats differ in the wrapper around the key, and the first line of the file names that wrapper. Run head -1 on the file before you convert anything. Often the key is already in the format you need, and the conversion you were about to search for has nothing to do.

This guide lists the first lines you will meet. Then it converts one fresh Ed25519 key and one fresh RSA key between formats with ssh-keygen and openssl, plus puttygen for PuTTY files. Every conversion ends with a fingerprint check, so you know the key is still the same key. If you are still deciding which keys to make and where to keep them, read the basics of SSH key management first, and come back here when a tool rejects a file.

SSH key formats by their first line

  • -----BEGIN OPENSSH PRIVATE KEY-----: OpenSSH's own private key format. Since OpenSSH 7.8, ssh-keygen writes new private keys in this format by default.
  • -----BEGIN RSA PRIVATE KEY-----: the traditional RSA private key format, PKCS#1. OpenSSL writes it when you ask openssl rsa for -traditional.
  • -----BEGIN EC PRIVATE KEY-----: the traditional private key format for an ECDSA key.
  • -----BEGIN PRIVATE KEY-----: PKCS#8, unencrypted. PKCS#8 is a general container that can hold RSA, ECDSA or Ed25519 keys.
  • -----BEGIN ENCRYPTED PRIVATE KEY-----: PKCS#8, encrypted with a passphrase.
  • -----BEGIN PUBLIC KEY-----: a public key in the form OpenSSL writes.
  • ---- BEGIN SSH2 PUBLIC KEY ----: an RFC 4716 public key. Note the four dashes and the spaces. Some commercial SSH software uses this format.
  • PuTTY-User-Key-File-3: ssh-ed25519: a PuTTY .ppk file. The digit is the PPK format version, and the text after the colon is the key type.
  • ssh-ed25519 AAAA... or ssh-rsa AAAA... on a single line: an OpenSSH public key. This is the .pub file, and the same line goes into authorized_keys. It is not a private key.

A private key file always lets you rebuild its public half, and later sections do exactly that. You can never rebuild a private key from a public one.

A .pem file is a container, not a key type

PEM (privacy-enhanced mail) is a way to wrap binary data as text: base64 between a -----BEGIN ...----- line and a matching -----END ...----- line. The word after BEGIN is the label, and the label says what is inside. A .pem extension only tells you the file is wrapped this way. It does not tell you whether the key is RSA or Ed25519, PKCS#1 or PKCS#8, or even whether it is private.

So "convert .pem to RSA" often asks for nothing. If the first line of server.pem is -----BEGIN RSA PRIVATE KEY-----, the file already is an RSA private key in PKCS#1 form. Use it as it is. OpenSSH reads it directly, because its key loader tries OpenSSH's own format first and falls back to a PEM parser when the first attempt fails with a format error.

Convert only when the label does not match what the tool in front of you asks for. A library that asks for PKCS#8 needs BEGIN PRIVATE KEY. PuTTY needs a .ppk. Some older tools read only the traditional PEM form, which is why ssh-keygen can still write it.

Set up a test directory with fresh keys

Practice on keys you create now, not on the key that opens your server. The ssh-keygen -p commands below rewrite the file you give them.

sudo apt update
sudo apt install -y openssh-client openssl putty-tools
ssh -V
openssl version
mkdir -m 700 ~/keylab && cd ~/keylab
ssh-keygen -t ed25519 -f demo-ed -N '' -C keylab-ed25519
ssh-keygen -t rsa -b 3072 -f demo-rsa -N '' -C keylab-rsa
head -1 demo-ed demo-rsa

On Ubuntu 26.04, putty-tools comes from the universe component. If apt cannot find the package, enable universe and run the install again. ssh -V and openssl version show which versions you are testing. Ubuntu 26.04 ships OpenSSH 10.2p1 and OpenSSL 3.5.

head -1 should print -----BEGIN OPENSSH PRIVATE KEY----- for both keys, because OpenSSH format is the default for new keys. -N '' sets an empty passphrase, which keeps the demo short. Put a passphrase on real keys.

Convert an OpenSSH private key to PEM or PKCS#8 with ssh-keygen

ssh-keygen has no separate convert command for private keys. You change the format with -p, the change-passphrase option, and name the new format with -m. The man page says this combination "may be used to convert between OpenSSH private key and PEM private key formats". -p saves the result over the file it read, so copy the key first and convert the copy.

cp -p demo-rsa rsa-pem
ssh-keygen -p -m PEM -f rsa-pem -P '' -N ''
head -1 rsa-pem

cp -p demo-rsa rsa-pkcs8
ssh-keygen -p -m PKCS8 -f rsa-pkcs8 -P '' -N ''
head -1 rsa-pkcs8

-P gives the old passphrase and -N gives the new one, so the command does not stop to ask. For an encrypted key, put the real passphrases there, or leave both options out and answer the prompts. -m PEM writes the legacy PEM private key format. -m PKCS8 writes PKCS#8. Check the first line: RFC 7468 labels an unencrypted PKCS#8 key PRIVATE KEY and an encrypted one ENCRYPTED PRIVATE KEY.

Now try the same PEM conversion on the Ed25519 key:

cp -p demo-ed ed-pem
ssh-keygen -p -m PEM -f ed-pem -P '' -N ''
head -1 ed-pem

On OpenSSH 10.2p1, the version Ubuntu 26.04 ships, this command does not fail. In the 10.2p1 source, the function that writes a private key file checks the key type before the format, and it sends Ed25519 keys straight to the OpenSSH-format writer. The PEM code is never reached, so the command finishes without an error and head -1 still prints -----BEGIN OPENSSH PRIVATE KEY-----. -m PKCS8 does the same to an Ed25519 key on 10.2p1. OpenSSH 10.3 changed this: its release notes add support for writing Ed25519 keys in PKCS#8 format, and from that version -m PEM on an Ed25519 key stops with a format error. So check the first line of the output rather than trusting the exit status. Either way, -p rewrote the file, which is why you converted a copy.

To go back to OpenSSH format, run -p again with no -m. The default private key format in ssh-keygen is OpenSSH's own, so the rewritten file starts with -----BEGIN OPENSSH PRIVATE KEY----- again.

cp -p rsa-pem rsa-back
ssh-keygen -p -f rsa-back -P '' -N ''
head -1 rsa-back

Get the public key back, or export it as RFC 4716 or PKCS#8

ssh-keygen -y reads a private key and prints the matching OpenSSH public key, the one-line form that goes into authorized_keys. It also reads the PEM and PKCS#8 files you just made, because of the PEM fallback in the loader.

ssh-keygen -y -f rsa-pkcs8 > rsa-pkcs8.pub
cat rsa-pkcs8.pub

That line is the half you hand to other people and paste onto servers, and whether sharing an SSH public key is safe has its own guide.

-e exports a public key for other SSH software. With no -m, it writes RFC 4716, the format that starts ---- BEGIN SSH2 PUBLIC KEY ---- and may carry a Comment: header. -i does the reverse and prints an OpenSSH key. Its default input format is also RFC 4716.

ssh-keygen -e -f demo-ed.pub > ed-rfc4716.pub
head -2 ed-rfc4716.pub
ssh-keygen -i -f ed-rfc4716.pub

The last command should print the same ssh-ed25519 AAAA... key as demo-ed.pub. For tools built on OpenSSL, export the public key with -m PKCS8 instead. The same option imports an OpenSSL public key back into OpenSSH form:

ssh-keygen -e -m PKCS8 -f demo-rsa.pub > rsa-pub.pem
head -1 rsa-pub.pem
ssh-keygen -i -m PKCS8 -f rsa-pub.pem

-i expects an unencrypted file. Remove the passphrase from a private key before you import it this way.

Convert between PKCS#1 and PKCS#8 with openssl

openssl pkey and openssl rsa read keys in DER, PEM, P12 or ENGINE form. OpenSSH's own private key format is not on that list. So when you start from an OpenSSH key, convert it to PEM or PKCS#8 with ssh-keygen first, as above. From there, OpenSSL moves between the PEM variants:

openssl pkey -in rsa-pem -out rsa-from-openssl.pem
head -1 rsa-from-openssl.pem

openssl rsa -in rsa-pkcs8 -traditional -out rsa-pkcs1.pem
head -1 rsa-pkcs1.pem

chmod 600 rsa-from-openssl.pem rsa-pkcs1.pem

openssl pkey writes PKCS#8 unless you tell it otherwise. Its manual says a private key "is written using standard format: this is PKCS#8 form". So the first output file should start with -----BEGIN PRIVATE KEY-----, whatever the input was. The openssl rsa manual says -traditional writes "the traditional PKCS#1 format instead of the PKCS#8 format", and OpenSSL's PEM code labels that format RSA PRIVATE KEY. Without -traditional, openssl rsa writes PKCS#8 as well. openssl pkey accepts -traditional too, and for an RSA key the traditional format is PKCS#1.

The chmod 600 line matters. Make sure no other user can read a private key file, whatever tool wrote it.

To encrypt a key as PKCS#8, name a cipher. OpenSSL asks for the new passphrase:

openssl pkey -in rsa-pkcs1.pem -aes256 -out rsa-enc.pem
chmod 600 rsa-enc.pem
head -1 rsa-enc.pem
openssl pkey -in rsa-enc.pem -check -noout

The first line should now read -----BEGIN ENCRYPTED PRIVATE KEY-----. Encryption works only for PEM output. The manual notes that encryption is not supported for DER output. -check asks for the passphrase and then checks that the private and public parts of the key are consistent.

OpenSSL can also give you the public key. -pubout limits the output to the public components, and ssh-keygen -i -m PKCS8 turns the result into an OpenSSH line:

openssl pkey -in rsa-pkcs1.pem -pubout -out rsa-openssl-pub.pem
ssh-keygen -i -m PKCS8 -f rsa-openssl-pub.pem

Convert between OpenSSH and PuTTY .ppk with puttygen

PuTTY, the SSH client common on Windows, keeps private keys in its own format, PPK (PuTTY private key). puttygen from putty-tools converts in both directions on Linux. It loads OpenSSH and ssh.com private keys, and it detects the input type by itself.

From OpenSSH to PPK:

puttygen demo-ed -O private -o demo-ed.ppk
puttygen demo-rsa -O private -o demo-rsa.ppk
chmod 600 demo-ed.ppk demo-rsa.ppk
head -1 demo-ed.ppk

The first line has the form PuTTY-User-Key-File-<version>: <algorithm>. The man page for the puttygen in Ubuntu 26.04 (putty-tools 0.83) says version 3 is the default. Version 3 derives the encryption key from your passphrase with Argon2 and uses HMAC-SHA-256 to protect the file. Version 2 used an older method based on SHA-1, and PuTTY's documentation says it is less resistant to brute-force guessing of the passphrase.

Ask for version 2 only when the key must load in PuTTY 0.74 or older, or in another tool that does not read version 3 yet:

puttygen demo-ed -O private --ppk-param version=2 -o demo-ed-v2.ppk
chmod 600 demo-ed-v2.ppk
head -1 demo-ed-v2.ppk

The header line should now start with PuTTY-User-Key-File-2. Compare the two files with diff demo-ed.ppk demo-ed-v2.ppk to see what else changed.

From PPK back to OpenSSH:

puttygen demo-ed.ppk -O private-openssh -o ed-from-ppk
puttygen demo-rsa.ppk -O private-openssh-new -o rsa-from-ppk
chmod 600 ed-from-ppk rsa-from-ppk
head -1 ed-from-ppk rsa-from-ppk
puttygen demo-ed.ppk -L

The two output types differ. private-openssh uses "the oldest format available" for the key type, to work with old software. For an RSA key, that is a legacy PEM file. PuTTY's documentation says Ed25519 keys always use OpenSSH's newer format, because no other format is valid for them. private-openssh-new forces the newer BEGIN OPENSSH PRIVATE KEY format even for RSA, DSA and ECDSA keys. -L prints the public key as an OpenSSH line, ready for authorized_keys. If the PPK has a passphrase, puttygen asks for it. In a script, --old-passphrase names a file whose first line is the passphrase. The converted file keeps that same passphrase, because puttygen changes the passphrase of an existing key only when you ask it to. So ssh-keygen asks for the old passphrase when it loads the result. To set a different one, add -P and puttygen prompts for it, or use --new-passphrase with a file whose first line is the new passphrase. The manual says that if that file is empty, the saved key is unencrypted.

On Windows, the PuTTYgen window does the same work. Use Conversions, then Import, to load an OpenSSH private key, and then save it as a .ppk. To go the other way, use Conversions, then Export OpenSSH key, or the "force new file format" entry if you want the newer format for an RSA or ECDSA key.

Prove the converted key is the same key

A conversion changes the wrapping, never the key. Fingerprints prove it. ssh-keygen -l prints the fingerprint of a public key file. So derive a public key from each private file, and fingerprint each result:

for f in demo-rsa rsa-pem rsa-pkcs8 rsa-back rsa-from-openssl.pem rsa-pkcs1.pem rsa-from-ppk; do
  ssh-keygen -y -f "$f" > check.pub && ssh-keygen -lf check.pub
done

Every line must show the same fingerprint. The comment at the end of the line can differ between formats. Only the fingerprint matters. Leave rsa-enc.pem out of the loop, or type its passphrase when ssh-keygen asks.

For the PuTTY files, compare the public key that puttygen extracts with the original .pub file:

puttygen demo-ed.ppk -L > from-ppk.pub
ssh-keygen -lf from-ppk.pub
ssh-keygen -lf demo-ed.pub

A different fingerprint means a different key. Usually you converted the wrong file, or a -p run overwrote a file you did not mean to touch. Go back to the copy you made before the conversion.

Why does my key fail with "invalid format"?

When ssh-keygen cannot read a key file, it prints Load key "<file>": followed by the reason. The reason is not always invalid format. OpenSSH tries its own format first and then hands the file to OpenSSL's PEM parser, and when OpenSSL is the last parser to give up, the reason can read error in libcrypto instead. Treat both messages as the same kind of failure: neither parser could read the file. Test any suspect file the same way: ssh-keygen -y -f <file>. If that prints a public key, the file format is fine and the problem is somewhere else. Then read the first and last lines with head -1 and tail -1, and go through the checks below.

The file is the public half

The first line starts with ssh-ed25519 AAAA or ssh-rsa AAAA, and the whole key sits on one line. That is a public key. It holds no private part, so it can never work as an identity. On Ubuntu 26.04, ssh-keygen -y -f on such a file prints Load key "<file>": error in libcrypto, not invalid format, so do not rule this cause out because of the wording. Point ssh -i, and any IdentityFile line in ~/.ssh/config, at the file without .pub.

The file is a PuTTY key

The first line starts with PuTTY-User-Key-File-. OpenSSH tries its own format and then the PEM parser, and a PPK file is neither. Convert it with puttygen key.ppk -O private-openssh-new -o key, then run chmod 600 key.

The file was cut short when you copied it

The last line must be the END line that matches the BEGIN line, with the same label. In OpenSSH's source, an OpenSSH-format key with no end marker fails as a format error, and the loader then passes the file to the PEM parser, which rejects it too. So the message can read invalid format or error in libcrypto. This happens when you paste a key from a terminal or a web page and miss a line. Copy the file again as a file, for example with scp, not through the clipboard.

Windows line endings or a missing final newline

These two are blamed in many forum threads. Treat them as suspects to test, not as known causes. OpenSSH's decoder is written to skip carriage-return characters inside the base64 body. Check the file, fix a copy, and test the copy:

grep -c $'\r' key
tail -c 1 key | od -c
cp -p key key.fixed
sed -i 's/\r$//' key.fixed
# only if od did not show \n as the last byte:
printf '\n' >> key.fixed
ssh-keygen -y -f key.fixed

A count above 0 from grep means the file has Windows line endings. If od does not show \n, the file has no final newline. If the fixed copy loads and the original does not, that was the cause on your system. If both fail the same way, the cause is one of the checks above.

If the key now loads but the server still refuses it, the problem has moved from the file to the server side, and fixing "Permission denied (publickey)" covers that half. Once the converted key logs you in, harden SSH on your VPS and turn off password logins.

FAQ

How do I convert an OpenSSH private key to PEM?

Copy the key first, because ssh-keygen -p rewrites the file it reads. Then run ssh-keygen -p -m PEM -f copy-of-key and answer the passphrase prompts, or add -P with the old passphrase and -N with the new one. For an RSA key this writes the legacy PEM format. OpenSSH does not write Ed25519 keys as legacy PEM. On OpenSSH 10.2p1, as shipped in Ubuntu 26.04, both -m PEM and -m PKCS8 leave an Ed25519 key in OpenSSH format, and writing Ed25519 keys as PKCS#8 needs OpenSSH 10.3 or newer. Check the result with head -1.

How do I tell which format my SSH key is in?

Run head -1 on the file. BEGIN OPENSSH PRIVATE KEY is OpenSSH's own format. BEGIN RSA PRIVATE KEY is traditional PKCS#1. BEGIN PRIVATE KEY and BEGIN ENCRYPTED PRIVATE KEY are PKCS#8. PuTTY-User-Key-File- is a PuTTY .ppk. A single line that starts with ssh-ed25519 or ssh-rsa is a public key. The .pem extension tells you only that the file is PEM-wrapped text, not which key is inside.

Can I convert a .ppk file to an OpenSSH key on Linux?

Yes. Install putty-tools, which provides puttygen, and run puttygen key.ppk -O private-openssh-new -o key, then chmod 600 key. private-openssh-new gives the BEGIN OPENSSH PRIVATE KEY format for every key type, while private-openssh picks the oldest format the key type supports. If the .ppk has a passphrase, the converted key keeps the same one unless you change it with -P or --new-passphrase. puttygen key.ppk -L prints the public key as an OpenSSH line. Compare ssh-keygen -lf fingerprints before and after to confirm it is the same key.

What does ssh-keygen -m PEM do to my Ed25519 key?

It depends on the OpenSSH version, which ssh -V shows. On OpenSSH 10.2p1, the version in Ubuntu 26.04, the command finishes without an error but writes the key in OpenSSH format again, so the file still starts with BEGIN OPENSSH PRIVATE KEY. From OpenSSH 10.3, -m PEM stops with a format error for an Ed25519 key, and -m PKCS8 writes it as PKCS#8. Check the result with head -1, and keep the original OpenSSH-format file as well.

Was this article useful?

#ssh#ssh-keygen#openssl#putty#pem