加密

对称加密

  • 加密一个文件
[root@localhost ~]# gpg -c passwd      #-c 指定加密的文件
                                 x 请输入密码                                          x
                                 x                                                     x
                                 x                                                     x
                                 x Passphrase *******_________________________________ x 输入对称的密码“123.com” 回车后再输入一次
                                 x                                                     x
                                 x   <OK>                             <Cancel>     x
[root@localhost ~]# ls
anaconda-ks.cfg  gh  hg  hg.gpg  mariadb-5.5.57-linux-x86_64.tar.gz  passwd原文件  passwd.gpg 加密后的文件
[root@localhost ~]# scp passwd.gpg 192.168.75.5:    把加密后的文件使用scp传输到192.168.75.5root的家目录下
root@192.168.75.5's password: 
passwd.gpg                                                                             100%  521     0.5KB/s   00:00    

到192.168.75.5的主机上解密passwd.gpg文件

[root@root ~]# gpg -o fr -d passwd.gpg  #-o指定输出的文件 -d指定解密的文件
                                 x 请输入密码                                          x
                                 x                                                     x
                                 x                                                     x
                                 x Passphrase *******_________________________________ x 输入对称的秘钥回车
                                 x                                                     x
                                 x   <OK>                             <Cancel>     x
                                 
[root@root ~]# gpg -o fr -d passwd.gpg 
gpg: CAST5 加密过的数据
can't connect to `/root/.gnupg/S.gpg-agent': 没有那个文件或目录
gpg: 以 1 个密码加密
gpg: 警告:报文未受到完整的保护
[root@root ~]# ls 
anaconda-ks.cfg  fr  hg.gpg  install.log  install.log.syslog  mariadb-5.5.57-linux-x86_64.tar.gz  passwd.gpg  rt
[root@root ~]# cat fr  查看解密的文件
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
mysql:x:36:36::/app/data:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
[root@root ~]# 

hash 算法保证数据的完整性

[root@root ~]# sha
sha1sum    sha224sum  sha256sum  sha384sum  sha512sum  支持的hash算法
[root@root ~]# sha1sum fr > fr.hash
[root@root ~]# sha1sum --check fr.hash   表示fr文件没有被更改过
fr: 确定
[root@root ~]# echo 1 >> fr  在fr文件追加一个数字1
[root@root ~]# sha1sum --check fr.hash  在次使用该命令检查文件他就会有提示
fr: 失败
sha1sum: 警告:1/1 生成的校验和不匹配
[root@root ~]# vim fr

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
mysql:x:36:36::/app/data:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
~                                                                                                                        
-- 插入 --         
[root@root ~]# sha1sum --check fr.hash  在检查就会恢复正常
fr: 确定
 [root@root ~]# cat op op文件的内容
123456789
987654321
[root@root ~]# sha1sum op >> fr.hash 同时检查多个文件
[root@root ~]# echo "0" >> op 修改op文件
[root@root ~]# sha1sum --check fr.hash 
fr: 确定
op: 失败
sha1sum: 警告:1/2 生成的校验和不匹配
 [root@root ~]# cat op 恢复op文件
123456789
987654321
 [root@root ~]# sha1sum --check fr.hash 
fr: 确定
op: 确定

非对称加密

A主机创建秘钥对

[root@localhost ~]# gpg --gen-key
gpg (GnuPG) 2.0.22; Copyright (C) 2013 Free Software Foun
dation, Inc.This is free software: you are free to change and redistr
ibute it.There is NO WARRANTY, to the extent permitted by law.

请选择您要使用的密钥种类:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (仅用于签名)
   (4) RSA (仅用于签名)
您的选择? 1
RSA 密钥长度应在 1024 位与 4096 位之间。
您想要用多大的密钥尺寸?(2048)1024
您所要求的密钥尺寸是 1024 位
请设定这把密钥的有效期限。
         0 = 密钥永不过期
      <n>  = 密钥在 n 天后过期
      <n>w = 密钥在 n 周后过期
      <n>m = 密钥在 n 月后过期
      <n>y = 密钥在 n 年后过期
密钥的有效期限是?(0) 2
密钥于 2017年08月03日 星期四 07时55分15秒 CST 过期
以上正确吗?(y/n)y

You need a user ID to identify your key; the software con
structs the user IDfrom the Real Name, Comment and Email Address in this for
m:    "Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.
de>"
真实姓名:chenxi
电子邮件地址:
注释:
您选定了这个用户标识:
    “chenxi”

更改姓名(N)、注释(C)、电子邮件地址(E)或确定(O)/退出(Q)?o 后回车


                                 x 请输入密码                                          x
                                 x                                                     x
                                 x                                                     x
                                 x Passphrase *******_________________________________ x 输入私钥加密口令;回车后需要再次确认
                                 x                                                     x
                                 x   <OK>                             <Cancel>     x
                                 
- 查看公钥 
[root@centos6 ~]# gpg --list-key
/root/.gnupg/pubring.gpg
------------------------
pub   1024R/E03121EF 2017-07-31 [expires: 2017-08-03]
uid                  chenxi
sub   1024R/46DE5C3F 2017-07-31 [expires: 2017-08-03]
- 导出公钥
[root@centos6 ~]# gpg -a --export -o chenxi.pubkey
把公钥传到另一台主机上
[root@centos6 ~]# scp chenxi.pubkey 192.168.75.5:
The authenticity of host '192.168.75.5 (192.168.75.5)' ca
n't be established.RSA key fingerprint is a9:ad:42:ec:d4:20:cd:a2:ee:67:98:0
c:f9:79:74:f8.Are you sure you want to continue connecting (yes/no)? ye
sWarning: Permanently added '192.168.75.5' (RSA) to the li
st of known hosts.root@192.168.75.5's password: 
Permission denied, please try again.
root@192.168.75.5's password: 
chenxi.pubkey          100% 1000     1.0KB/s   00:00    
[root@centos6 ~]# 

在B主机上导入公钥

[root@root ~]# gpg --import chenxi.pubkey 
gpg: /root/.gnupg/trustdb.gpg:建立了信任度数据库
gpg: 密钥 E03121EF:公钥“chenxi”已导入
gpg: 合计被处理的数量:1
gpg:           已导入:1  (RSA: 1)
查看公钥
[root@root ~]# gpg --list-key
/root/.gnupg/pubring.gpg
------------------------
pub   1024R/E03121EF 2017-07-31 [有效至:2017-08-03]
uid                  chenxi
sub   1024R/46DE5C3F 2017-07-31 [有效至:2017-08-03]
使用对应公钥加密
[root@root ~]# gpg -e -r chenxi yuer 加密yuer文件
gpg: 46DE5C3F:没有证据表明这把密钥真的属于它所声称的持有
者
pub  1024R/46DE5C3F 2017-07-31 chenxi
 主钥指纹: F271 527F 283A 201F D0A1  3210 88FC D9E7 E031
 21EF 子钥指纹: BA78 90A6 44C9 B0E0 98DB  AA7F 87E6 0E24 46DE
 5C3F
这把密钥并不一定属于用户标识声称的那个人。如果您真的知道
自己在做什么,您可以在下一个问题回答 yes。

无论如何还是使用这把密钥吗?(y/N)y
[root@root ~]# ls
anaconda-ks.cfg  fr       hg.gpg       install.log.syslog                  op          rt    yuer.gpg 加密后的文件
chenxi.pubkey    fr.hash  install.log  mariadb-5.5.57-linux-x86_64.tar.gz  passwd.gpg  yuer
传到A主机上
[root@root ~]# scp yuer.gpg 192.168.75.131:
The authenticity of host '192.168.75.131 (192.168.75.131)' can't be established.
RSA key fingerprint is 9b:e7:15:49:df:42:db:e4:40:9d:74:b2:71:b9:f5:40.
Are you sure you want to continue connecting (yes/no)? yes  
Warning: Permanently added '192.168.75.131' (RSA) to the list of known hosts.
root@192.168.75.131's password: 
yuer.gpg                                                                               100%  227     0.2KB/s   00:00    
[root@root ~]# 

到a主机解密

[root@centos6 ~]# gpg -o sed -d yuer.gpg 回车后需要输入私钥密码才能使用私钥解开

You need a passphrase to unlock the secret key for
user: "chenxi"
1024-bit RSA key, ID 46DE5C3F, created 2017-07-31 (main key ID E03121EF)

can't connect to `/root/.gnupg/S.gpg-agent': No such file or directory

(pinentry-gtk-2:3282): GLib-GObject-CRITICAL **: Object class GtkSecureEntry doesn't implement property 'editing-canceled
' from interface 'GtkCellEditable'gpg: encrypted with 1024-bit RSA key, ID 46DE5C3F, created 2017-07-31
      "chenxi"
[root@centos6 ~]# ls
anaconda-ks.cfg  Desktop    Downloads    install.log.syslog  Pictures  sed        Videos
chenxi.pubkey    Documents  install.log  Music               Public    Templates  yuer.gpg
[root@centos6 ~]# cat sed  查看解开的文件
123456789
987654321
[root@centos6 ~]#

删除私钥

[root@centos6 ~]# gpg --delete-secret-keys chenxi
gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


sec  1024R/E03121EF 2017-07-31 chenxi

Delete this key from the keyring? (y/N) y
This is a secret key! - really delete? (y/N) y
[root@centos6 ~]# 

删除公钥

[root@centos6 ~]# gpg --delete-keys chenxi
gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


pub  1024R/E03121EF 2017-07-31 chenxi

Delete this key from the keyring? (y/N) y
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341

推荐阅读更多精彩内容