如何使用vim快速加密和解密文件
譯文【51CTO.com快譯】每當您在Linux系統中有一個想要保持私密的文本文件,無論系統中擁有帳戶的其他用戶可能具有什么特權,您都可以借助加密來實現。一種簡單的方法是使用vim編輯器中內置的功能。您需要提供一個密碼,然后記住該密碼或將其存儲在密碼保險箱中,該過程非常簡單。文件名無法更改,恢復文件內容的方式與加密方式幾乎相同。
首先,假設我們有一個以這樣開頭的文件:
- $ head -3 mysecret
- I feel the need to put my deepest darkest secret into a text file on my Linux
- system. While this likely isn't common practice, I'm not sure that I can trust
- anyone with it. But a penguin? That's a different story! So here goes ...
現在,如果不想讓其他用戶知道您的私密信息,可以使用帶-x(加密)選項的vim。
- $ vim -x mysecret
vim編輯器將立即索要加密密鑰。您將輸入兩次密碼。請注意,密碼輸入時不會顯示,而是每個字符將顯示為星號。
- Enter encryption key: *********
- Enter same key again: *********
一旦vim打開了文件,它看起來很正常,您可以繼續編輯詳細信息或添加到您的私密信息中——如果您想這么做,也可以寫出加密形式的文件。
想寫出加密的內容,只需像平常使用vim那樣保存文件即可。
- :wq
隨后試圖查看該文件的任何人都可能會看到以下內容:
- VimCrypt~036▒!y)K▒▒i▒▒▒▒▒{▒z▒▒▒D▒:▒▒7▒\▒蠅Xd▒#n▒▒▒ڎq4▒▒▒^9▒|▒▒▒+A▒]j▒▒▒a▒N▒▒
- ▒▒▒▒▒▒}▒▒&f▒▒A3▒Wt[▒T\:с▒أny▒*▒▒}▒▒▒▒▒"▒▒▒ڈ^▒C▒E▒W▒▒v▒pV▒_▒Cj͞.EA▒▒▒#▒ex▒:▒K▒▒`P
- ▒u▒ ▒▒yhK▒X▒▒(W▒s(RY▒A▒
- ▒▒l9▒▒▒_▒▒▒▒▒I▒▒Lk▒ ▒k▒▒▒▒=▒5G▒▒▒t▒2Ӣ▒gF▒ 3▒Iq▒C▒▒▒▒OZ[▒l▒_▒~▒▒z
一旦您準備好再次讀取文件或繼續詳細表述私密信息,請再次使用vim命令,并在系統出現提示時輸入密碼。
- $ vim mysecret
- Need encryption key for "mysecret"
- Enter encryption key: *********
內容會再次以純文本顯示。
- I feel the need to put my deepest darkest secret into a text file on my Linux
- system. While this likely isn't common practice, I'm not sure that I can trust
- anyone with it. But a penguin? That's a different story! So here goes ...
用通常的:wq結束vim會話,文件將保持加密狀態。
如果您準備在某個時候與他人共享您的私密信息,可以像當初調用它那樣解密文件。首先使用vim -X命令。注意這回使用大寫的X:
- $ vim -X mysecret
- Need encryption key for "mysecret"
- Enter encryption key: *********
隨后您會看到原始文本。
- I feel the need to put my deepest darkest secret into a text file on my Linux
- system. While this likely isn't common practice, I'm not sure that I can trust
- anyone with it. But a penguin? That's a different story! So here goes ...
然后輸入:X,但是當系統提示您再次輸入加密密鑰(兩次)時,只需按回車鍵:
- Enter encryption key:
- Enter same key again:
使用:wq再次寫出文件。之后,您的文件將恢復為未加密形式。
- $ head -3 mysecret
- I feel the need to put my deepest darkest secret into a text file on my Linux
- system. While this likely isn't common practice, I'm not sure that I can trust
- anyone with it. But a penguin? That's a different story! So here goes ...
更多的選擇
還有許多其他工具可用于加密文件,但是這種方法只需要vim和用于記住密鑰的任何方法。要確定某個文件是否被vim加密,可以運行file命令。在下面的示例中,我們看到該命令告訴您文件何時加密以及何時未加密。
- $ file mysecret
- mysecret: Vim encrypted file data
- $ file mysecret
- mysecret: UTF-8 Unicode text
原文標題:Using vim to quickly encrypt and decrypt files,作者:Sandra Henry-Stocker
【51CTO譯稿,合作站點轉載請注明原文譯者和出處為51CTO.com】