# Encrypted file in vim editor

**URL:** <https://community.unix.com/t/encrypted-file-in-vim-editor/389358>\
**Category:** Linux and Unix-Like\
**Created:** [December 28, 2022, 5:30am UTC](https://community.unix.com/t/encrypted-file-in-vim-editor/389358 "2022-12-28T05:30:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_xor](https://community.unix.com/letter_avatar/_xor/32/6_22734026db12803ebb3e059622ac3534.png) [@\_xor](https://community.unix.com/u/_xor)\
**Post date:** [December 28, 2022, 5:30am UTC](https://community.unix.com/t/encrypted-file-in-vim-editor/389358/1 "2022-12-28T05:30:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![drysdalk](https://community.unix.com/letter_avatar/drysdalk/32/6_22734026db12803ebb3e059622ac3534.png) [@drysdalk](https://community.unix.com/u/drysdalk)\
**Post date:** [December 28, 2022, 10:02am UTC](https://community.unix.com/t/encrypted-file-in-vim-editor/389358/2 "2022-12-28T10:02:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [December 29, 2022, 5:26am UTC](https://community.unix.com/t/encrypted-file-in-vim-editor/389358/3 "2022-12-29T05:26:01Z")

</div>

Excellent reply as always @drysdalk

@_xor , see also:

> **[Using the vim editor in Linux to quickly encrypt and decrypt files](https://www.networkworld.com/article/3604676/using-vim-to-quickly-encrypt-and-decrypt-files.html)**
>
> Most Linux users know vim as a text editor that descended from vi. It can also function as a tool for encrypting text files. In this post, we examine how this is done and how to reverse the process.

---

<div class="post-metadata">

**Author:** ![chatgpt](https://community.unix.com/user_avatar/community.unix.com/chatgpt/32/19766_2.png) [@chatgpt](https://community.unix.com/u/chatgpt)\
**Post date:** [January 4, 2023, 3:03am UTC](https://community.unix.com/t/encrypted-file-in-vim-editor/389358/4 "2023-01-04T03:03:35Z")

</div>

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.

> [@Replying to Questions Using ChatGPT](https://community.unix.com/t/replying-to-questions-using-chatgpt/389421):
>
> I have just provided over 10 examples of using [ChatGPT](https://chat.openai.com/chat) to answer questions. All members can do the same, but please state / reference that you are using ChatGPT if you do so. We may create a (future) rule for this, I think (have not discussed with the team yet). In the meantime, anyone who posts a reply using ChatGPT should say so. FWIW, some of the replies I have read in 2022 seem to have originated from ChatGPT, TBH... Thanks: Examples Using ChatGPT ....

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [January 11, 2023, 3:03am UTC](https://community.unix.com/t/encrypted-file-in-vim-editor/389358/5 "2023-01-11T03:03:44Z")

</div>

Closed on January 11, 2023, 3:03am UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
