Security - Encrypt and decrypt files with GPG
Simon Scholzcreated 2022-01-09updated 2024-01-202 min readFeedback / Requests?

Securing your files with gpg

GPG or GnuPG is an open source software that let's you encrypt your files, which should be protected.

Please visit https://gnupg.org/ for more information.

Encrypt single file

gpg -cv --no-symkey-cache desired-file
gpg command parameter
-cencryption only with symmetric cipher
-vverbose (get more output)
--no-symkey-cachedo NOT cache the password

Then you´ll be promted to enter a password. This will result in a desired-file.gpg file, which can be decrypted again as follows:

gpg --no-symkey-cache desired-file.gpg
gpg command parameter
--no-symkey-cachedo NOT cache the password

In case you´d omit the --no-symkey-cache flag the given password will be cached in the gpg-agent and you won´t be promted for the password when decrypting the file.

Create an encrypted archive

tar -cvzf - desired-directory | gpg -cv > encrypted-archive.tar.gz.gpg

This command basically creates a tar file containing the files in the desired-directory and due to - this tar will be piped to the gpg command. This gpg command will then take the piped tar and encrypt it and write the result into encrypted-archive.tar.gz.gpg.

tar command parameter
-cIs used to create an archive
-vverbose (get more output)
-zCompress the file for smaller size
-fSpecifies that the file name will be mentioned next
gpg command parameter
-cencryption only with symmetric cipher
-vverbose (get more output)

Decrypt and decompress a gpg protected file

gpg -dv encrypted-archive.tar.gz.gpg | tar -xvzf -

gpg -dv encrypted-archive.tar.gz.gpg will cause a prompt, which asks for the password and then extracts the archive.

gpg command parameter
-dstands for decrypt
-vverbose (get more output)
tar command parameter
-xExtract the archive
-vverbose (get more output)
-zDecompress the archive
-fWhere to put the file/files