Using vim editor in linux to encrypt and decrypt files

Chinthaka Weerakkody
3 min readJun 17, 2021

Most linux users recognize vim [1] as a text editor. In this article we are going to explore on how we can use vim editor to encrypt and decrypt text files.

When you have a text file and if you need to keep the contents of the text file private regardless of the privileges of the user on the system, you can use encryption.

One of the methods of achieving this is using the built in support of vim editor. Encryption with vim editor requires a secret key where the same will have to be provided if the file needs to be decrypted properly. Hence you have to remember the secret key you enter during encryption process.

Let’s get the hands on experience on this. To begin with let’s assume we have a file named ‘secret-stuff’ with following content.

$ head secret-stuff
This content is confidential. Access to this information should be restricted to other users. So we need to protect this document with a secret key

Now assuming the given file contains sensitive information where the users in the system don’t need to have access to, we can use one of the following two approaches to encrypt the file with vim editor.

  1. Passing encryption switch while opening the file with vim

We can use vim with it’s -x(encryption) switch to encrypt the file. We have to open the file with the -x switch.

$ vim -x secret-stuff

When you enter the above command it will immediately prompt the user to enter an encryption key(secret key). Once you provide the encryption key and press enter,it will request the user to enter the same secret key again.

Enter encryption key: ********
Enter same key again: ********

After successfully providing the same secret key for the second time, the file will be opened and the original text of the file will be visible. Now if you wish to edit the content of the file, you can continue to do so.

Saving the file is mandatory for the encryption process to be effected. You have to press Esc key once and then press :wq to save the file and exit.

:wq

2. Encrypt file after opening the file with vim

In this approach we first open the file using vim editor.

$ vim secret-stuff

Now the file will be opened in the view mode. You can edit the file if you need and then switch back to the view mode by pressing Esc button once. To entrypt the file you have to type below command in view mode and press enter.

:X

This will request you to provide a secret key. After providing the secret key press enter. Then again it will ask you to enter the same secret key again.

Enter encryption key: ********
Enter same key again: ********

After providing the secret key in the above steps press enter. You will be switched back to the view mode of the file. Now you have to save the file for the encryption process to be effected. Press :wq to save the file and exit.

:wq

After encryption if you need to view the file content, you can open it using vim editor.

$ vim secret-stuff

It will request for the secret key to decrypt the file.

Need encryption key for "secret-stuff"
Enter encryption key:

If you provide the correct secret key, the document will be displayed with decrypted information. If you provide an incorrect secret key, a document with junk characters will be displayed as below.

VimCrypt~03! hù\»¨Ü<<98>v0^XS^H'<97><8d>õ<97>1O
÷ÌÇóðaV<80>Σy^[m{¿A<9a>{g aÌtjÓ4~òáóU¤ý·`^õ<97>oÊ^Dx~5Im¢^U«Ô<93>;®u^TÒQ1<93>Ü<<8e>@ålLòÛÝ<96>«Î^N^Y<82>îd<9e>N0w$y¶4Ï¡w^WUiRf©<8e>µ<94>^OGÓK^Vª7E%"Ëh<86>¼<93>lä«/ÑÏ<90>JÕÇmEÏ8Y­UDLÀÉ^BJ^_}n^VÀbÄ

References

[1] “vim editor”, Vim.org. [Online]. Available: https://www.vim.org. [Accessed: 04- May- 2021].

--

--