How to encrypt a file or directory in Linux?

Solution 1:

I think it would be GnuPG. The syntax for files and directories differs though.

Encryption

For files (outputs filename.gpg):

gpg -c filename

For directories:

gpg-zip -c -o file.gpg dirname

Decryption

For files (outputs filename.gpg):

gpg filename.gpg

For directories:

gpg-zip -d file.gpg

Deprecation Update

It seems gpg-zip command is deprecated in recent versions. Instead, either use gpgtar command, or compress the directory (e.g. convert it to a tarball) and then encrypt it as a file.

Edit: Corrected as @Mk12 pointed out the mistake of compression/decompression for encryption/decryption.

Solution 2:

  • with openssl

openssl des3 -salt -in unencrypted-data.tar -out encrypted-data.tar.des3

Decrypt:

openssl des3 -d -salt -in encrypted-data.tar.des3 -out unencrypted-data.tar

  • encrypt with AES

aescrypt -e -p password file.jpg

Decrypt:

aescrypt -d -p password file.jpg.aes

Solution 3:

This is my method using openssl and tar

Open Encrypted Directory:

openssl enc -aes-256-cbc -d -in ~/vault.tar.gz.dat | tar xz; thunar ~/vault

Lock Encrypted Directory:

tar cz vault/ | openssl enc -aes-256-cbc -out ~/vault.tar.gz.dat; rm -r ~/vault