Tuesday, 10 May 2011

Quick tip: How to encrypt files in linux

We can easily encrypt files ( whether it is text file,audio or video file) in linux using the command line program called gpg.

To encrypt file:

To encrypt files using gpg, open the terminal and type the following syntax:
  • gpg -c /path to file/filename
gpg is the program we are using to encrypt the file. -c switch is used to create the encrypted file. '/path to file' is the path where the file is located and '/filename' is the name of the file to be encrypted.


Eg: gpg -c /home/thegeekylinux/Downloads/test.txt

Here the path is /home/thegeekylinux/Downloads and the file name is test.txt. The file test.text is located at Downloads folder in my home directory.

It will ask you to enter a passphrase which you will need later to decrypt the file. Re-enter the passphrase and your file will be encrypted.

You can see that a new file called test.txt.gpg has been created on the same folder which is the encrypted file. Now you can delete the original file (in this case test.txt).

To decrypt the encrypted file:

You need to decrypt the encrypted file to use it. In order to decrypt the file you need to enter the passphrase you have given while encrypting it.

We can use the following syntax to decrypt the file.
  • gpg -d /path to file/file name > /path where you want to save file/new name you want to use to save the file
We are using the '-d' switch to decrypt the file.  'path to file' is the location where your encrypted file is located. 'file name' is the name of the encrypted file. 'path where you want to save file' is the location where you want to save the decrypted file. 'new name you want to use to save file' is the name you will be using to save the file. See below example to get the correct idea.

Eg: gpg -d /home/thegeekylinux/Downloads/test.txt.gpg > /home/thegeekylinux/Downloads/test.txt

Here the encrypted file is located at my (thegeekylinux) Downloads folder and the name of the file is test.txt.gpg (see the .gpg extension). I want to save the decrypted file in the same folder itself (Downloads folder in my home directory)  as its original name which is test.txt.

After entering the command for decryption, it will ask for the passphrase and if you have given it correctly, it will decrypt the file and save at the location you have specified. Once the process is completed, see the location and you can see the decrypted file test.txt and encrypted file test.txt.gpg.

Now you can open the test.txt file using the text editor program of your choice. You can use the same method for encrypting and decrypting video files,audio file, images etc.

See the below video created by Leszek Lesner about the file encryption in linux.



                                                                                       via [lubuntu screencast]






No comments:

Post a Comment