前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux下文件加密操作记录

linux下文件加密操作记录

作者头像
洗尽了浮华
发布2018-01-23 15:59:52
2.9K0
发布2018-01-23 15:59:52
举报
文章被收录于专栏:散尽浮华散尽浮华

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

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

代码语言:javascript
复制
下面开始为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次密码。 此文件即被加密解压时候是需要密码的。

代码语言:javascript
复制
下面开始对目录进行加密
[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只能对文件进行加密,对目录则无法完成加密!

代码语言:javascript
复制
下面开始使用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!!!
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-01-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档