linux下文件加密操作记录

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

一、 ZIP加密
1)文件加密
使用命令"zip -e filename.zip filename" 即可出现输入密码的提示,输入2次密码。 此文件即被加密解压时候是需要密码的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
下面开始为 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次密码。 此文件即被加密解压时候是需要密码的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
下面开始对目录进行加密
[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只能对文件进行加密,对目录则无法完成加密!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
下面开始使用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 !!!
***************当你发现自己的才华撑不起野心时,就请安静下来学习吧***************

本文转自散尽浮华博客园博客,原文链接:http://www.cnblogs.com/kevingrace/p/8194784.html,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值