Is it possible to encrypt gpg with a public key instead of the recipient?
Is it safe for me to encrypt gpg or Should I stick with the recipient when encrypting with gpg? While going through various websites to understand the encryption with gpg, I found a blog that said that
Public-key cryptography, or asymmetric cryptography, is an encryption scheme that uses two mathematically related, but not identical, keys - a public key and a private key. ... The public key is used to encrypt and the private key is used to decrypt.
Gpg encrypt with public key is a common method, however GnuPG does not support encrypting to a recipient specified by a key file. The key must be imported in advance, and the recipient defined with either his mail address or key ID. I'd recommend using a cleaner approach as expected by GnuPG and hard-code either the key's fingerprint, or a user ID given by that key and import it as usual. If you really do not want to import the key, you could do following as workaround (which actually imports the key, but to a temporary GnuPG home directory): Import the key to a temporary folder, for example using gpg --homedir /tmp/gnupg --import my.pub
Determine the key ID of the key stored in the file:
KEYID=`gpg --list-public-keys --batch --with-colons --homedir /tmp/gnupg | head -n1 | cut -d: -f5`
Encrypt a message to the recipient
gpg --homedir /tmp/gnupg --recipient ${KEYID} --encrypt
Clean up temporary GnuPG home directory
rm -f /tmp/gnupg You could of course save this as a script to make using it more convenient.