加密

對稱加密

  • 加密一個文件
[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傳輸?shù)?92.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 加密過的數(shù)據(jù)
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 算法保證數(shù)據(jù)的完整性

[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文件追加一個數(shù)字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  在檢查就會恢復(fù)正常
fr: 確定
 [root@root ~]# cat op op文件的內(nèi)容
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 恢復(fù)op文件
123456789
987654321
 [root@root ~]# sha1sum --check fr.hash 
fr: 確定
op: 確定

非對稱加密

A主機創(chuàng)建秘鑰對

[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 密鑰長度應(yīng)在 1024 位與 4096 位之間扩氢。
您想要用多大的密鑰尺寸窖剑?(2048)1024
您所要求的密鑰尺寸是 1024 位
請設(shè)定這把密鑰的有效期限徐钠。
         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]
- 導(dǎo)出公鑰
[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主機上導(dǎo)入公鑰

[root@root ~]# gpg --import chenxi.pubkey 
gpg: /root/.gnupg/trustdb.gpg:建立了信任度數(shù)據(jù)庫
gpg: 密鑰 E03121EF:公鑰“chenxi”已導(dǎo)入
gpg: 合計被處理的數(shù)量:1
gpg:           已導(dǎo)入: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]
使用對應(yīng)公鑰加密
[root@root ~]# gpg -e -r chenxi yuer 加密yuer文件
gpg: 46DE5C3F:沒有證據(jù)表明這把密鑰真的屬于它所聲稱的持有
者
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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末抖韩,一起剝皮案震驚了整個濱河市蛀恩,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌茂浮,老刑警劉巖双谆,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異席揽,居然都是意外死亡顽馋,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進店門幌羞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來寸谜,“玉大人,你說我怎么就攤上這事属桦⌒艹眨” “怎么了?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵聂宾,是天一觀的道長果善。 經(jīng)常有香客問我,道長系谐,這世上最難降的妖魔是什么巾陕? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮纪他,結(jié)果婚禮上鄙煤,老公的妹妹穿的比我還像新娘。我一直安慰自己茶袒,他們只是感情好梯刚,可當我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著薪寓,像睡著了一般乾巧。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上预愤,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天,我揣著相機與錄音咳胃,去河邊找鬼植康。 笑死,一個胖子當著我的面吹牛展懈,可吹牛的內(nèi)容都是我干的销睁。 我是一名探鬼主播供璧,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼冻记!你這毒婦竟也來了睡毒?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤冗栗,失蹤者是張志新(化名)和其女友劉穎演顾,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體隅居,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡钠至,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了胎源。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片棉钧。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖涕蚤,靈堂內(nèi)的尸體忽然破棺而出宪卿,到底是詐尸還是另有隱情,我是刑警寧澤万栅,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布佑钾,位于F島的核電站,受9級特大地震影響申钩,放射性物質(zhì)發(fā)生泄漏次绘。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一撒遣、第九天 我趴在偏房一處隱蔽的房頂上張望邮偎。 院中可真熱鬧,春花似錦义黎、人聲如沸禾进。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽泻云。三九已至,卻和暖如春狐蜕,著一層夾襖步出監(jiān)牢的瞬間宠纯,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工层释, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留婆瓜,地道東北人。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像廉白,于是被迫代替她去往敵國和親个初。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內(nèi)容