In this tutorial, you are going to learn how to encrypt and decrypt files with Vim
editor on Linux systems. Securing data with encryption is of paramount importance in many aspects of today’s System Administration. Encryption keeps criminals and spies from stealing very important or highly classified information.
Checking whether Vim is installed, installing it and more usage guide in your system, use the link below;
Edit Text Files with Vim in Linux
Encrypt and Decrypt Files with Vim Editor
Encrypting Files
Having your secret file classified.txt you want to secure with encryption using vim
editor, use -x
option with vim
to encrypt your file.
Using cat
command let’s check the contents of classified.txt file;
cat classified.txt
The /etc/ipsec.secrets file => The file ipsec.secrets stores the secrets used by the pluto daemon to authenticate communication between both gateways. Two different kinds of secrets can be configured in this file, which are preshared secrets and RSA private keys.
Now, let’s encrypt our file classified.txt with vim
;
vim -x classified.txt
Enter encryption key: ******
Enter same key again: ******
When you run the above command, you will be prompted to enter encryption key twice for confirmation as per the output above. The key is displayed in an asterisk format (*
) as you type it. Once you hit ENTER
key vim
will open the file and it will allow you to edit the file i.e adding more secrets.
Save and exit the file using the following command in vim
;
:wq
If you tried to display the contents of classified file using cat command, it will display as follows meaning that the file is encrypted;
cat classified.txt
VimCrypt~03!�������9����A~���QvR�Ծ�
q�[��gKs�U&��c\�w�K$��L#�Q1ʠ����\�f��q�� HWV�&jb����,��"�P#\4
Xv��J�ȟ�&���!�`���˱"� }�
[+"sQ���O~j�
H5�'&'�`��t����Q鶇����1uzr�8��Ao������zo$>�<i�¾N�8j�R�k����s6�7��vM��,M���=|��,��YҌ�["1?�����.�5�썤�`�f�Ҟ��R�x%
To open a this file classified.txt again for editing or reading the contents, use vim
. This time round you prompted to enter the encryption key(password
) which you set during first time encryption of the file.
vim classified.txt
Need encryption key for "classified.txt"
Enter encryption key: ******
The contents will be displayed plainly in vim
.
The /etc/ipsec.secrets file => The file ipsec.secrets stores the secrets used by the pluto daemon to authenticate communication between both gateways. Two different kinds of secrets can be configured in this file, which are preshared secrets and RSA private keys.
~
~
Type :wq
to safe and exit the file and it will remain encrypted.
Vim contains swap file which can interfere with the security of encrypted files. Use -n
option with vim
to turn off this feature when encrypting the files.
vim -xn classified.txt
Similarly, if you have already opened the file, you can encrypt it within vim, simply press escape and enter :X to encrypt the file.
Decrypting Files
Now once you want everybody to open and see what is contained in the file classified.txt you can decrypt the file. First open the file with -X
option (upper case
), you will be asked for encryption key (password
).
vim -X classified.txt
Need encryption key for "classified.txt"
Enter encryption key: ******
Once you provide the correct password the file will open in vim
;
The /etc/ipsec.secrets file => The file ipsec.secrets stores the secrets used by the pluto daemon to authenticate communication between both gateways. Two different kinds of secrets can be configured in this file, which are preshared secrets and RSA private keys.
Now, decrypt the file. Before you save and exit, type :X
and hit ENTER
key when prompted to enter the encryption key leave it blank
and hit the ENTER
key twice.
Enter encryption key:
Enter same key again:
Save and exit the file with :wq
command. This will leave the file decrypted.
You can confirm by using cat
command to view the contents of the file.
cat classified.txt
The /etc/ipsec.secrets file => The file ipsec.secrets stores the secrets used by the pluto daemon to authenticate communication between both gateways. Two different kinds of secrets can be configured in this file, which are preshared secrets and RSA private key.
The contents are displayed normally.
This marks the end of our tutorial on how to use vim to encrypt and decrypt files on Linux. Drop a comment if you have other suggestion.
You can also check;
Install Request Tracker (RT) on Debian 10