Linux下利用openssl對檔案進行加密和解密
阿新 • • 發佈:2019-01-04
--建立檔案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裡, 這樣每次登陸後直接使用函式即可, 如下:
然後就可以如下使用了(注意輸入引數都是原檔名, 且會自動刪除原檔案):
# jiami test.txt
enter aes-128-cbc encryption password:
Verifying - enter aes-128-cbc encryption password:
# jiemi 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--
# 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:<輸入正確密碼>
--檢視解密前後的檔案大小, 和加密前是一樣的
-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 jiami()
{
/usr/bin/opensslaes-128-cbc -salt -in $1-out $1.aes && rm -f $1
}
function jiemi()
{
/usr/bin/opensslaes-128-cbc -d -salt -in $1.aes-out $1 && rm -f $1.aes
}
然後就可以如下使用了(注意輸入引數都是原檔名, 且會自動刪除原檔案):
# jiami test.txt
enter aes-128-cbc encryption password:
Verifying - enter aes-128-cbc encryption password:
# jiemi 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--