Tuesday, 26 February 2013

How to encrypt & decrypt files


Many people want to keep some of the files on their system in encrypted format and for that people spend a lot of money on proprietory solutions for encryption or they find a trial version of any software which works fine initially but ultimately denies to decrypt the file. (After using such softwares it's obvious that encrytion gives a nightmare to people).
In this article I will describe a very simple utility by which you can encrypt your file & keep it on your system. There are many uses of encrypting a file but for a typical home users there are two main uses of file encryption.

1. Other users of the computer won't be able to access the file, only the person who have encrypted the file can decrypt it.
2. If you want to store some personal information or sensitive information (like your passwords or credit card numbers) online on some third party cloud storage utility then you can just encrypt the file & upload it. So next time you can download the file decrypt it & use it.

I personally use the encryption for second purpose. Linux provides an inbuilt GPG (GNU Privacy Guard) utility for encrypting the files. Here I will describe most basic usage of this utility however this utility has many advanced features too which I will be covering in next articles. So here are the steps to encrypt a file using gpg encryption.

1. Suppose I have a file named foo.txt & I want to encrypt it. (You may encrypt any type of file)
2. Run the following command on a terminal

gpg -c <filename>

Here give the absolute path of the filename in case you are working from some other directory.
So here I will give command as

gpg -c foo.txt

3. Now it will prompt you for a passphrase. Here provide a passphrase which only you know. It's like a password. This passphrase will be needed to decrypt your file.

Note: Remember the passphrase you provide at this step. Without this passphrase you won't be able to decrypt your file.

4. It will prompt to repeat the passphrase. Type it in again & Voila! You are done.

Now you will see a file foo.txt.gpg in the same directory. This is the encrypted file. Now you can delete the original file & keep this encrypted version of it. This file can only be read when it is decrypted with same gpg utility & with same passphrase which was used to encrypt it.

Steps to decrypt your file:

1. Go to the directory where file is located & run the command:

gpg <filename>.gpg (Provide filename with .gpg extension)

e. g. gpg foo.txt.gpg

2. It will ask for the passphrase. Provide the same passphrase as you used for encryption & press enter.
3. You will see original file foo.txt in the same directory, now you can normally open & read it.
Note: If the original file already exists then the gpg utility will ask you to replace the file. If you specify YES then it will replace the already existing file and if you specify NO then it will ask for a new file name and it will restore the file with new filename.

Do not use this encryption with Directories(Folders) as it will encrypt the folder but when you will try to decrypt it, It will return a file with a size of 0 Bytes.

There are advanced encryption techniques of GPG using a key pair which includes a public key & a private key. I will be covering the advanced features in my subsequent articles.
Note: If a file has an extension .asc then it's an ascii encrypted file and if a file has an extension .gpg then it's a binary encrypted file.



No comments:

Post a Comment