Is it possible to use a gpg public key to encrypt a message without importing the key

gpgpgppublic-key

Sometimes I might want to use someone's gpg key to send a message but will have no need to ever use the key again.

Importing the key in this instance seems unnecessary.

I've searched, but can't find anything suggesting this is possible. It is a bit annoying to have to do –delete-keys each time.

Best Answer

You could make a small shell script that copies your pubring.gpg file, imports the key, encrypts your file, then moves your original pubring.gpg file back into place. This turns it into a one-liner next time.

#!/bin/sh
cp -a ~/.gnupg/pubring.gpg ~/.gnupg/pubring.gpg-backup
gpg ... # Command to import 
gpg ... # Command to encrypt message/file
mv ~/.gnupg/pubring.gpg-backup ~/.gnupg/pubring.gpg

Note: parameters to the script are variables "$1", "$2", ...


Edit: I know I answered this a long time ago. I'd like to mention a pitfall in the above: an interruption before restoring the backup would lead to an altered keystore. I suggest instead copying into a temp directory:

#!/bin/sh
gpgtemp="$(mktemp -d gpgtemp.XXXXXXXXXX)"
cp -a ~/.gnupg "$gpgtemp"
gpg --homedir "$gpgtemp/.gnupg" ... # Command to import 
gpg --homedir "$gpgtemp/.gnupg" ... # Command to encrypt message/file
rm "$gpgtemp" -rf