If you store private information on your Linux system and you want to prevent other people who use the system from viewing your private files, you need to password protect these files. Solution is to use following commands to encrypt or decrypt files with a password.

gpg command

GnuPG is the GNU project’s complete and free implementation of the OpenPGP standard as defined by RFC4880. GnuPG allows to encrypt and sign your data and communication, features a versatile key management system as well as access modules for all kinds of public key directories. GnuPG, also known as GPG, is a command line tool with features for easy integration with other applications.

To encrypt a file, use command gpg as follows:

$ gpg -c filename

Example, to encrypt private.txt file, type the command

$ gpg -c private.txt

Output

Enter passphrase:
Repeat passphrase:

Where,
-c : Encrypt with symmetric cipher.

To decrypt file use gpg command

$ gpg private.txt.gpg

Output:

gpg private.txt.gpg
gpg: CAST5 encrypted data
Enter passphrase:

mcrypt command

Mcrypt is a simple crypting program, a replacement for the old unix crypt. When encrypting or decrypting a file, a new file is created with the extension .nc and mode 0600. The new file keeps the modification date of the original. The original file may be deleted by specifying the -u parameter.

Examples, to encrypt data.txt file, type the following command

$ mcrypt data.txt

Output:

Enter the passphrase (maximum of 512 characters)
Please use a combination of upper and lower case letters and numbers.
Enter passphrase:
Enter passphrase:

Note that a new file is created with the extension .nc.

To decrypt the data.txt.nc file, enter

$ mcrypt -d data.txt.nc

Output:

Enter passphrase:
File data.txt.nc was decrypted.