用 OpenSSL 加密/解密

除了是個 library 外,還提供了 CLI (command line interface) 操作,你可以透過 openssl 這隻程式對檔案加密解密。

先輸入:

$ openssl help

由於沒有 help 這個指令,所以會出現可用指令列表。在 Cipher commands 會列出很多可用的 cipher,我個人偏好用 Blowfish CBC (bf-cbc),主要原因是速度非常快。

加密的指令是:

$ openssl bf-cbc -in input.txt -out output.txt

輸入進去後就會問你要設定的密碼。而解開只要加上 -d,把 -in 改成加密後的檔案,-out 改成要輸出的檔案即可:

$ openssl bf-cbc -d -in output.txt -out input.txt.orig

然後輸入密碼解開,如果密碼錯誤會出現 bad decrypt 的錯誤訊息,接下來你可以用 cmp 或是 diff 比較檔案是不是一樣。

接下來講比較實用的部分。最近 提供的硬碟空間愈來愈大了,於是你可以拿 來備份 (不要放到 www 目錄下),但你又不想直接把 .tar.gz 直接丟上去,這時候你就可以在壓縮的時候順便加密:

$ tar -zc -f - /backup/this/directory | openssl bf-cbc -k password -out mybackup-20061114.gz-bf-cbc

其中 -k 後面接的就是 key,而不指定 -in 代表從 stdin 讀入。

如果你想要把檔案拆小一點,可以把 -out 拿掉 (表示從 stdout 輸出),再 pipe 給 split 切開。(也許切成 600MB 再燒成光碟?)

One thought on “用 OpenSSL 加密/解密”

  1. Pingback: Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *