Linux: How to Encrypt and decrypt files with a password
To encrypt and decrypt files with a password, use gpg command. It is an encryption and signing tool for Linux/UNIX like operating system such as FreeBSD/Solaris.
GnuPG stands for GNU Privacy Guard and is GNU's tool for secure communication and data storage. It can be used to encrypt data and to create digital signatures. It includes an advanced key management facility.
Encrypting a file in linux
To encrypt single file, use command gpg as follows:
$ gpg -c filename
To encrypt myfinancial.info file, type the command:
$ gpg -c myfinancial.info
Output:
Enter passphrase:<YOUR-PASSWORD> Repeat passphrase:<YOUR-PASSWORD>
This will create a myfinancial.info.gpg file.
Option:
- -c : Encrypt with symmetric cipher.
Caution if you ever forgot your password aka passphrase, you cannot recover the data as it use very strong encryption.
Task: decrypt file
To decrypt file use gpg command:
$ gpg myfinancial.info.gpg
Output:
gpg myfinancial.info.gpg gpg: CAST5 encrypted data Enter passphrase:<YOUR-PASSWORD>
Decrypt file and write output to file vivek.info.txt you can run command:
$ gpg myfinancial.info.gpg –o vivek.info.txt
Remember if file extension is .asc, it is a ASCII encrypted file and if file extension is .gpg, it is a binary encrypted file.
See also:
- GnuPG Frequently Asked Questions
- Consult man page of gpg
Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
You may also be interested in other helpful articles:
- Linux or UNIX password protect files
- Linux > How to encrypting a files for particular user (recipients aka your trusted friend)
- How to secure the data in your PHP applications with PHP encryption
- Best Linux / UNIX Posts of 2007: Part ~ III
- WIFI network is here. But how safe is it?
Discussion on This Article:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: decrypt_files, digital_signatures, encrypt, encryption, gnupg, gnu_privacy_guard, gpg, gpg_command, key_management, Linux, passphrase, secure_communication, UNIX


Thanks! This helped me a lot. Encryption everywhere seems to be only about e-mail. I just wanted to encrypt a password file.
This came very handy…and is now bookmarked! Thanks.
This is fine as far as it goes. However, if you decrypt a file and look at it using a word processor (say), the word processor might well leave back-up copies of your sensitive file in /tmp or in a back-up directory. So when you delete your unencrypted file, you have to delete any other copies that might be lying around the filing system too. A better way around this problem might be to use whole partition encryption: http://encryptionhowto.sourceforge.net/Encryption-HOWTO-4.html
But even then, you need to watch out for stuff that may be on a different disk/partition, e.g. in /tmp.
David,
Good point about backup files. Thanks for sharing howto links.
Appreciate your post!
Great post.
I need to encript a backup file in non interactive mode, so I do not have a prompt to type the password, because is a automated script.
Do you have how I can proceed?
Tkx in advance.
Sirs,
can i encrypt squid.conf or httpd.conf that are located on /etc/xxx folder… knowing that squid process and apache process need to access this file in order to read the file configuration! if this is not workable in my case… is there a way to hide the .conf files from even the root users.. please your help is highly appreciated…
greetings from lebanon
Regards,
i want to encrypt/hide .conf. your help is highly appreciated…