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

Super User Asked by Inaimathi on January 1, 2022

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?

4 Answers

Use the --export-ssh-key option and then convert it to PEM:

ssh-keygen -f <(gpg --export-ssh-key <short_key_id>) -e -m pem

Answered by AiB on January 1, 2022

Since GitLab needs the Public GPG key in PEM format, you could follow their tutorial.

To export the private key to txt specifically:

gpg --armor --export-secret-keys 0x<key-ID>


gpg --armor --export-secret-keys "email-address"

Use the Key-ID or the email-address that was used to generate the private/secret key.

To write private key to disk:

gpg --armor --export-secret-keys "[email protected]" > test.pem

To public key to txt/pem:

gpg --export -a "[email protected]"

Remember to use your email/key-id instead.

Answered by lnksz on January 1, 2022

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}

Answered by petko on January 1, 2022

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

Answered by Dolanor on January 1, 2022

Add your own answers!

Related Questions

Conditional Formatting of decimal places

0  Asked on December 5, 2021 by natalie-lawrence


How to tell full path of file on Mac?

6  Asked on December 3, 2021 by user27449


OS Update gone wrong

1  Asked on December 3, 2021 by rutday


SSH automatically logging out

2  Asked on December 3, 2021 by myranda-rose


PXELINUX network booting an ISO file

1  Asked on December 3, 2021 by sundararajan


Power button on USB keyboards?

1  Asked on December 3, 2021


images disappeared after saving a big WORD file

2  Asked on December 3, 2021 by roudan


Account sometimes partially lose administrator status

0  Asked on December 1, 2021 by zdimension


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir