在Linux環境下使用OpenSSL對訊息和檔案進行加密(轉載)

來源:互聯網
上載者:User

標籤: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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.