標籤:style http color 使用 io strong 檔案 art
轉自:http://netsecurity.51cto.com/art/201301/378513.htm
1.簡介
OpenSSL是一款功能強大的加密工具包。我們當中許多人已經在使用OpenSSL,用於建立RSA私匙或認證簽章要求(CSR)。不過,你可曾 知道可以使用OpenSSL來測試電腦速度?或者還可以用它來對檔案或訊息進行加密?本文將介紹幾個簡單易學的技巧,教你如何使用OpenSSL對訊息 和檔案進行加密。
【相關推薦】:網路安全工具百寶箱
2.對訊息進行加密和解密
首先,我們不妨對簡單的訊息進行加密。下面這個命令將使用Base64編碼方法(Base64 Encoding),對"Welcome to LinuxCareer.com"(歡迎訪問LinuxCaeer.com)這個訊息進行加密:
$ echo "OpenSSL" | openssl enc -aes-256-cbc > openssl.dat
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
$ file openssl.dat
openssl.dat: data
想對openssl.dat檔案進行解密,恢複至原始訊息,請使用:
$ openssl enc -aes-256-cbc -d -in openssl.dat
enter aes-256-cbc decryption password:
OpenSSL
3.對檔案進行加密和解密
想使用OpenSSL對檔案進行加密,其實就跟對訊息進行加密一樣簡單。唯一的區別在於,我們不是使用echo命令,而是使用-in選項,後面跟以我們想進行加密的實際檔案,並使用-out選項,這會指令OpenSSL將經過加密的檔案儲存體到某個名稱的檔案中:
$ openssl enc -aes-256-cbc -in /etc/services -out services.dat
想對我們的服務檔案進行解密,恢複成原樣,請使用:
$ openssl enc -aes-256-cbc -d -in services.dat > services.txt
enter aes-256-cbc decryption password:
4.對目錄進行加密和解密
萬一你需要使用OpenSSL對整個目錄進行加密,首先需要建立gzip打包檔案(tarball),然後用上述方法對該打包檔案進行加密,也可以使用pipe,同時完成這兩項任務:
# tar cz /etc | openssl enc -aes-256-cbc -out etc.tar.gz.dat
tar: Removing leading `/‘ from member names
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
想對整個etc/目錄進行解密,並提取到當前的工作目錄,請使用:
# openssl enc -aes-256-cbc -d -in etc.tar.gz.dat | tar xz
enter aes-256-cbc decryption password:
上述方法對自動備份加密目錄來說相當有用。
5.小結
大家剛才看到的只是OpenSSL加密方面的基本介紹。說到OpenSSL這款加密工具包,它其實沒有限制你能用它來做什麼,可以說用途廣泛。想看看如何使用不同的編碼方法,請參閱OpenSSL手冊頁:
$ man openssl