Using vim to encrypt your documents.

By | July 31, 2014

vimThere are literally dozens of ways you can encrypt your text documents. Some are more secure than others, but this method, I find to be the most useful, because it’ uses my favorite text editor. Vim. This is just another method you can use if you find it as useful as I do. I’ve

Vim uses a dot file for it’s configuration, so we are going to create ANOTHER dot file, very similar, but slightly different than the normal one that you use for your default vim configuration. The default vim dot file is .vimrc, we are going to create a new one called .vimencrypt.

This file will be in your home directory. /home/$USER/.vimencrypt
It contains the following:

#--------------------
source ~/.vimrc
set nobackup
set noswapfile
set nowritebackup
set cm=blowfish
set viminfo=
#--------------------

The next thing we are going to do is to create a function for your .bashrc file. Previously I had just used an alias that looked like this (alias vime=”vim -u ~/.vimencrypt -x ″), but the function allows us to test if the file already exists, and if it does, it only asks for the password once to decrypt, instead of twice, like it did with the alias. So, the function is a bit more user friendly.

#--------------------
vime () { FILE="$1";
if [ -f "$FILE" ];
then
vim -u ~/.vimencrypt "$FILE"
else
vim -u ~/.vimencrypt -x "$FILE"
fi }
#--------------------

Put that function into your .bashrc file and refresh your .bashrc file with

. ~/.bashrc

The “vime” name I used for the function is short for “vim encrypt”.

Note: Since Vim 7.3 (2010-08-15)(and newer), vim can now use Blowfish encryption.
If your using an older vim — remove the “set cm=blowfish” line. It will then default to crypt for encryption.

I know, very complicated right ? That’s it folks, you are now READY to use encryption with vim!
Now, simply type “vime something.txt” and you’re on your way.

You will then see:

Enter encryption key:

Enter same key again:

If you entered the same key twice, then you should be presented with a normal vim interface.

If you use “vime” to encrypt the document the first time, it will stay encrypted.
You can then just use vim normally to edit it endlessly (vim will ask for your password, you only have to use “vime” on the initial creation of the document), and it’s encrypted when you close it.

Now you will see VimCrypt~02……. when you cat your encrypted file, and it’s encrypted with blowfish, a MUCH MUCH stronger encryption than just “crypt” which can be broken fairly easily.

> cat test
VimCrypt~02!sD~wLПYEx

I hope you have fun with vim and encryption.