While creating text files using the “vi” editor we can make the files secure by encrypting the files with our own encryption key. Any encrypted file will be unreadable unless the coorect key is not known.
Let us say we have a file by the name temp:
$ cat temp This is test file to be used for encryption using vi editor in linux. We should not be able to read the contents of the file untill we do not know the encryption key.
To encrypt this file, so that no one with out the encryption keys is able to read or write from the file, open the file using vi editor
$ vi temp
Now go to command mode by typing “esc”
Then type :X and press enter.
Note the “X” is in upper case
VI will now prompt for an encryption key as shown below.
Enter any key, and press enter
It will prompt for confirmation of the key, enter the same key again .
To find out whether the file is encrypted or not we can use the command file
$file temp temp: Vim encrypted file data
Thus indicating that it is an encrypted file. If we try to read from the file using cat
The output will be junk value. If we try to open the file using “vi” editor, it will prompt us for the encryption key.
In case we entered the wrong key, the file will open but with garbage values in it, and it will be in read only mode.
Only on entering the correct key, the file be visible in the original form
To remove the encryption, open the file using “vi” editor by entering the correct key and go to the command mode. Use the command
And press enter. It will prompt for a value of key, hit enter, do not enter any value for the key.
The encryption should have been removed now and we can edit the file as usual way.