From my mailbag:
Q. How do I password protect files?
Linux and other Unixish oses offers strong file permissions and ACL (access control list) concept in Linux/UNIX computer security used to enforce privilege separation.
However, none of them offers a password to protect files. You can use GNU gpg (GNU Privacy Guard) encryption and signing tool. It is a suite of cryptographic software. Many new UNIX/Linux users get confused with this fact.
Solution is to use following commands to encrypt or decrypt files with a password.
- Use GNU gpg command
- Use mcrypt command
- Use openssl command
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
Encrypt data.txt file:
$ 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:
A new file is created with the extension .nc i.e. data.txt.nc:
$ ls data.txt.nc
$ cat data.txt.nc
Decrypt the data.txt.nc file:
$ mcrypt -d data.txt.nc
Output:
Enter passphrase: File data.txt.nc was decrypted.
Verify that file was decrypted:
$ ls data.txt
$ cat data.txt
For mcrypt to be compatible with the Solaris des, the following parameters are needed:
$ mcrypt -a des --keymode pkdes --bare -noiv data.txt
Delete the input file if the whole process of encryption/decryption succeeds (pass -u option):
$ mcrypt -u data.txt
OR
$ mcrypt -u -d data.txt.nc
openssl command
OpenSSL is a cryptography toolkit implementing the Secure Sockets Layer (SSL v2/v3) and Transport Layer Security (TLS v1) network protocols and related cryptography standards required by them. You can use the openssl program which is a command line tool for using the various cryptography functions of OpenSSL's crypto library from the shell. It can be used for encrypt and decrypt files with a password:
Examples:
Encrypt file.txt to file.out using 256-bit AES in CBC mode
$ openssl enc -aes-256-cbc -salt -in file.txt -out file.out
Decrypt encrypted file file.out
$ openssl enc -d -aes-256-cbc -in file.out
Where,
- enc : Encoding with Ciphers.
See also:
- Use of GNU gpg command to encrypt and decrypt files with a password
- OpenSSL Command-Line HOWTO
- Please consult man pages of mcrypt, openssl and gpg for more information
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop




![Important: Openssl Security Update [CVE-2008-5077]](http://s13.cyberciti.org/images/shared/rp/3/15.jpg)






{ 2 comments… read them below or add one }
to delete source file the option is
mcrypt -u [filename]
not –u.
This is not shwon in the options section of the man pages but is right up front in the description of the command.
On decrypt command, with openssl you forgot to put the ‘out’ file.
Thanks! Great tip!
Thiago