为了安全考虑,通常会对一些重要文件进行加密备份或加密保存,下面对linux下的文件加密方法做一简单介绍:
一、 ZIP加密 1)文件加密 使用命令"zip -e filename.zip filename" 即可出现输入密码的提示,输入2次密码。 此文件即被加密解压时候是需要密码的
下面开始为test.txt文件进行加密
[root@centos6-vm02 ~]# cat test.txt
this is a test!!!
[root@centos6-vm02 ~]# zip -e test.txt.zip test.txt //如下进行加密操作时,需要输入两次密码
Enter password:
Verify password:
adding: test.txt (stored 0%)
[root@centos6-vm02 ~]# ls
test.txt test.txt.zip
进行解压的时候,需要输入密码
[root@centos6-vm02 ~]# rm -f test.txt
[root@centos6-vm02 ~]# unzip test.txt.zip
Archive: test.txt.zip
[test.txt.zip] test.txt password:
extracting: test.txt
[root@centos6-vm02 ~]# cat test.txt
this is a test!!!
2)文件夹加密 使用命令"zip -re dirname.zip dirname"即可出现输入密码的提示,输入2次密码。 此文件即被加密解压时候是需要密码的。
下面开始对目录进行加密
[root@centos6-vm02 ~]# mkdir dirtest
[root@centos6-vm02 ~]# cat dirtest/haha.txt
this is test of dir!!!
[root@centos6-vm02 ~]# zip -re dirtest.zip dirtest
Enter password:
Verify password:
adding: dirtest/ (stored 0%)
adding: dirtest/haha.txt (stored 0%)
解压目录时需要输入密码
[root@centos6-vm02 ~]# rm -rf dirtest
[root@centos6-vm02 ~]# unzip dirtest.zip
Archive: dirtest.zip
creating: dirtest/
[dirtest.zip] dirtest/haha.txt password:
extracting: dirtest/haha.txt
[root@centos6-vm02 ~]# ls dirtest
haha.txt
[root@centos6-vm02 ~]# cat dirtest/haha.txt
this is test of dir!!!
二、GnuPG加密 GnuPG的全称是GNU隐私保护(GNU Privacy Guard),常常被称为GPG,它结合了一组加密软件。它是由GNU项目用C编程语言编写的。最新的稳定版本是2.0.27。在如今的大多数Linux发行版中,gnupg程序包都是默认随带的,所以万一它没有安装,你可以使用apt或yum从软件库来安装它(yum install gnupg)。注意:gpg只能对文件进行加密,对目录则无法完成加密!
下面开始使用GnuPG方式对test.txt文件进行加密
[root@centos6-vm02 ~]# cat test.txt
this is a test!!!
[root@centos6-vm02 ~]# gpg -c test.txt
can't connect to `/root/.gnupg/S.gpg-agent': No such file or directory //这个信息可以忽略
注意:如上加密的时候,会要求Paraphrase输入两次密码,对这个特定的文件进行加密。
一旦运行带-c选项(完全使用对称密码算法加密)的gpc命令,它会生成一个文件.gpg文件。
[root@centos6-vm02 ~]# 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@centos6-vm02 ~]# rm -f test.txt
文件解密操作。
注意出现Paraphrase提示时,需要提供加密时输入的同一个密码才能解密
[root@centos6-vm02 ~]# 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
[root@centos6-vm02 ~]# 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@centos6-vm02 ~]# cat test.txt
this is a test!!!