为了安全考虑,通常会对一些重要文件进行加密备份或加密保存,下面对linux下的文件加密方法做一简单总结:

gzexe加解密

加密:

[root@test~]# gzexe a.txt
a.txt:    22.2%
[root@test~]# ls
a.txt  a.txt~ ls

解密:

[root@test~]# gzexe -d a.txt
[root@test~]# ls
a.txt  a.txt~ ls


gzexe方法会把原来没有加密的文件a.txt备份为a.txt~ ,同时a.txt文件变成了加密文件(即变成了密文),而解密也一样会将之前的加密文件变成a.txt~

用tar命令 对文件加解密

加密:

[root@test~]# tar -zcf - test.txt | openssl des3 -salt -k 123@123 | dd of=test.des3
0+1 records in
0+1 records out
152 bytes (152 B) copied, 0.00333366 s, 45.6 kB/s
[root@test~]# ls
test.txt  test.des3

也可以将/mnt目录下的所有文件全部加密压缩
[root@test3 ~]# tar -zcf - /mnt/* |openssl des3 -salt -k 123@123 | dd of=test.des3

对test.txt文件进行加密压缩,生成test.des3加密压缩文件,123@123为加密的密码

解密:

[root@test~]# dd if=test.txt.des3 |openssl des3 -d -k 123@123 | tar zxf -
0+1 records in
0+1 records out
152 bytes (152 B) copied, 4.5873e-05 s, 3.3 MB/s
[root@ipsan-node03 ~]# ls
test.txt  test.des3

注意命令最后面的"-",它将释放所有文件,-k 123@123可以没有,没有时在解压时会提示输入密码

结合Tar和OpenSSL给文件和目录加解密

当有重要的敏感数据的时候,给文件和目录额外加一层保护是至关重要的,特别是当需要通过网络与他人传输数据的时候。基于这个原因,可以用到tar(Linux 的一个压缩打包工具)和OpenSSL来解决的方案。借助这两个工具,你真的可以毫不费力地创建和加密 tar 归档文件。

现在要加密当前工作目录的内容(根据文件的大小,这可能需要一点时间)

[root@test mnt]# tar -czf - * | openssl enc -e -aes256 -out test.tar.gz
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
上述命令的解释:
enc 使用加密进行编码
-e 用来加密输入文件的 enc 命令选项,这里是指前一个 tar 命令的输出
-aes256 加密用的算法
-out 用于指定输出文件名的 enc 命令选项,这里文件名是test.tar.gz
[root@test mnt]# ls
a.txt  b.txt  c.txt  test.tar.gz
[root@test mnt]# rm -rf a.txt
[root@test mnt]# rm -rf b.txt
[root@test mnt]# rm -rf c.txt
[root@test mnt]# ls
test.tar.gz

对于上面加密后的tar包直接解压肯定是不行的,会报一下错误

[root@test mnt]# tar -zvxf test.tar.gz
gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now

要解密上述tar归档内容,需要使用以下命令

[root@test mnt]# openssl enc -d -aes256 -in test.tar.gz | tar xz -C /mnt/
enter aes-256-cbc decryption password:
[root@test mnt]# ls
a.txt  b.txt  c.txt  test.tar.gz
上述命令的解释:
-d 用于解密文件
-C 将加压后的文件提取到目标目录下

当你在本地网络或因特网工作的时候,你可以随时通过加密来保护你和他人共享的重要文本或文件,这有助于降低将其暴露给恶意攻击者的风险。

shc加密

仅仅对shell脚本加密(sch只能能shell脚本文件进行加密,其他文件都不可以)

shc是一个专业的加密shell脚本的工具.它的作用是把shell脚本转换为一个可执行的二进制文件,这个办法很好的解决了脚本中含有IP、密码等不希望公开的问题.
如果你的shell脚本包含了敏感的口令或者其它重要信息,而且你不希望用户通过ps -ef(查看系统每个进程的状态)捕获敏感信息,你可以使用shc工具来给shell脚本增加一层额外的安全保护,shc是一个脚本编译工具,使用RC4加密算法,它能够把shell程序转换成二进制可执行文件(支持静态链接和动态链接),该工具能够很好的支持: 需要加密, 解密, 或者通过命令参数传递口令的环境.

shc的官网下载地址: http://www.datsi.fi.upm.es/~frosal/sources/

安装方法:

[root@test ~]# cd /usr/local/src/
[root@test src]# wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9.tgz
[root@test src]# tar -zvxf shc-3.8.9.tgz
[root@test src]# cd shc-3.8.9
[root@test shc-3.8.9]# mkdir -p /usr/local/man/man1

这步是必须的,不然安装过程中会报错,shc将安装命令到/usr/local/bin/目录下;
将帮助文档存放在/usr/local/man/man1/目录下,如果系统中无此目录,安装时会报错,可创建此目录后再执行安装

[root@test shc-3.8.9]# make install

这是要回答yes或者y,不能直接回车,否则会报错

加密:

[root@test ~]# shc -r -f text.sh
[root@test ~]# ls
text.sh  text.sh.x  text.sh.x.c


运行后会生成两个文件,script-name.x 和 script-name.x.c
script-name.x是加密后的可执行的二进制文件.
./script-name.x 即可运行.
script-name.x.c是生成script-name.x的原文件(c语言)
从安全角度考虑,使用sch命令对shell脚本文件进行加密后,只需保留.x的二进制文件即可,其他两个文件均可以删除!

shc还提供了一种设定有效执行期限的方法,可以首先使用shc将shell程序转化为二进制,并加上过期时间

[root@test ~]# shc -e 28/02/2018 -m "this script file is about to expire" -v -r -f text.sh
shc shll=bash
shc [-i]=-c
shc [-x]=exec '%s' "$@"
shc [-l]=
shc opts=
shc: cc  text.sh.x.c -o text.sh.x
shc: strip text.sh.x
shc: chmod go-r text.sh.x
[root@test ~]# ls
text.sh  text.sh.x  text.sh.x.c
上述命令的解释:
-e:指定过期时间为2018年2月28日
-m:过期后打印出的信息;
-v: verbose
-r: 可在相同操作系统的不同主机上执行
-f: 指定源shell

如果在过期后执行,则会有如下提示:

[root@test ~]# ./text.sh.x
./text.sh.x: this script file is about to expire

使用以上方法要注意,需防止用户更改系统时间,可以通过在程序中加入自动更新系统时间的命令来解决此问题!!

ZIP加密

加密:

[root@test ~]# zip -e test.txt.zip test.txt
Enter password:                          
Verify password:
adding: test.txt (stored 0%)
[root@test ~]# ls
test.txt  test.txt.zip

解密:

[root@test ~]# unzip test.txt.zip
Archive:  test.txt.zip
[test.txt.zip] test.txt password:
extracting: test.txt  

如需要对文件夹进行加密,加-r 即可递归加密文件夹

GnuPG加密

gpg只能对文件进行加密,对目录则无法完成加密

GnuPG的全称是GNU隐私保护(GNU Privacy Guard),常常被称为GPG,它结合了一组加密软件,它是由GNU项目用C编程语言编写的,最新的稳定版本是2.0.27,在如今的大多数Linux发行版中,gnupg程序包都是默认随带的,所以万一它没有安装,你可以使用yum install gnupg从软件库来安装它。

加密:

[root@test ~]# gpg -c test.txt   
can't connect to `/root/.gnupg/S.gpg-agent': No such file or directory    //这个信息可以忽略
[root@test ~]# ll test.txt*
-rw-r--r--. 1 root root 18 Jan  4 10:08 test.txt
-rw-r--r--. 1 root root 61 Jan  4 10:04 test.txt.gpg

解密:

[root@test ~]# gpg test.txt.gpg  
gpg: 3DES encrypted data
can't connect to `/root/.gnupg/S.gpg-agent': No such file or directory
gpg: encrypted with 1 passphrase
gpg: WARNING: message was not integrity protected

出现Paraphrase提示时,需要提供加密时输入的同一个密码才能解密

最后修改:2019 年 09 月 30 日
-