To top page of uparrow

Encryption made easy #1
Using vi for encryption/decryption
Linux has a whole lot of tools to make your life simpler and safer.

You want to encrypt a text file and carry it with you safely. You want to be able to
decrypt it on all machines, even if they dont have special cryptographic software (like GPG). You
want to let your friends and colleagues decrypt the file, even if they do not have your
public key or know-how to use public key cryptography. Use the good old editor "vi" to 
help you (LFY October 2011, Tips and Tricks, by S. Sathyanarayanan). Here's how: 

Encrypt :  

Method #1 -- Open any document file, say foo, make modifs you may like to do, 
Then use  ESC :X to encrypt the file. Give password when prompted. 
You will need the password for decryption, so store it safely.
Save and quit with ESC : wq as usual. The original foo gets overwritten 
with the encrypted version.

Method #2 -- You can also encrypt by 
opening the file with vi using the -x option on the command line :: vi -x foo .  
Give password when prompted.
Use :wq and store the encrypted file. The original foo gets overwritten 
with the encrypted version.

Decrypt : To decrypt and get the plain text file foo. 

Open the file with vi, then give password. vi opens the encrypted file and shows 
the plain text in the vi window. When you write and close
using :wq it **saves the encrypted version only** and ** does not give the  plain
text file**.  

To recover the plain text (unencrypted version) file which you see in the vi window :

Open encrypted file foo with vi. Give password. Reset key to blank using
 ESC :set key= . Write and quit with :wq

Another way to get the decrypted plain text file after it is encrypted by vi: 

Open the encrypted file with vi. Give password. You see the decrypted plain text version. To save the 
plain text in a file again, encrypt  the file with :X . 
But, give blank password (hit ENTER twice). Save and quit :wq. You now have 
the decrypted plain text in your file.

Beware : The original (unencrypted) file always gets overwritten by this procedure.

Use the password and decrypt, if you want the original file again.

Since vi is a text editor, this method can be used for encrypting text files only.