GPG symmetric encryption and asymmetric encryption

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/ck784101777/article/details/102453754

First, the encryption / decryption Overview

1. The risk of transmission of information

  Data encryption is a long history of technology (such as Morse code during World War II), it refers to the encryption algorithm and encryption keys plaintext into ciphertext, and decrypted by the decryption algorithm and decryption key to restore the ciphertext in plain text. It is the core cryptography.

  Data encryption is still the most reliable way computer systems for information protection. It uses cryptography to encrypt information, information screen, which play a role to protect the security of information.

  Common application scenario is as follows. In the unusual process of data transmission (e-mail, login, chat conversations), we need to use encryption technology, since the data capture is very easy to do (hacking), if not encrypted, once get caught that is information leakage can cause serious damage.

2. The encryption / decryption process

As shown, the encrypted text information or character information into digital data encryption is called, typically read data will be read to the non-encrypted data

3. Press the encryption function and object division

  Divided by common action encryption technology into encrypted data storage, data transmission encryption, data integrity identification, secret key management techniques.

  Data storage encryption object is to prevent data stored in the link has been compromised, the encrypted data storage techniques can be divided encrypted data and access control of two. The former is generally achieved by means of encryption algorithm conversion, additional passwords, encryption modules, etc.; the latter is the user qualifications, rights to be reviewed and restrictions to prevent unauthorized users from accessing data or unauthorized access to legitimate user data.

  数据传输加密技术的目的是对传输中的数据流加密,通常有线路加密与端对端加密两种。线路加密侧重在线路上而不考虑信源与信宿,是对保密信息通过各线路采用不同的加密密钥提供安全保护。端对端加密指信息由发送端自动加密,并且由TCP/IP进行数据包封装,然后作为不可阅读和不可识别的数据穿过互联网,当这些信息到达目的地,将被自动重组、解密,而成为可读的数据。

  数据完整性鉴别技术的目的是对介入信息传送、存取和处理的人的身份和相关数据内容进行验证,一般包括口令、密钥、身份、数据等项的鉴别。系统通过对比验证对象输入的特征值是否符合预先设定的参数,实现对数据的安全保护。

  密钥管理技术包括密钥的产生、分配、保存、更换和销毁等各个环节上的保密措施。

4.加密算法主要有以下几种分类

1)为确保数据机密性算法:

a) 对称加密算法(AES,DES)

b) 非对称加密算法,又叫公开密钥加密(RSA,DSA)

2)为确保数据完整性算法:

a) 信息摘要(MD5,SHA256,SHA512)

 

二、使用GPG做对称加密

GnuPG是非常流行的加密软件,支持所有常见加密算法,并且开源免费使用。

官网:http://www.gnupg.org/

支持算法:

公钥:RSA,ELG,DS

对称加密:3DES,CAST5,BLOWFISH,AES,AES256

散列:MD5,SHA1,SHA256,SHA512

加密解密命令:

加密:gpg -c  文件名 或 gpg --symmetric 文件名

解密:gpg -d 加密文件 >  解密后文件 gpg --decrypt 加密文件 > 解密后文件  (>重定向到新文件,新文件名随意)

1.使用GPG做对称加密

1)确保已经安装了相关软件(默认已经安装好了)

  1. [root@proxy ~]# yum -y install gnupg2            //安装软件
  2. [root@proxy ~]# gpg --version                    //查看版本
  3. gpg (GnuPG) 2.0.22

2) gpg使用对称加密算法加密数据的操作

执行下列操作:

  1. [root@proxy ~]#echo 123 > file2.txt
  2. [root@proxy ~]#cat file2.txt       //未加密前
  3. 123
  4. [root@proxy ~]# gpg -c file2.txt
  5. .. ..

根据提示依次输入两次密码即可,这两个次密码是你为这个文件设置的,你接下来要打开这个加密文件需要用到。如果是在GNOME桌面环境,设置密码的交互界面会是弹出的窗口程序,如图-1所示:

 

如果是在tty终端(ssh远程连接)执行的上述加密操作,则提示界面也是文本方式的,如图-2所示。

 

根据提示输入两次口令,加密后的文件(自动添加后缀 .gpg)就生成了,传递过程中只要发送加密的文件(比如 file2.txt.gpg)就可以了。

  1. [root@proxy ~]# cat file2.txt.gpg                    //查看加密数据为乱码
  2. ��,D����"{���O����<��{�Z̳�C�����w�';3�B�

3)使用gpg对加密文件进行解密操作

收到加密的文件后,必须进行解密才能查看其内容。

注意,本操作在本机上操作不需要验证密码(本地有秘钥),只有在其他主机上解密需要输入密码

  1. [root@proxy ~]# gpg -d file2.txt.gpg > file2.txt      //解密后保存
  2. [root@proxy ~]# cat file2.txt
  3. 123
  4. [root@proxy ~]# scp test.txt.gpg [email protected]:/root    //发送到其他主机
  5. [root@client ~]# yum -y install gnupg2                                         //安装软件
  6. [root@client ~]# gpg -d file2.txt.gpg > file2.txt                           //解密后保存
  7. gpg: 3DES 加密过的数据
  8. .. ..                                                                    //会弹出提示框,根据提示输入正确密码,之前输入的
  9. [root@client ~]# cat file2.txt                     //查看解密后的文件
  10. 123

 

三、使用GPG做非对称加密

  非对称加密流程:公钥是公有的,用来加密(必须是同一套公钥,或者是对方的公钥),私钥是私有的用于解密,前提是加密文件是用的你创建的公钥加密的

加密解密命令:

加密:gpg -e -r 目标用户公钥  文件名 或 gpg --encrypt  --recipient 目标用户公钥 文件名

解密:gpg -d 加密文件 >  解密后文件 gpg --decrypt 加密文件 > 解密后文件  (>重定向到新文件,新文件名随意)

1.非对称加密流程:

 非对称加密/解密文件时,UserA(192.168.4.100)生成私钥与公钥,并把公钥发送给UserB(192.168.4.5),UserB使用公钥加密数据,并把加密后的数据传给UserA,UserA最后使用自己的私钥解密数据。

2.实现过程如下所述。

1)UserA创建自己的公钥、私钥对(在192.168.4.100操作)

 创建过程中需要你输入一些信息,前几个按回车就行了,后面需要你输入姓名邮箱和注释,不能随意填写,填一个你能记住的即可,然后输入大O,会弹出一个密码框,提示你创建密码.如果在生成秘钥期间卡主了,可以参照如下方法:

[gpg生成秘钥时卡死]https://blog.csdn.net/ck784101777/article/details/101212318

  1. [root@client ~]# gpg --gen-key //创建密钥对
  2. … …
  3. 请选择您要使用的密钥种类:
  4. (1) RSA and RSA (default)                            //默认算法为RSA
  5. (2) DSA and Elgamal
  6. (3) DSA (仅用于签名)
  7. (4) RSA (仅用于签名)
  8. 您的选择?                                              //直接回车默认(1)[回车]
  9. RSA 密钥长度应在 1024 位与 4096 位之间。
  10. 您想要用多大的密钥尺寸?(2048)                             //接受默认2048位,[回车]
  11. 您所要求的密钥尺寸是 2048 位
  12. 请设定这把密钥的有效期限。
  13. 0 = 密钥永不过期
  14. <n> = 密钥在 n 天后过期
  15. <n>w = 密钥在 n 周后过期
  16. <n>m = 密钥在 n 月后过期
  17. <n>y = 密钥在 n 年后过期
  18. 密钥的有效期限是?(0)                                         //接受默认永不过期[回车]
  19. 密钥永远不会过期
  20. 以上正确吗?(y/n)y                                         //输入y确认
  21.  
  22. You need a user ID to identify your key; the software constructs the user ID
  23. from the Real Name, Comment and Email Address in this form:
  24. "Heinrich Heine (Der Dichter) <[email protected]>"
  25. 真实姓名:UserA            //需输入
  26. 电子邮件地址:[email protected]    //需输入
  27. 注释:UserA              //需输入
  28. 您选定了这个用户标识:
  29. “UserA (UserA) <[email protected]>”
  30.  
  31. 更改姓名(N)、注释(C)、电子邮件地址(E)或确定(O)/退出(Q)?O  //输入大写O确认,然后会弹出提示框输入密码     
  32.   
  33. 您需要一个密码来保护您的私钥。
  34.  
  35. 我们需要生成大量的随机字节。这个时候您可以多做些琐事(像是敲打键盘、移动
  36. 鼠标、读写硬盘之类的),这会让随机数字发生器有更好的机会获得足够的熵数。
  37.  
  38.  
  39. gpg: 正在检查信任度数据库
  40. gpg: 需要 3 份勉强信任和 1 份完全信任,PGP 信任模型
  41. gpg: 深度:0 有效性: 1 已签名: 0 信任度:0-,0q,0n,0m,0f,1u
  42. pub 2048R/421C9354 2017-08-16
  43. 密钥指纹 = 8A27 6FB5 1315 CEF8 D8A0 A65B F0C9 7DA6 421C 9354
  44. uid UserA (UserA) <[email protected]>
  45. sub 2048R/9FA3AD25 2017-08-16

注意:生产密钥后当前终端可能会变的无法使用,执行reset命令即可,或者关闭后再开一个终端。

2)UserA导出自己的公钥文件(在192.168.4.100操作)

用户的公钥、私钥信息分别保存在pubring.gpg和secring.gpg文件内:

  1. [root@client ~]# gpg --list-keys                         //查看公钥环
  2. /root/.gnupg/pubring.gpg
  3. ------------------------------
  4. pub 2048R/421C9354 2017-08-16
  5. uid UserA (User A) <[email protected]>
  6. sub 2048R/9FA3AD25 2017-08-16

使用gpg命令结合--export选项将其中的公钥文本导出:

  1. [root@client ~]# gpg -a --export UserA > UserA.pub
  2. //--export的作用是导出密钥,-a的作用是导出的密钥存储为ASCII格式
  3. [root@client ~]# scp UserA.pub 192.168.4.5:/tmp/    //将密钥传给Proxy  

3)UserB导入接收的公钥信息(在192.168.4.5操作)

使用gpg命令结合--import选项导入发送方的公钥信息,以便在加密文件时指定对应的公钥。

  1. [root@proxy ~]# gpg --import /tmp/UserA.pub
  2. gpg: 密钥 421C9354:公钥“UserA (UserA) <[email protected]>”已导入
  3. gpg: 合计被处理的数量:1
  4. gpg: 已导入:1 (RSA: 1)

4) UserB使用公钥加密数据,并把加密后的数据传给UserA(在192.168.4.5操作)

  1. [root@proxy ~]# echo "I love you ." > love.txt
  2. [root@proxy ~]# gpg -e -r UserA love.txt
  3. 无论如何还是使用这把密钥吗?(y/N)y                         //确认使用此密钥加密文件
  4. //-e选项是使用密钥加密数据
  5. //-r选项后面跟的是密钥,说明使用哪个密钥对文件加密
  6. [root@proxy ~]# scp love.txt.gpg 192.168.4.100:/root    //加密的数据传给UserA

5)UserA以自己的私钥解密文件(在192.168.4.100操作)

  1. [root@client ~]# gpg -d love.txt.gpg > love.txt
  2. 您需要输入密码,才能解开这个用户的私钥:“UserA (UserA) <[email protected]>”
  3. 2048 位的 RSA 密钥,钥匙号 9FA3AD25,建立于 2017-08-16 (主钥匙号 421C9354)
  4.                                                 //验证私钥口令
  5. gpg: 由 2048 位的 RSA 密钥加密,钥匙号为 9FA3AD25、生成于 2017-08-16
  6. “UserA (UserA) <[email protected]>”
  7. [root@client ~]# cat love.txt                     //获得解密后的文件内容
  8. I love you.

3.使用GPG的签名机制,检查数据来源的正确性

 使用私钥签名的文件,是可以使用对应的公钥验证签名的,只要验证成功,则说明这个文件一定是出自对应的私钥签名,除非私钥被盗,否则一定能证明这个文件来自于某个人!

   本实验用于验证这个加密文件是否出自加密方,证书验证就是基于这种验证(htpps开头的网站都带证书验证)

1)在client(192.168.4.100)上,UserA为软件包创建分离式签名

将软件包、签名文件、公钥文件一起发布给其他用户下载。

  1. [root@client ~]# tar zcf log.tar /var/log             //建立测试软件包
  2. [root@client ~]# gpg -b log.tar                     //创建分离式数字签名
  3. [root@client ~]# ls -lh log.tar*
  4. -rw-rw-r--. 1 root root 170 8月 17 21:18 log.tar
  5. -rw-rw-r--. 1 root root 287 8月 17 21:22 log.tar.sig
  6. [root@client ~]# scp log.tar* 192.168.4.5:/root        //将签名文件与签名传给UserB

2)在192.168.4.5上验证签名

  1. [root@proxy ~]# gpg --verify log.tar.sig log.tar
  2. gpg:于2028年06月07日 星期六 23时23分23秒 CST 创建的签名,使用 RSA,钥匙号 421C9354
  3. gpg: 完好的签名,来自于“UserA (UserA) <[email protected]>”
  4. .. ..

Guess you like

Origin blog.csdn.net/ck784101777/article/details/102453754