12

I have a GPG public key key.gpg in binary format. I would like to convert this to ASCII so that the resulting file has the header

-----BEGIN PGP PUBLIC KEY BLOCK-----

The process I am setting up to do this does not have access to the usual central key store, so I cannot import and then export.

What is the GnuPG (version 2) command to do this?

rlandster
  • 1,412

3 Answers3

6
gpg --enarmor < key.gpg > key.asc

should do the trick.

Burnus
  • 214
  • 3
    That puts the header -----BEGIN PGP ARMORED FILE----- rather than the one I wanted. If the only difference is the header I suppose I could just change that manually... – rlandster Jan 10 '20 at 18:02
  • 1
    True that. This is probably due to the fact that the enamor command is supposed to handle arbitrary data, not just key files. You should be save just editing the Begin and End lines (even if the receiving end does care about those at all). – Burnus Jan 10 '20 at 19:52
  • For reference: https://lists.gnupg.org/pipermail/gnupg-devel/2011-October/026253.html – Jérôme Pouiller Oct 25 '23 at 06:30
  • This also works if you have a signed, encrypted, binary GPG message and want to make it a signed, encrypted, ascii message for inclusion in something like an email. – Stewart Feb 13 '24 at 12:43
6

Perhaps you can just create another, temporary keyring to import and export the key like this:

gpg  --keyring /tmp/temp.gpg  --no-default-keyring --import key.gpg 
gpg  --keyring /tmp/temp.gpg  --no-default-keyring  --export -a

I don't find that elegant but it should work. Note that the second command exports all the keys present in the keyring to one single file.

Kate
  • 491
5
gpg --keyring ./key.gpg --no-default-keyring --export -a > key.asc

Note the use of ./key.gpg rather than just key.gpg: GnuPG won't do what you expect with the latter and will create an empty keyring in ~/.gnupg/key.gpg instead.

Abdull
  • 2,194