引用自:http://blog.chinaunix.net/u/19895/showart_232861.html

Linux关于文件加密的两种方法和详解:

一、用GnuPG加密文件。

GnuPG软件包(Gnu Privacy Guard,Gnu隐私保镖),软件包的名称是gpg。

gpg在加密文件时使用的是公共密钥加密方法。

1.第一步是要创建一个将来用来发送加密数据和进行解密数据的密钥。我们执行一下gpg命令,就会在你的主目录下创建一个.gnupg子目录。

(如果它不存在的话,有时已经存在了)。在该子目录里面有一个gpg.conf的配置文件,它里面是gpg工具的各种配置选项及其默认设置值。

接下来,我们来进行第一项,生成密钥:

[root@fxvsystem root]# gpg --gen-key                         >>这个命令生成密钥

gpg (GnuPG) 1.2.4; Copyright (C) 2003 Free Software Foundation, Inc.

This program comes with ABSOLUTELY NO WARRANTY.

This is free software, and you are welcome to redistribute it

under certain conditions. See the file COPYING for details.

Please select what kind of key you want:                    >>选择密钥类型

(1) DSA and ElGamal (default)

(2) DSA (sign only)

(4) RSA (sign only)

Your selection? 1

DSA keypair will have 1024 bits.

About to generate a new ELG-E keypair.                      >>选择密钥长度

minimum keysize is  768 bits

default keysize is 1024 bits

highest suggested keysize is 2048 bits

What keysize do you want? (1024) 768

Requested keysize is 768 bits

Please specify how long the key should be valid.            >>选择密钥有效期,0代表没有期限

0 = key does not expire

= key expires in n days

w = key expires in n weeks

m = key expires in n months

y = key expires in n years

Key is valid for? (0) 0

Key does not expire at all

Is this correct (y/n)? y                                     >>最后确认是否正确

You need a User-ID to identify your key; the software constructs the user id

from Real Name, Comment and Email Address in this form:

"Heinrich Heine (Der Dichter) "

Real name: test201                                           >>输入基本信息,真实名字

Email address: test201@test201.com                           >>输入邮件地址

Comment: this is 201 key                                     >>其他相关注释信息

You selected this USER-ID:

"test201 (this is 201 key) "

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O                >>确认OK

You need a Passphrase to protect your secret key.

Enter passphrase:                                            >>输入密钥口令

Repeat passphrase:

We need to generate a lot of random bytes. It is a good idea to perform

some other action (type on the keyboard, move the mouse, utilize the

disks) during the prime generation; this gives the random number

generator a better chance to gain enough entropy.

+++++.+++++++++++++++++++++++++++++++++++++++++++++.+++++..+++++++++++++++++++++++++++++++++++++++++++++.+++++..++++++++++.+++++++++++++++>.++++++++++...........................................................+++++

>>生成密钥过程会出现连续的这种符号。

Not enough random bytes available.  Please do some other work to give

the OS a chance to collect more entropy! (Need 215 more bytes)

We need to generate a lot of random bytes. It is a good idea to perform

some other action (type on the keyboard, move the mouse, utilize the

disks) during the prime generation; this gives the random number

generator a better chance to gain enough

entropy.

>>这段话表明提示我们在系统上创建一些随机的活动,如果没有足够的活动,他会停下来提示我们继续这样做。(比如查看一下cpu,随意敲打一

下键盘都可以)

++++++++++.+++++++++++++++.++++++++++++++++++++.++++++++++.+++++++++++++++++++++++++.+++++.+++++.+++++++++++++++.+++++.++++++++++++++++++++....>+++++..+++++^^^^^

gpg: /root/.gnupg/trustdb.gpg: trustdb created

public and secret key created and signed.

key marked as ultimately trusted.

pub  1024D/BA56DDDA 2007-01-16 test201 (this is 201 key)

这行里面的BA56DDDA是生成的公共密钥的标识,我们在后面还要使用,记住它吧。

Key fingerprint = 98E8 0A56 9E16 F61B 379D  2F53 D5DF 4117 BA56 DDDA

sub   768g/8F754496 2007-01-16

>>成功

[root@fxvsystem root]#

现在我们已经生成了一对密钥。查看.gnupg目录:

[root@fxvsystem root]# cd .gnupg/

[root@fxvsystem .gnupg]# ll

total 24

-rw-------  1 root root 8075 Jan 16 11:10 gpg.conf

-rw-------  1 root root  856 Jan 16 11:30 pubring.gpg                存放别人公共密钥的“钥匙环”文件。

-rw-------  1 root root    0 Jan 16 11:10 pubring.gpg~

-rw-------  1 root root  600 Jan 16 11:30 random_seed

-rw-------  1 root root  991 Jan 16 11:30 secring.gpg

-rw-------  1 root root 1240 Jan 16 11:30 trustdb.gpg

[root@fxvsystem .gnupg]#

又新生成了几个相关的文件。

2.为了把刚才生成的公共密钥发送给对方,我们需要先用命令把它提取出来:

[root@fxvsystem gpg]# gpg --armor --export BA56DDDA > 201.key        把公共密钥提取到文件201.key中。

其中:

--armor是让gpg生成ASCII格式的输出,这样适合电子邮件来发送。如果可以使用ssh等支持二进制文件传输的工具。可以不使用这个选项。

--export  就不用多说了,就是导出的意思。

3.在收到别人传过来的公共密钥后,需要把这个公共密钥放到“钥匙环”文件里:

比如,我们在另一台计算机上收到了刚才201.key这个公共密钥,然后我们执行:

[root@localhost gpg]# gpg --import 201.key

gpg: key BA56DDDA: public key "test201 (this is 201 key) " imported

gpg: Total number processed: 1

gpg:               imported: 1

通过这条命令,可以把刚才在201机器上生成的公共密钥导入到161机器的“钥匙环”文件(~/.gnupg/pubring.gpg)中。

可以通过gpg -kv命令查看161机器上当前存放多少个别人的公共密钥:

[root@localhost gpg]# gpg -kv

/root/.gnupg/pubring.gpg

------------------------

pub   1024D/1C05EC6B 2007-01-15

uid                  Paolo (this test destination 213)

sub   1024g/A16A8685 2007-01-15

pub   1024D/BC3AA97D 2007-01-15

uid                  Wangqi (test to 161)

sub   1024g/33A9764D 2007-01-15

pub   1024D/BA56DDDA 2007-01-16

uid                  test201 (this is 201 key)

sub    768g/8F754496 2007-01-16

[root@localhost gpg]#

4.接下来,我们在161机器上用201的公共密钥加密一个文件

[root@localhost ~]# gpg -ea -r BA56DDDA install.log               >>通过这个命令对install.log文件进行加密。

gpg: 8F754496: There is no assurance this key belongs to the named user

pub   768g/8F754496 2007-01-16 test201 (this is 201 key)

Primary key fingerprint: 98E8 0A56 9E16 F61B 379D  2F53 D5DF 4117 BA56 DDDA

Subkey fingerprint: DC76 48E6 70C0 CD36 F671  D2D3 AEC5 02A2 8F75 4496

It is NOT certain that the key belongs to the person named

in the user ID.  If you *really* know what you are doing,

you may answer the next question with yes.

Use this key anyway? (y/N) y

[root@localhost ~]# ls

-e   代表加密

-a   代表ASCII格式,如果不是通过电子邮件传输,可不加此参数

-r   后面是密钥的标识。可以使用多个-r参数,加多个标识,这样就可以把它发给多个需要该文件的人。

这个命令执行之后,在当前目录下查看,生成了一个同名的install.log.asc的文件,这个文件就是加密后的文件。

5.最后我们把install.log.asc文件传回到201机器上,进行解密查看:

[root@fxvsystem gpg]# ls

201.key  install.log.asc

[root@fxvsystem gpg]# gpg -o install.log -d install.log.asc                   >>这个命令进行解密,-o为输出到一个文件中,-d表示解密。

You need a passphrase to unlock the secret key for

user: "test201 (this is 201 key) "

768-bit ELG-E key, ID 8F754496, created 2007-01-16 (main key ID BA56DDDA)

gpg: encrypted with 768-bit ELG-E key, ID 8F754496, created 2007-01-16

"test201 (this is 201 key) "

[root@fxvsystem gpg]# ls

201.key  install.log  install.log.asc

[root@fxvsystem gpg]#

我们可以看到在当前目录下生成了一个install.log文件,这个文件就是解密后的文件,可以直接查看。

二、用openssl加密文件

openssl也可以进行文件的加密。方法比上面的gpg简单很多,没有创建密钥的过程,也没有相关的配置文件,只要执行一条命令就可以对文件进行加密。

把加密的文件传给需要的人后,只要他知道加密方式和加密口令,就可以解密查看文件。

openssl支持的加密算法很多,包括:bf,cast,des,des3,idea,rc2,rc5等及以上各种的变体,具体可参阅相关文档。

具体的方法如下:

1.加密一个文件:

[root@fxvsystem root]# openssl enc -des -e -a -in install.log -out install.log.des

enter des-cbc encryption password:

Verifying - enter des-cbc encryption password:

输入密码之后,就会生成install.log.des文件,这个文件名是自己指定的,可以随意写。

其中:

enc表明你打算使用某个算法

-des是具体使用的某个算法

-e 表明要加密

-a 同样是使用ASCII进行编码

-in  要加密的文件名字

-out 加密后的文件名字

把生成的文件传到另一台机器后,执行如下命令进行解密

[root@fxvsystem gpg]# openssl enc -des -d -a -in install.log.des -out install.log

enter des-cbc decryption password:

输入口令后,就可以得到解密后的文件了。

其中

-d表明要进行解密

linux系统日志文件夹加密,linux系统环境下,对文件进行加密相关推荐

  1. debian 文件夹中文件大小_Linux下查看文件和文件夹大小 删除日志

    场景:在sts中执行自动部署时候maven提示No space left on device错误,后来经检查发现是磁盘空间满了,用下面的方法分析发现tomcat下面的logs目录占用了很大的空间,删除 ...

  2. ftp 服务器新建文件夹,ftp服务器路径下新建文件夹

    ftp服务器路径下新建文件夹 内容精选 换一换 单击"源码迁移"后面的,打开如图1所示界面,参数描述如表1所示.配置参数后单击"开始分析".以下内容中的&quo ...

  3. 如何在ftp服务器下查找文件夹,查找ftp服务器下的文件夹名

    查找ftp服务器下的文件夹名 内容精选 换一换 Linux x86-64(64位)服务器,常见的有EulerOS.Ubuntu.Debian.CentOS.OpenSUSE等.Windows 7及以上 ...

  4. windows 技术篇 - cmd命令查看当前目录下的所有文件和文件夹以及所有子目录下的文件,dir命令的使用方法

    dir /b 可以查看当前路径下的所有文件和文件夹. dir /b /s 可以查看当前路径下的所有文件和文件夹以及子目录下的文件. 这是目录,直接在路径里输入 cmd 回车可以进入 cmd 的该路径下 ...

  5. linux创建目录无权限,linux下文件夹权限 linux 创建文件夹没有权限

    linux 给所有文件下文件加权限 更改文件用户组.使用命令chgrp,命令如下. 更改文件所有者.使用命令chown. chown命令可以同时修改所有者和用户组名称(即拥有chgrp的功能),命令如 ...

  6. linux复制文件夹全部文件夹里,Linux系统怎么复制文件夹下的全部文件到另外文件夹?...

    在linux系统中复制或拷贝文件我们可以用cp或者copy命令,但要对一个文件夹中的全部文件复制到另外一个文件夹中去,如何进行操作呢? 下面简单来介绍一下. copy命令 1.copy ,cp,该命令 ...

  7. Linux之不删除指定文件夹,其他都删(文件和文件夹)

    例子1 查看当前文件夹下有哪些文件和子文件夹 我们看到俩个文件夹 和多个文件,我现在的需求是 只保留 public 这个文件夹 其他的文件夹和文件 统统都删除,怎么办呢? 只要一个命令 ls | gr ...

  8. linux递归创建文件夹_Python中并发请求创建文件夹带来的线程安全问题

    在我们日常写程序的过程中,免不了与文件系统打交道,各种开发语言都提供了与文件系统交互的指令函数,比如Python中的os模块,就提供了许多这样的指令函数,今天就以创建文件夹为例,来说一下,如果用的不当 ...

  9. linux上创建一文件夹命令,linux创建文件夹命令是什么

    linux创建文件夹命令是mkdir,语法为[mkdir (选项)(参数)],其中Z为设置安全上下文,当使用SELinux时有效,version显示版本信息. 本文操作环境:linux2.6.32系统 ...

  10. Linux文件夹乱码,inode号处理文件

    1.删除该文件或者文件夹 如果乱码中有带数字或者带字母的可以,可以匹配删除 如果全为乱码 ll  -i1   第一列数字即该乱码文件或者文件夹的额inode号 find -inum inode号 |x ...

最新文章

  1. bzoj3514(LCT+主席树)
  2. Windows Server 2008 没有磁盘清理工具的情况下使用系统提供的磁盘清理工具
  3. Object+C语法快速入门
  4. java调用怎么调用方法区_Java中的方法调用有多昂贵
  5. Android研发中对String的思考(源码分析)
  6. 浅谈浏览器多进程与JS线程 1
  7. Java成神之路——CountDownLatch、CyclicBarrier
  8. 【专栏精选】实战:动态配置图片
  9. c++描述将一个2进制数转化成10进制数(用到初始化栈,进栈,入栈)
  10. 权限管理----用户与模块关系
  11. mqtt 推送的客户端测试方法
  12. 2022款Thinkphp家政上门预约系统-全开源系统源码
  13. CPU中寄存器作用及说明
  14. 【经验】通过JVM调优,让凯哥个人博客响应速度提升了不少
  15. FHQ Treap【基于P3369的讲解】【随机数、各数组、函数运用】
  16. QPixmap的尺寸设置
  17. 解决word在输入文字后公式自动矮的问题,同时实现公式自动居中,编号居右
  18. 【前端——vue】:过滤器、侦听器、计算属性、vue-cli、vue组件、动态组件、插槽、自定义属性、路由
  19. CainAbel工具的下载和使用
  20. 版权概念小结(音乐方面)

热门文章

  1. 006基于SSM小区物业系统
  2. 深搜+回溯+广搜小结
  3. 【SwinTransformer源码阅读二】Window Attention和Shifted Window Attention部分
  4. UTC时间格式转时间戳
  5. 怎样利用好量价选股?量价应用法则有哪些?
  6. 姚期智计算机科学实验班,清华大学成立量子信息班 姚期智院士领衔
  7. ENVI:如何对自带GLT表的图像进行几何校正?
  8. 软件测试常用词汇表(中英对照解释)
  9. 恒源云(GPUSHARE)_CV论文笔记之CE-FPN(通道增强特征金字塔网络)
  10. 针对金融行业的数据治理解决方案