we want to encrypt emails that we send to the user with his public key. this works great, however the user can also type in a broken/invalid public key ("asdf123") and then the encryption won't work obviously.

how can i validate, that the user inputted a pgp public key with correct format?

  • 2
    That depends on the environment. Would you like to do this in the context of an application or a shell?
    – MechMK1
    Oct 30 '19 at 13:38

You might want to try using pgpdump to test if the format of the public key that the user entered is valid. See https://superuser.com/questions/696941/human-readable-dump-of-gpg-public-key for more information.


You would import the key into a new keyring file, and use the --status-fd or --status-file option to gpg to have it generate running commentary in a fixed format that you can then process afterwards.

For example, importing my key from a keyserver with

curl 'http://keys.gnupg.net/pks/lookup?op=get&search=0xEBF67A846AABE354' | \
    gpg --status-file=status --import

generates a status file listing

[GNUPG:] KEY_CONSIDERED 9C43253495E4DCA837945F5BEBF67A846AABE354 0
[GNUPG:] IMPORT_OK 4 9C43253495E4DCA837945F5BEBF67A846AABE354
[GNUPG:] IMPORT_RES 1 0 0 0 0 0 0 295 0 0 0 0 0 0 0

In my case, the key was already in my keyring, although 295 signatures were added.

In addition, you should also check the exit code from the gpg process.

The same method can be used for retrieving a key from a keyserver, but you might be given multiple keys here, even for search by ID -- for example, if you use my short ID, 6AABE354, you get my real key and one from the evil32 set, so you may need additional UI to select one key in this case.


"@mti2935"'s answer tells you how to validate the format of a public key. This may not help if an adversary uses a correctly formatted public key. There is another way to validate and find out if a correctly formatted key came from the original sender.

PGP supports that creation and use of certificate authorities (CA) for the public keys. A CA distributes public and private keys (only kept by the owner of the private key). Each of these public keys belong to a specific CA. The CAs hands out certificates that match to specific public keys. The certificate includes information about the SHA hash of the public key, the current owner of the public key, and both the issue and expiration date of the certificate. With the involvement of a secure hash algorithm in the certificate, it will be almost impossible to forge a public key with the same hash as the original sender’s public key. If a CA is involved, you can look at the hash of the key in the public key certificate. Calculate the secure hash of the public key and try to match it with the one in the certificate. If it does not match, then you do not have the original senders public key. If it matches, check to see if the date you received the key is within the issue and expiration date of the certificate. If it is outside of that range, then the certificate was not valid when the key was created. An invalid certificate when a key was created mean the key should not be trusted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.