Encrypted file in vim editor

I encrypted a file with a password in vim editor (using command :X). Next time when I open that file with a wrong password, it shows me the encrypted text, and if I bring some modification to that text and save it (:wq), the entire original human-readable text of the file get replaced with the encrypted text that it was displaying, which made the data in that file meaningless.

How does that make any sense? I mean since I have entered the wrong password, it shouldn't give me the write access to that file. What I think is, if an unauthorized user tries to modify the file, it should not allow that user to do so. Or am I missing something?

Hello,

Welcome to the forum ! We hope you enjoy your time here, and find this to be a friendly and helpful place.

I've never personally used the built-in encryption in vim (I didn't even know it was a thing to be honest, so thanks for the question !), but what you describe makes sense. When you encrypt a file, you're not preventing others from accessing its contents - you're just making its contents un-readable to anyone who doesn't have the encryption key.

So it would be entirely possible for a user who had write permission to an encrypted file to open that file in an editor in its raw encrypted form, add or remove whatever text they wanted (thus rendering it corrupt and useless), and then to save that over the original file. Encrypting a file doesn't stop people accessing it or using it - it just stops them being able to understand the contents of the file unless they have the ability to decrypt it by one means or another.

File access is ultimately determined by user and group ownerships and permissions on a UNIX-style system. Even if vim were written such that it did refuse to open a file that it recognised as one it had encrypted if the user entered the wrong password, that would still not offer any meaningful protection, as you could simply cat it to see the encrypted contents or cp it to a new file, open it in a different text editor entirely, or indeed do anything else your UNIX file permissions permitted you to do.

So if you want to prevent users accessing a file, encryption is perhaps only one part of the answer. The file also needs to have its permissions set such that only the users you want to be able to access the file are able to do so. Of course there's nothing you can do to stop the root user accessing the contents of your encrypted file, as by design on most UNIX-style systems the superuser account bypasses all file and directory security permissions. But by encrypting it, you make it impossible for someone to understand the contents, unless they have the encryption key as well.

Hope this helps to explain what you're seeing. If you have any further questions please let us know and we can take things from there.

Excellent reply as always @drysdalk

@_xor , see also:

LOL, here is "the answer" by ChatGTP. Reads like some of the answers we see here from time-to-time.

That does seem strange. When you open the file in Vim and enter the wrong password, it should not allow you to make any changes to the file or save it. It should only display the encrypted text, but not allow you to make any changes to it. If you are able to make changes to the file and save it even when you have entered the wrong password, then that would indicate that something is not working as expected.

It is possible that there could be some other factor at play here, such as a problem with the Vim configuration or the way that the file was encrypted. Without more information, it is difficult to say for sure what might be causing the issue.