37

I've some stuff encrypted with GnuPG using gpg -e. When I decrypt them, the system does not ask for the passphrase, it decrypts it straight away.

Does it store the secret key somewhere and uses it (I also stored my secret key in the GnuPG key chain, does it uses that)?

How can I force the system to ask the passphrase every time?

40

Does it store the secret key somewhere and uses it (I also stored my secret key in the GnuPG key chain, does it uses that)?

GnuPG only uses keys from your key chain, so it must be in there to use it.

How can I force the system to ask the passphrase every time?

Old versions of GnuPG uses the gpg-agent, which caches the passphrase for a given time. Use the option --no-use-agent or add a line no-use-agent to ~/.gnupg/gpg.conf to prevent using the agent.

For newer versions (v2.1+), disable password caching for the agent by creating ~/.gnupg/gpg-agent.conf and adding the following lines:

default-cache-ttl 1
max-cache-ttl 1

Restart the agent with:

echo RELOADAGENT | gpg-connect-agent
| improve this answer | |
  • 10
    Note that for gpg2 "--no-use-agent" is an obsolete option - it has no effect. – 10 cls Nov 19 '16 at 17:36
  • 4
    0 seems to mean no caching at all, as it correctly asks for passphrase even immediately after encrypting. I just wanted to add that the correct way to reload gpg-agent would be echo RELOADAGENT | gpg-connect-agent – Jeffrey Lebowski Jan 10 '17 at 13:01
  • 8
    It might be noteworthy that gpg-agent also caches passphrases used with gpg --symmetric, which allows anyone with access to the shell / gpg-agent-session to decrypt the file without entering the passphrase. – fbmd Jan 14 '17 at 11:33
  • 1
    GnuPG does not read a system-wide configuration file, so you need to roll out the configuration for all existing users and create it when creating new users (skeleton folder on Linux, don't know how to do so in Windows). – Jens Erat Aug 24 '18 at 6:37
  • 2
    Well this is annoying. Is there any option that can be passed to the gpg command to prevent caching? – user76284 Jul 30 '19 at 2:53
4

GnuPG 2.2.15

  --symmetric
          -c  Encrypt with a symmetric cipher using a passphrase. The default sym-
          metric cipher used is AES-128, but may be chosen with the  --cipher-algo
          option.  This command may be combined with --sign (for a signed and sym-
          metrically encrypted message), --encrypt (for  a  message  that  may  be
          decrypted  via  a  secret  key or a passphrase), or --sign and --encrypt
          together (for a signed message that may be decrypted via a secret key or
          a  passphrase).  gpg caches the passphrase used for symmetric encryption
          so that a decrypt operation may not require that the user needs to enter
          the  passphrase.   The  option  --no-symkey-cache can be used to disable
          this feature.
# encrypt files
gpg -c --no-symkey-cache file.txt
# decrypt files
gpg --no-symkey-cache file.txt.gpg

with --no-symkey-cache option, it will not cache your password

| improve this answer | |
  • This works, but if I switch to another user, like root, it doesn't work--I get the error "gpg: problem with the agent: Permission denied" BUT there is a solution, "pinentry mode" gpg --pinentry-mode=loopback test.gpg askubuntu.com/a/1158297/429995 – PJ Brunet Apr 26 at 8:03
0

For gpg version 2.2.4 the following work using symmetric and adding --batch to the gpg command:

Therefore, to clear the password stored in the session, we can run:

echo RELOADAGENT | gpg-connect-agent

More info in this tutorial: https://www.baeldung.com/linux/encrypt-decrypt-files

| improve this answer | |
0

Encrypting a "test" file should give us test.gpg

# gpg -c test

But no, we get some errors.

gpg: problem with the agent: Permission denied
gpg: error creating passphrase: Operation cancelled
gpg: symmetric encryption of 'test' failed: Operation cancelled

Loopback mode to the rescue!

# gpg -c --pinentry-mode=loopback test

It prompts for your password and works as expected.

When it comes time to decrypt, maybe you change users and get an error:

gpg: problem with the agent: Permission denied

Loopback mode to the rescue!

# gpg --pinentry-mode=loopback test.gpg

Tested with...

gpg (GnuPG) 2.2.20
libgcrypt 1.8.5

IMO --pinentry-mode=loopback and --no-symkey-cache should be the default settings. I found the solution here https://askubuntu.com/a/1158297/429995 and the "anonymous" answer here (to not cache the password) was helpful as well.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.