When I generate my keys using
gpg2 --gen-key
and then I try to try to encrypt the file using
gpg2 --batch --yes -r myemail@test.com --always-trust --homedir . -e test.text
I get an error "Encryption failed: No Public Key" error.
I can however see it using the command:
gpg2 --list-keys
Importing the public key explicitly doesn’t help. Ideas?
Best Answer
You're setting your current work directory as GnuPG home directory, which is pretty much never the thing you want to do. For day to day usage, do not set this option at all and have GnuPG use the default GnuPG home directory location (
~/.gnupg
).Leave out this option, and encryption will work fine.
Additionally,
--always-trust
should never be used together with keys you did not validate on your own, ie. only together with the fingerprint or at least long key ID of the key. An attacker can generate keys for arbitrary other mail addresses/user IDs and distribute them, you might be ending up encrypting for the wrong key. There are reasons to use this flag (especially when interfacing GnuPG from scripts and applications), but be very sure about its implications regarding security.