6

I've created and symmetrically encrypted a file using GPG:

touch test.txt && echo 'test' >> test.txt
gpg --output test.txt --symmetric test.txt

But now I cannot figure out how to unencrypt it, and surprisingly, I cannot find an example online. Here's what I've tried:

$ gpg --decrypt test.txt
gpg: AES encrypted data
gpg: encrypted with 1 passphrase
$ gpg --symmetric --decrypt test.txt
gpg: conflicting commands
$ gpg --passphrase --decrypt test.txt
gpg: no valid OpenPGP data found.
gpg: decrypt_message failed: Unknown system error
$ gpg --decrypt --output test_decrypted.txt test.txt
gpg: no valid OpenPGP data found.
gpg: decrypt_message failed: Unknown system error

What am I doing wrong?

jds
  • 974
  • 2
  • 10
  • 13
  • Did you actually look at the decrypted file? The first version above seems to work for me with a bug in the information text, should say it's a description of the original file. – Samuel Åslund May 05 '22 at 13:47

2 Answers2

3

The correct command is

gpg --decrypt test.txt

but gpg overwrites its output before reading its input so your test.txt’s original contents were lost.

You need to encrypt to a different file:

gpg --output test.gpg --symmetric test.txt
Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
1

The usage of the --symmetric seems confusingly asymmetric:

  • You must use --symmetric for encrypting, but then
  • Use --decrypt for decrypting.

Moreover, you output the result into a different file.

A complete reproducible example (uses batch-mode, prompts nothing):

#!/bin/bash

echo "Some important content" > a.txt
[ -f a.txt.gpg ] && rm a.txt.gpg
[ -f b.txt ] && rm b.txt

echo "secret" | gpg --batch --passphrase-fd 0 --output a.txt.gpg --symmetric a.txt
echo "secret" | gpg --batch --passphrase-fd 0 --output b.txt --decrypt a.txt.gpg

echo "------------------------- a.txt"
cat a.txt

echo "------------------------- b.txt"
cat b.txt

diff a.txt b.txt && echo "Files have the same content"