GPG automatic decryption password passing

后端 未结 1 540
醉话见心
醉话见心 2020-12-18 13:27

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

相关标签:
1条回答
  • 2020-12-18 14:30

    The Problem

    You're using GnuPG 2, which only allows the --passphrase* options together with --batch.

    Using --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).

    Presetting the Passphrase

    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 running gpg-agent with passphrases. It is mainly useful for unattended machines, where the usual pinentry 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#.

    0 讨论(0)
提交回复
热议问题