Linux Encrypt Files/Decrypt Files – GPG Interactive/Non Interactive Modes - Comment Page: 1
GNU Privacy Guard (GnuPG or GPG) is a free software alternative to the PGP suite of cryptographic software. This is quick guide, howto Encrypt and Decrypt files on Linux with password using GPG.
This guide deals with both the interactive mode and the non interactive mode. Interactive mode is useful when the purpose is encrypt some files on the command line. Non interactive mode is useful when the purpose is encrypt files using scripts. This guide also deals with single file encryption/decryption and multiple files encryption/decryption simultaneously.
Linux Encrypt/Decrypt File on Linux using GPG
Encrypt File on Linux using GPG
Interactive Mode
gpg -c filename.txt
## OR...
gpg –yes –passphrase=$PASSPHRASE -c $OUTPUT_FILE
does still want manual passphrase.
Use
gpg –yes –batch –passphrase $PASSPHRASE -c $OUTPUT_FILE
instead. Look into the man pages for further details.
Hi,
Thanks for this information.
This is interesting, just tried this on Fedora and it works without –batch, but to make this work on all systems, I added –batch to this guide. Thanks!
I have run into problems using the above methods due to many distributions wrapping the GPG with a more secure password prompt (FreeBSD & Gentoo Linux both compile GPG with the pinentry package as a requirement).
This has served me well in these cases…
read -sp "Enter passphrase: " pass
echo "${pass}" | gpg --yes --no-tty --batch --passphrase-fd 0 -o /path/to/cipher/file.ext.gpg /path/to/plaintext/file.ext
Or to decrypt…
read -sp "Enter passphrase: " pass
echo "${pass}" | gpg --yes --no-tty --batch --passphrase-fd 0 -o /path/to/decrypted/file.ext /path/to/encrypted/file.ext.gpg
Hope that helps some others that run into this
Interesting, thanks for sharing jas.
The Non Interactive Mode doesn’t work in Ubuntu
A most useful page, thanks !
Also worth noting : the suggestion above to use “–passphrase-fd 0” is a good one, since it prevents the passphrase being visible if someone else on the host runs “ps” to list the processes.
Actually, to make that true, run via open2 / open3 in perl, so you can write to the command’s STDIN