Skip to content

Instantly share code, notes, and snippets.

@TomZhuPlanetart
Last active May 17, 2024 00:41
Show Gist options
  • Save TomZhuPlanetart/856f845a9508cd19a282ab22557f5cfc to your computer and use it in GitHub Desktop.
Save TomZhuPlanetart/856f845a9508cd19a282ab22557f5cfc to your computer and use it in GitHub Desktop.
Key formats

Key Formats

  • RFC4716: SSH2 public or private key. Begins with -----BEGIN OPENSSH PRIVATE KEY-----
  • PKCS#8: PKCS8 public or private key. Use the -m PKCS8 option of ssh-keygen. Begins with -----BEGIN PUBLIC KEY-----
  • PKCS#1
  • DER: Binary format
  • PEM: PEM public key. This refers to PKCS#8 when it's used in openssl commands.

PKCS8/PKCS1 are specifically for RSA keys.

Command Of ssh-keygen

  • -l Show fingerprint of specified public key file
  • -c Requests changing the comment in the private and public key files.
  • -e This option will read a private or public OpenSSH key file and print the key in RFC 4716 SSH Public Key File Format to stdout.
  • -y This option will read a private OpenSSH format file and print an OpenSSH public key to stdout.

Common Commands

Export the SSH public key in PEM format.

ssh-keygen -e -f ~/.ssh/id_rsa.pub -m PEM

note, the output key is a RSA public key, not a generic public key required for openssl commands like openssl rsautl -pubin

To be more specific, a RSA public key begins with a line like -----BEGIN RSA PUBLIC KEY----- while a PKCS8 starts with a line like -----BEGIN PUBLIC KEY-----

You'll need to convert it to a generic public key before running those commands, or you'll fail

openssl rsa -in ~/.ssh/pub.pem -RSAPublicKey_in -out -

Import an PEM format public key:

ssh-keygen -i -m PEM -f /tmp/pub.pem

Generate a public openssh key from a private key

ssh-keygen -y  -f ~/.ssh/id_rsa_1

show fingle print of SSH key

ssh-keygen -l -f ~/.ssh/id_rsa.pub 

References

https://linux.die.net/man/1/ssh-keygen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment