Linux下利用openssl對檔案進行加密和解密

2021-06-19 21:16:18 字數 3750 閱讀 9863

--建立檔案test.txt, 特意寫入中英文

# cd /tmp

# echo "test測試" > test.txt

--開始加密, 使用aes-128-cbc演算法, 也可以使用其他演算法, 通過檢視openssl的幫助可獲知

# openssl aes-128-cbc -salt -in test.txt -out test.txt.aes

enter aes-128-cbc encryption password:《輸入密碼》

verifying - enter aes-128-cbc encryption password:《確認密碼》

--檢視加密前後的檔案大小, 加密後檔案明顯增大了

# ll test.txt*  

-rw-r--r--  1 root root  9 aug 11 15:42 test.txt

-rw-r--r--  1 root root 32 aug 11 15:43 test.txt.aes

--檢視加密前後的檔案內容, 加密後檔案無法直接檢視, 顯示亂碼

# cat test.txt

test測試

# cat test.txt.aes

salted__碾rtqm6棚顱

--現在開始解密, 會提示輸入密碼, 如果密碼有誤則無法解密

# openssl aes-128-cbc -d -salt -in test.txt.aes -out test.txt.out

enter aes-128-cbc decryption password:《輸入錯誤密碼》

bad decrypt

6150:error:06065064:digital envelope routines:evp_decryptfinal:bad decrypt:evp_enc.c:438:

# openssl aes-128-cbc -d -salt -in test.txt.aes -out test.txt.out

enter aes-128-cbc decryption password:《輸入正確密碼》

--檢視解密前後的檔案大小, 和加密前是一樣的

# ll test.txt*

-rw-r--r--  1 root root  9 aug 11 15:42 test.txt

-rw-r--r--  1 root root 32 aug 11 15:43 test.txt.aes

-rw-r--r--  1 root root  9 aug 11 15:45 test.txt.out

--檢視解密前後的檔案內容, 和加密前是一樣的

# cat test.txt.out

test測試

這種方法非常適合linux下的檔案內容保密, 呵呵....以上命令加引數比較複雜, 我們可以把命令加引數做個函式, 然後放到.bash_profile裡, 這樣每次登陸後直接使用函式即可, 如下:

function encryption() 

function decryption() 

然後就可以如下使用了(注意輸入引數都是原檔名, 且會自動刪除原檔案):

# encryption test.txt

enter aes-128-cbc encryption password:

verifying - enter aes-128-cbc encryption password:

# decryption test.txt

enter aes-128-cbc decryption password:

# ll test.txt*  

-rw-r--r--  1 root root 9 aug 11 15:46 test.txt

-rw-r--r--  1 root root 9 aug 11 15:45 test.txt.out

--end--

openssl進行rsa加密解密:

rsa是乙個非對稱加密演算法。簡單地說,非對稱加密演算法就是說加密解密乙個檔案需要兩個金鑰,乙個用來加密,為公鑰,乙個用來解密,為私鑰。證書可以用來授權公鑰的使用。

介紹下linux平台下openssl工具的簡單使用:

1、生成乙個金鑰:

openssl  genrsa  -out  test.key  1024

這裡-out指定生成檔案。需要注意的是這個檔案包含了公鑰和私鑰兩部分,也就是說這個檔案既可用來加密也可用來解密。後面的1024是生成金鑰的長度。

2、openssl可以將這個檔案中的公鑰提取出來:

openssl  rsa  -in  test.key  -putout  -out  test_pub.key

-in指定輸入檔案,-out指定提取生成公鑰的檔名。至此,我們手上就有了乙個公鑰,乙個私鑰(包含公鑰)。現在可以用公鈅來加密檔案了。

openssl  rsautl  -encrypt  -in  hello  -inkey  test_pub.key  -pubin  -out  hello.en

-in指定要加密的檔案,-inkey指定金鑰,-pubin表明是用純公鑰檔案加密,-out為加密後的檔案。

4、解密檔案:

openssl rsautl -decrypt -in hello.en -inkey test.key -out hello.de
-in指定被加密的檔案,-inkey指定私鑰檔案,-out為解密後的檔案。

openssl命令列工具驗證數字簽名:

一、傳送方a:

生成私鑰:

openssl> genrsa -passout pass:123456 -out apri.pem 1024

生成公鑰:

openssl> rsa -passin pass:123456 -pubout -in apri.pem -out apub.pem

用b的公鑰加密資料:

openssl> rsautl -encrypt -pubin -inkey bpub.pem -in data.txt -out edata.txt

計算資料的訊息摘要:

openssl> dgst -sha1 -out md.txt data.txt

用a的私鑰給訊息摘要簽名:

openssl> rsautl -sign -inkey apri.pem -in md.txt -out signature.bin

將edata.txt和signature.bin傳送給接收方b

二、接收方b

生成私鑰:

openssl> genrsa -passout pass:654321 -out bpri.pem 1024

生成公鑰:

openssl> rsa -passin pass:654321 -pubout -in bpri.pem -out bpub.pem

用b的私鑰解密資料:

openssl> rsautl -decrypt -inkey bpri.pem -in edata.txt -out data.txt

計算data.txt的資訊摘要:

openssl> dgst -sha1 -out ms2.txt data.txt

用a的公鑰解密數字簽名:

openssl> rsautl -verify -pubin -inkey apub.pem -in signature.bin -out ms3.txt

最後比較:ms2.txt 和ms3.txt內容完全相同:

sha1(data.txt)= ad6910d33d5f96cbd7b9b3378107b8b04ba1c138

Linux下編譯安裝openssl

wget 2 解壓壓縮包,例如 解壓到當前資料夾 tar zcvf openssl 1.0.1c.tar.gz c 解壓完後會生成openssl資料夾,如 openssl 1.0.1c。3 進入該資料夾,開啟install 檔案,可以看到安裝的具體步驟,按照這些步驟一步步做下來,就完成了openss...

Linux下原始碼安裝OpenSSL

開發環境為ubuntu 12.04,預設安裝了openssl了,可以通過 which openssl來檢視安裝位置,但是在 usr include等目錄下都找不到openssl相關的標頭檔案,所以需要從原始碼重新安裝openssl。wget tar zxvf openssl 1.0.0l.tar.g...

linux下使用openssl加密檔案

1.加密乙個檔案 root fxvsystem root openssl enc des e a in install.log out install.log.des enter des cbc encryption password verifying enter des cbc encrypti...