--建立文件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:<输入正确密码>
--查看解密前后的文件大小, 和加密前是一样的
# 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 jiami()
{
/usr/bin/openssl aes-128-cbc -salt -in $1 -out $1.aes && rm -f $1
}
function jiemi()
{
/usr/bin/openssl aes-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--