如何使用vim快速加密和解密文件
譯文【51CTO.com快譯】每當(dāng)您在Linux系統(tǒng)中有一個(gè)想要保持私密的文本文件,無論系統(tǒng)中擁有帳戶的其他用戶可能具有什么特權(quán),您都可以借助加密來實(shí)現(xiàn)。一種簡單的方法是使用vim編輯器中內(nèi)置的功能。您需要提供一個(gè)密碼,然后記住該密碼或?qū)⑵浯鎯υ诿艽a保險(xiǎn)箱中,該過程非常簡單。文件名無法更改,恢復(fù)文件內(nèi)容的方式與加密方式幾乎相同。
首先,假設(shè)我們有一個(gè)以這樣開頭的文件:
- $ 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 ...
現(xiàn)在,如果不想讓其他用戶知道您的私密信息,可以使用帶-x(加密)選項(xiàng)的vim。
- $ vim -x mysecret
vim編輯器將立即索要加密密鑰。您將輸入兩次密碼。請注意,密碼輸入時(shí)不會顯示,而是每個(gè)字符將顯示為星號。
- Enter encryption key: *********
- Enter same key again: *********
一旦vim打開了文件,它看起來很正常,您可以繼續(xù)編輯詳細(xì)信息或添加到您的私密信息中——如果您想這么做,也可以寫出加密形式的文件。
想寫出加密的內(nèi)容,只需像平常使用vim那樣保存文件即可。
- :wq
隨后試圖查看該文件的任何人都可能會看到以下內(nèi)容:
- 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
一旦您準(zhǔn)備好再次讀取文件或繼續(xù)詳細(xì)表述私密信息,請?jiān)俅问褂胿im命令,并在系統(tǒng)出現(xiàn)提示時(shí)輸入密碼。
- $ vim mysecret
- Need encryption key for "mysecret"
- Enter encryption key: *********
內(nèi)容會再次以純文本顯示。
- 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結(jié)束vim會話,文件將保持加密狀態(tài)。
如果您準(zhǔn)備在某個(gè)時(shí)候與他人共享您的私密信息,可以像當(dāng)初調(diào)用它那樣解密文件。首先使用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,但是當(dāng)系統(tǒng)提示您再次輸入加密密鑰(兩次)時(shí),只需按回車鍵:
- Enter encryption key:
- Enter same key again:
使用:wq再次寫出文件。之后,您的文件將恢復(fù)為未加密形式。
- $ 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和用于記住密鑰的任何方法。要確定某個(gè)文件是否被vim加密,可以運(yùn)行file命令。在下面的示例中,我們看到該命令告訴您文件何時(shí)加密以及何時(shí)未加密。
- $ file mysecret
- mysecret: Vim encrypted file data
- $ file mysecret
- mysecret: UTF-8 Unicode text
原文標(biāo)題:Using vim to quickly encrypt and decrypt files,作者:Sandra Henry-Stocker
【51CTO譯稿,合作站點(diǎn)轉(zhuǎn)載請注明原文譯者和出處為51CTO.com】