We receive GPG encrypted files from a third party. I\'m modifying a C# program that finds the encrypted files, decrypts them, and deletes the encrypted ones. It all works
You're using GnuPG 2, which only allows the --passphrase*
options together with --batch
.
--batch
The --passphrase*
options are meant to be used for scripting. GnuPG 2 limits them (probably for slowly deprecating them out) to the --batch
mode, where GnuPG does not perform any interaction (eg., asking for your passphrase or other "dialogues").
While this is still possible, it might be preferable to use the password presetting in gpg-agent
instead, which allows you to remove the passphrase completely from your application code. Note the implications of --passphrase
(all users on your system can read it, as long as GnuPG is running!) and --passphrase-file
(the passphrase is stored on the hard disk, watch out for permissions).
Preferred method with GnuPG 2 is to preset the passphrase in gpg-agent
, which GnuPG heavily relies on; in case of GnuPG 2.1 the even handles private key and passphrase operations completely on its own.
But, to your rescue, GnuPG 2 brings a new tool, gpg-preset-passphrase
. On Debian Linux, it hides in /usr/lib/gnupg2/
, I don't know where it is stored in Windows.
From man gpg-preset-passphrase
:
The
gpg-preset-passphrase
is a utility to seed the internal cache of a runninggpg-agent
with passphrases. It is mainly useful for unattended machines, where the usualpinentry
tool may not be used and the passphrases for the to be used keys are given at machine startup.[...]
gpg-preset-passphrase
is invoked this way:gpg-preset-passphrase [options] [command] cacheid
cacheid
is either a 40 character keygrip of hexadecimal characters identifying the key for which the passphrase should be set or cleared. [...]One of the following command options must be given:
--preset Preset a passphrase. This is what you usually will use. gpg-preset-passphrase will then read the passphrase from stdin.
To wrap up, when initialising GnuPG for your application (and in intervalls corresponding to the configured cache time) run gpg-preset-passphrase --preset [fingerprint]
, which will read the passphrase from stdin, or additionally use a --passphrase passphrase
option to directly set it in your query. Be aware that when using both the echo or --passphrase
approach, other system users might get hold of the passphrase by listing processes; better directly write to the process' stdin from C#.