如何使用vim快速加密和解密文件

译文
开发 前端
大多数Linux用户都知道vim是vi派生而来的文本编辑器,它还可以用作加密文本文件的工具。本文将介绍如何使用它来加密和解密文件。

[[379438]]

【51CTO.com快译】每当您在Linux系统中有一个想要保持私密的文本文件,无论系统中拥有帐户的其他用户可能具有什么特权,您都可以借助加密来实现。一种简单的方法是使用vim编辑器中内置的功能。您需要提供一个密码,然后记住该密码或将其存储在密码保险箱中,该过程非常简单。文件名无法更改,恢复文件内容的方式与加密方式几乎相同。

首先,假设我们有一个以这样开头的文件:

  1. $ head -3 mysecret 
  2. I feel the need to put my deepest darkest secret into a text file on my Linux 
  3. system. While this likely isn't common practice, I'not sure that I can trust 
  4. anyone with it. But a penguin? That's a different story! So here goes ... 

现在,如果不想让其他用户知道您的私密信息,可以使用带-x(加密)选项的vim。

  1. $ vim -x mysecret 

vim编辑器将立即索要加密密钥。您将输入两次密码。请注意,密码输入时不会显示,而是每个字符将显示为星号。

  1. Enter encryption key: ********* 
  2. Enter same key again: ********* 

一旦vim打开了文件,它看起来很正常,您可以继续编辑详细信息或添加到您的私密信息中——如果您想这么做,也可以写出加密形式的文件。

想写出加密的内容,只需像平常使用vim那样保存文件即可。

  1. :wq 

随后试图查看该文件的任何人都可能会看到以下内容:

  1. VimCrypt~036▒!y)K▒▒i▒▒▒▒▒{▒z▒▒▒D▒:▒▒7▒\▒蝇Xd▒#n▒▒▒ڎq4▒▒݋▒^9▒|▒▒▒+A▒]j▒▒▒a▒N▒▒ 
  2.  
  3. ▒▒▒▒▒▒}▒▒&f▒▒A3▒Wt[▒T\:с▒أny▒*▒▒}▒▒▒▒▒"▒▒▒ڈ^▒C▒E▒W▒▒v▒pV▒_▒Cj͞.EA▒▒▒#▒ex▒:▒K▒▒`P 
  4.  
  5. ▒u▒ ▒▒yhK▒X▒▒(W▒s(RY▒A▒ 
  6.  
  7. ▒▒l9▒▒▒_▒▒▒▒▒I▒▒Lk▒ ▒k▒▒▒▒=▒5G▒▒▒t▒2Ӣ▒gF▒ 3▒Iq▒C▒▒▒▒OZ[▒l▒_▒~▒▒z 

一旦您准备好再次读取文件或继续详细表述私密信息,请再次使用vim命令,并在系统出现提示时输入密码。

  1. $ vim mysecret 
  2. Need encryption key for "mysecret"  
  3. Enter encryption key: ********* 

内容会再次以纯文本显示。

  1. I feel the need to put my deepest darkest secret into a text file on my Linux  
  2. system. While this likely isn't common practice, I'not sure that I can trust  
  3. anyone with it. But a penguin? That's a different story! So here goes ... 

用通常的:wq结束vim会话,文件将保持加密状态。

如果您准备在某个时候与他人共享您的私密信息,可以像当初调用它那样解密文件。首先使用vim -X命令。注意这回使用大写的X:

  1. $ vim -X mysecret 
  2. Need encryption key for "mysecret" 
  3. Enter encryption key: ********* 

随后您会看到原始文本。

  1. I feel the need to put my deepest darkest secret into a text file on my Linux  
  2. system. While this likely isn't common practice, I'not sure that I can trust  
  3. anyone with it. But a penguin? That's a different story! So here goes ... 

然后输入:X,但是当系统提示您再次输入加密密钥(两次)时,只需按回车键:

  1. Enter encryption key
  2. Enter same key again: 

使用:wq再次写出文件。之后,您的文件将恢复为未加密形式。

  1. $ head -3 mysecret 
  2. I feel the need to put my deepest darkest secret into a text file on my Linux  
  3. system. While this likely isn't common practice, I'not sure that I can trust  
  4. anyone with it. But a penguin? That's a different story! So here goes ... 

更多的选择

还有许多其他工具可用于加密文件,但是这种方法只需要vim和用于记住密钥的任何方法。要确定某个文件是否被vim加密,可以运行file命令。在下面的示例中,我们看到该命令告诉您文件何时加密以及何时未加密。

  1. $ file mysecret  
  2. mysecret: Vim encrypted file data  
  3. $ file mysecret  
  4. mysecret: UTF-8 Unicode text 

原文标题:Using vim to quickly encrypt and decrypt files,作者:Sandra Henry-Stocker

【51CTO译稿,合作站点转载请注明原文译者和出处为51CTO.com】

 

责任编辑:华轩 来源: 51CTO
相关推荐

2021-05-08 05:56:15

加密OpenSSL密钥

2015-05-19 08:58:08

加密解密GnuPG

2022-09-26 08:35:53

磁盘Java解密

2023-09-01 09:31:48

2015-03-26 14:19:53

GPG加密解密

2021-07-18 11:43:58

Linux密码加密

2024-03-01 09:58:44

2023-09-04 14:00:28

加密密钥私钥

2023-12-13 12:27:46

2017-12-07 10:25:55

LinuxGPG加密解密

2015-03-26 11:25:10

对称加密加密压缩加密解密解压

2018-08-28 10:40:08

Windows 10EFS加密文件

2018-05-11 14:59:21

LinuxVim编辑器

2022-11-07 07:04:25

2023-03-06 08:49:02

加密和解密SpringBoot

2010-07-06 10:35:59

2018-04-23 13:10:01

2020-09-24 10:50:53

加密解密语言hmac

2013-11-11 11:12:17

EFS加密

2024-01-26 16:32:08

点赞
收藏

51CTO技术栈公众号