I'm able to export the public key using gpg2 --export-ssh-key 15EDA5801C8D18FF. How do I get the private SSH subkey?

Asked this question way too complicated here as well

2 Answers 2


This is a helpful article for RSA keys.


GnuPG to OpenSSH

First, you need to know fingerprint of your RSA key. You can use:

gpg --list-secret-keys --keyid-format short

Next, you can use openpgp2ssh tool distributed in with monkeyshpere project:

gpg --export-secret-keys (or --export-secret-subkeys) 01234567! | openpgp2ssh 01234567 > id_rsa

A few notes are necessary:

01234567 must be fingerprint of a RSA key (or subkey)
gpg --export-secret-keys also accept finger print of global key (in this case, it exports all sub-keys). However, openpgp2ssh only accept finger print of an RSA key
If no arguments are provided, openpgp2ssh export RSA keys it find

You can now extract ssh public key using:

ssh-keygen -y -f id_rsa > id_rsa.pub

First, there are tens maybe hundreds of formats used for privatekeys, and you don't say which you want. gpg --export-ssh-key produces the publickey format defined by OpenSSH and also used by many things compatible with OpenSSH (like Putty, Jsch, paramiko) and you seem satisifed with that, so I'll arbitrarily guess you want a format usable by OpenSSH. If not, this may not work, and you lose.

Ignoring SSHv1 keys, which were different but are now all long obsolete, OpenSSH historically used for SSHv2 the same privatekey formats as OpenSSL, for the very good reason that it used the OpenSSL library code to handle those keys and the related crypto operations. OpenSSL in turn supported several DER formats (not relevant here) and 8 PEM formats: 2 'legacy' formats specific to each keytype aka algorithm (RSA, DSA/DSS, EC[DSA]) and 2 'PKCS8' formats applicable to any (supported) keytype. ssh-keygen would write only the legacy foramts, but both it and ssh and sshd could read either legacy or PKCS8. (And since the encrypted form of PKCS8 used a much stronger PBE method then the encrypted form of legacy, you can find lots of Stack Qs/As and other websites that date from this period recommending you convert or replace a legacy privatekey used for OpenSSH with a PKCS8 one for better security.)

Then OpenSSH added ed25519, a keytype/algorithm not then implemented by OpenSSL, so they created their own privatekey format called simply 'new' format, using an even stronger PBE method than OpenSSL PKCS8, and able to support all keytypes. Initially starting with 6.5 in 2014 this was used by default only for ed25519, but could be requested for other types by specifying -o, and again you will find Qs/As and websites recommending this for better security. Then starting with 7.8 in 2018 new format is used by default for all types, but you can request legacy format (except for ed25519) using -m pem.

gpg, the program that supports PGP messaging, does not support any of these. However, the GnuPG package also includes a different program gpgsm, which supports S/MIME messaging, and it DOES support PKCS8 format. (And also PKCS12 format, which is supported by OpenSSL but not OpenSSH.) And for RSA only there is an amazing hack by which you can transfer a key defined for PGP in gpg to gpgsm nominally for use in S/MIME. Follow the procedure in this answer except use --export-secret-key-p8 --armor|-a or equivalently my gloss over here but stop at step 3. The PKCS8 (privatekey) PEM file is usable with OpenSSH. If you want it encrypted you can use openssl pkey -$alg or openssl pkcs8 -topk8 to convert to PKCS8-encrypted, or ssh-keygen -p to convert to OpenSSH new format.

Alternatively, as I said in the latter answer, you could write a very simple program to do this in Java using the BouncyCastle libraries, which support BOTH PGP/gpg formats and OpenSSL/PKIX/S/MIME formats (and others as well). There are almost certainly libraries in JS/nodejs and python and perl that can do the same, and probably in some other languages I don't even know about, but I'm not familiar with them. However, writing a program is usually considered to belong on StackOverflow not SU.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .