How can I export public keys in PEM format with GnuPG?

I know that gpg --export -o pub.key -a "Username" exports a users' public key, but it doesn't seem to write a standard PEM format block.

Is there any way to export keys from gpg in PEM format?


You need the gpgsm utility, but, yes, you can.

gpgsm -o secret-key.p12 --export-secret-key-p12 0xXXXXXXXX

It contains keys and certificates. Then you can split them with openSSL and transform it in .pem at the same time

openssl pkcs12 -in secret-key.p12 -nocerts -out gpg-key.pem
openssl pkcs12 -in secret-key.p12 -nokeys -out gpg-certs.pem

You need gpgsm.

To clone the keys from the gpg keystore to the gpgsm keystore, check this comment. Copying the solution here

 $ gpg --list-secret-keys --with-keygrip
 $ gpgsm --gen-key -o temporary.cert
 > Existing Key
 > use keygrip from gpg output
 > fill the X509 values
 > create a self signed certificate
 $ gpgsm --import temporary.cert
 $ gpgsm --list-keys
 > find the key just imported
 $ gpgsm -o cert.p12 --export-secret-key-p12 ${KEY_ID}