Skip to content

Instantly share code, notes, and snippets.

@ardnew
Created March 5, 2024 20:39
Show Gist options
  • Save ardnew/b823cc682a371f0480ca6c9d11086dd0 to your computer and use it in GitHub Desktop.
Save ardnew/b823cc682a371f0480ca6c9d11086dd0 to your computer and use it in GitHub Desktop.
Extract or recreate original SSH private key added to GPG2 keyring
$ gpg2 --list-secret-keys --keyid-format short
/home/user/.gnupg/pubring.kbx
-------------------------------
sec rsa4096/53C2371D 2020-06-18 [SC]
47009EB0BA7E95211A217130D77BCDBD8340250A
uid [ultimate] User Name <[email protected]>
ssb rsa4096/53AF00DS 2020-06-18 [E]
ssb rsa4096/CA7F00DS 2021-02-18 [A]
# Notice I'm not using the primary key, but instead the first subkey with
# authentication capability "[A]".
#
# Also important to append the "!" to the key ID given to gpg2. From the
# manpage:
# | By specifying the key to export using a key ID or a fingerprint
# | suffixed with an exclamation mark (!), a specific subkey or the
# | primary key can be exported.
#
# The openpgp2ssh utility is part of the MonkeySphere project. Most apt-
# based distros can install it via 'apt install monkeysphere'.
$ gpg2 --export-secret-keys CA7F00D5! | openpgp2ssh CA7F00D5 > ~/.ssh/gpg.id_rsa
Please enter the passphrase to export the OpenPGP secret key:
"User Name <[email protected]>"
4096-bit RSA key, ID D77BCDBD8340250A,
created 2020-06-18.
Passphrase:
# Verify output is identical to original, SSH-formatted RSA private key.
$ file ~/.ssh/gpg.id_rsa
.ssh/gpg.id_rsa: PEM RSA private key
$ diff ~/.ssh/id_rsa ~/.ssh/gpg.id_rsa
$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment