使用gpg來加密數據
一、數據的加密方式
數據加密有三種方式:
1、對稱加密(算法有:DES、AES、3DES、)加密和解密使用同一個密鑰
2、非對稱加密(RSA、DSA、ELGamal等等)一共四把鑰匙,用公鑰加密數據,只能使用與之配對的私鑰解密;反之亦然
3、單項加密(md5 sha1 sha2 sha128 sha256 sha512等)算出數據的hash值,當數據發生微弱的變化都會產生雪崩效應,所生成的新的hash值將變得完全不一樣,主要用於數據校驗。
二、幾種加密方式的特點
1、對稱加密:
優點:加密、解密使用同一個密鑰,效率高,將原始數據分割成固定大小的塊,逐個進行加密
缺點:密鑰過多,密鑰分發,最主要的:數據來源無法確認
2、非對稱秘鑰加密:
優點:用公鑰加密數據,只能使用與之配對的私鑰解密;反之亦然,加密系數高
缺點:密鑰長,加密解密效率低下,尤其解密效率極其低下。
適用場景:
數字簽名:主要在於讓接收方確認發送方身份
3、單項加密:
類似於指紋,將任意數據縮小成固定大小的“指紋”,任意長度的數據都可以,但是以固定長度輸出,如果修改數據,則會產生雪崩效應,生成新的hash值完全不同。
但是無法從指紋反推回原始數據。
適用場景:
數據完整性校驗,數據可靠性校驗。
三、gpg加密工具
1、實現對稱和非對稱加密
1)、對稱加密演示
- [root@newhostname app]# echo ‘Ok ok ok ok ok ‘ > fist_encrypt
- [root@newhostname app]# ls
- fist_encrypt
- [root@newhostname app]# gpg -c fist_encrypt #對文件進行加密
- gpg: 已創建目錄‘/root/.gnupg’
- gpg: 新的配置文件‘/root/.gnupg/gpg.conf’已建立
- gpg: 警告:在‘/root/.gnupg/gpg.conf’裏的選項於此次運行期間未被使用
- gpg: 鑰匙環‘/root/.gnupg/pubring.gpg’已建立
- [root@newhostname app]# ls
- fist_encrypt fist_encrypt.gpg #fist_encrypt.gpg這個就是加密過的文件
執行 gpg -c fist_encrypt會出現兩次如下圖
輸入一個密碼,加密就完成了,我們來看一下這個文件fist_encrypt.gpg
- [root@newhostname app]# cat fist_encrypt.gpg
- gKf_);root@newhostname:/app[root@newhostname app]#
已經實現了加密
現在我們將這個文件拷貝到其他主機,並進行解密
- [root@joker-6-01 ~]# cd /app
- [root@joker-6-01 app]# rm -rf fist
- [root@joker-6-01 app]# gpg -d fist_encrypt.gpg >fist
- gpg: CAST5 encrypted data
- can‘t connect to `/root/.gnupg/S.gpg-agent‘: No such file or directory
- gpg: encrypted with 1 passphrase
- Ok ok ok ok ok
- gpg: WARNING: message was not integrity protected
- [root@joker-6-01 app]# ls
- fist_encrypt.gpg
- [root@joker-6-01 app]#
解密需要輸入密碼,同上圖
現在我們看一下生成的新文件
- [root@joker-6-01 app]# cat fist
- Ok ok ok ok ok
總結: 通過gpg -c file 進行文件加密,並生成一個 file.gpg的新文件 ,通過 gpg -d file.gpg >file 可以進行解密
2)通過gpg工具進行非對稱的文件加密(我們需要兩對秘鑰)
hostA上操作:
- [root@newhostname app]# gpg --gen-key #創建密鑰對
- gpg (GnuPG) 2.0.22; Copyright (C) 2013 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.
- gpg: 鑰匙環‘/root/.gnupg/secring.gpg’已建立
- 請選擇您要使用的密鑰種類:
- (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) 0
- 密鑰永遠不會過期
- 以上正確嗎?(y/n)y
1到這裏會出現下面四個圖,意思是讓我們再對私鑰進行加密,我們一路回車,因為只是測試,所以就不再加密了。
一路回車過後,秘鑰生成完畢
- You need a user ID to identify your key; the software constructs the user ID
- from the Real Name, Comment and Email Address in this form:
- "Heinrich Heine (Der Dichter) <[email protected]>"
- 真實姓名:zhang
- 電子郵件地址:765030447@qq.com
- 註釋:test
- 您選定了這個用戶標識:
- “zhang (test) <765030447@qq.com>”
- 更改姓名(N)、註釋(C)、電子郵件地址(E)或確定(O)/退出(Q)?o
- 您需要一個密碼來保護您的私鑰。
- 您不想要有密碼――這個想法實在是遭透了!
- 不過,我仍然會照您想的去做。您任何時候都可以變更您的密碼,僅需要
- 再次執行這個程序,並且使用“--edit-key”選項即可。
- 我們需要生成大量的隨機字節。這個時候您可以多做些瑣事(像是敲打鍵盤、移動
- 鼠標、讀寫硬盤之類的),這會讓隨機數字發生器有更好的機會獲得足夠的熵數。
- 我們需要生成大量的隨機字節。這個時候您可以多做些瑣事(像是敲打鍵盤、移動
- 鼠標、讀寫硬盤之類的),這會讓隨機數字發生器有更好的機會獲得足夠的熵數。
- gpg: /root/.gnupg/trustdb.gpg:建立了信任度數據庫
- gpg: 密鑰 11F74DDB 被標記為絕對信任
- 公鑰和私鑰已經生成並經簽名。
- gpg: 正在檢查信任度數據庫
- gpg: 需要 3 份勉強信任和 1 份完全信任,PGP 信任模型
- gpg: 深度:0 有效性: 1 已簽名: 0 信任度:0-,0q,0n,0m,0f,1u
- pub 1024R/11F74DDB 2018-01-06
- 密鑰指紋 = E1F8 DE1D B979 48F4 A216 DCB7 D5B9 E6A3 11F7 4DDB
- uid zhang (test) <765030447@qq.com>
- sub 1024R/710D443B 2018-01-06
我們看一下我們生成的秘鑰,在自己的家目錄下的.gnupg目錄下
- [root@newhostname .gnupg]# pwd
- /root/.gnupg
- [root@newhostname .gnupg]# ll
- 總用量 28
- -rw-------. 1 root root 7680 1月 6 19:45 gpg.conf
- drwx------. 2 root root 6 1月 6 19:45 private-keys-v1.d
- -rw-------. 1 root root 675 1月 6 20:12 pubring.gpg
- -rw-------. 1 root root 675 1月 6 20:12 pubring.gpg~
- -rw-------. 1 root root 600 1月 6 20:12 random_seed
- -rw-------. 1 root root 1338 1月 6 20:12 secring.gpg
- srwxr-xr-x. 1 root root 0 1月 6 20:12 S.gpg-agent
- -rw-------. 1 root root 1280 1月 6 20:12 trustdb.gpg
- pubring.gpg 這個是公鑰
- secring.gpg 這個是私鑰
我們導出公鑰
- [root@newhostname .gnupg]# gpg --list-key #可以查看公鑰
- /root/.gnupg/pubring.gpg
- ------------------------
- pub 1024R/11F74DDB 2018-01-06
- uid zhang (test) <765030447@qq.com>
- sub 1024R/710D443B 2018-01-06
- [root@newhostname .gnupg]# gpg -a --export -o zhang.pubkey #導出公鑰並生成可視的字符
- [root@newhostname .gnupg]# ls
- gpg.conf private-keys-v1.d pubring.gpg pubring.gpg~ random_seed secring.gpg S.gpg-agent trustdb.gpg zhang.pubkey
- [root@newhostname .gnupg]# ll
- 總用量 32
- -rw-------. 1 root root 7680 1月 6 19:45 gpg.conf
- drwx------. 2 root root 6 1月 6 19:45 private-keys-v1.d
- -rw-------. 1 root root 675 1月 6 20:12 pubring.gpg
- -rw-------. 1 root root 675 1月 6 20:12 pubring.gpg~
- -rw-------. 1 root root 600 1月 6 20:12 random_seed
- -rw-------. 1 root root 1338 1月 6 20:12 secring.gpg
- srwxr-xr-x. 1 root root 0 1月 6 20:12 S.gpg-agent
- -rw-------. 1 root root 1280 1月 6 20:12 trustdb.gpg
- -rw-r--r--. 1 root root 1020 1月 6 20:37 zhang.pubkey
- [root@newhostname .gnupg]# cat zhang.pubkey #查看導出的公鑰
- -----BEGIN PGP PUBLIC KEY BLOCK-----
- Version: GnuPG v2.0.22 (GNU/Linux)
- mI0EWlC9RgEEAMaYQLcdCujOTwdKiBLPfSrhwceFqt7FGT/xBggSNHs8c9EyQ/8T
- ls7PiiaK4drm0mdPtIPr/pa6m2T20mFB0DMpeB9SEI+z7v/jWB/Y1xxINjGvFtKP
- JSwFSa8qYUNLBYpBIPoa5IlXpzrkG+gRvHh++7yu8e4e06oA9lAzWT2rABEBAAG0
- H3poYW5nICh0ZXN0KSA8NzY1MDMwNDQ3QHFxLmNvbT6IuQQTAQIAIwUCWlC9RgIb
- AwcLCQgHAwIBBhUIAgkKCwQWAgMBAh4BAheAAAoJENW55qMR903btFMEAIm/9HWT
- ofXbTTVUbjFMV8MSYU+TRj1n2xuPKDaWLgJyb2cH9c/57RMS5bHqvqEBxOzrtnXB
- 2FPYiS8VHgLhvnUkMqer1WA7RjVESAcp52UKvoI+7yD5K7vABMg9yDPnNag2EKLr
- H7db0wspYo5ox3w8AZsJj3dUMLgipAQDUpckuI0EWlC9RgEEAK0PxCRuHLL3XUC6
- wDr4gSRj5TVcwVuSOE0ECBZakbDeuljhq9Fn0UR8FfdiPOw4Cakt0RWiYh9oxdsF
- R3Y6DL6fzBAwozxz1I5NXtTj68HY+/6bEiHtz4xvFPR9YEwrqzPZWaWMaTRgtAL6
- n/ONYQKPHa82p2qIdzv7+y59u9zRABEBAAGInwQYAQIACQUCWlC9RgIbDAAKCRDV
- ueajEfdN204xA/9ZVVqFAnFti6vogSCzPMFDj6s4M66EBGEUIKkK9cnw1vzXzsv3
- v8YfevZsWGMX7Cag89a3ox+qIVvHhsEkL3TXEAmrYLZn9/T/E1yRQnYpQcI9uvMg
- /UAy3j0HtOzui4ofMuGoNJ1V7hOMt8Jlzt2+WQVvVogHcNWmEUSWcmIp8w==
- =o0NK
- -----END PGP PUBLIC KEY BLOCK-----
- [root@newhostname .gnupg]#
將公鑰文件拷貝到hostB機器
- [root@newhostname .gnupg]# scp zhang.pubkey 172.18.30.254:~
- root@172.18.30.254‘s password:
- zhang.pubkey 100% 1020 2.9MB/s 00:00
2)、在hostB上操作:
同樣的方法生成自己的密鑰對
- [root@joker-6-01 ~]# gpg --gen-key
- 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.
- Please select what kind of key you want:
- (1) RSA and RSA (default)
- (2) DSA and Elgamal
- (3) DSA (sign only)
- (4) RSA (sign only)
- Your selection?
- RSA keys may be between 1024 and 4096 bits long.
- What keysize do you want? (2048) 1024
- Requested keysize is 1024 bits
- Please specify how long the key should be valid.
- 0 = key does not expire
- <n> = key expires in n days
- <n>w = key expires in n weeks
- <n>m = key expires in n months
- <n>y = key expires in n years
- Key is valid for? (0)
- Key does not expire at all
- Is this correct? (y/N)
- Key is valid for? (0)
- Key does not expire at all
- Is this correct? (y/N) y
- GnuPG needs to construct a user ID to identify your key.
- Real name: shang
- Email address: 765030447@qq.com
- Comment: test
- You selected this USER-ID:
- "shang (test) <[email protected]>"
- Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit?
查看秘鑰,並導出公鑰
- [root@joker-6-01 .gnupg]# gpg --list-key
- /root/.gnupg/pubring.gpg
- ------------------------
- pub 1024R/7FFA70D9 2018-01-06
- uid shang (test) <765030447@qq.com>
- sub 1024R/E774B778 2018-01-06
- [root@joker-6-01 .gnupg]# gpg -a --export -o shang.pubkey
- [root@joker-6-01 .gnupg]# ls
- gpg.conf private-keys-v1.d pubring.gpg pubring.gpg~ random_seed secring.gpg shang.pubkey trustdb.gpg
- [root@joker-6-01 .gnupg]# ll
- total 36
- -rw-------. 1 root root 7856 Nov 7 16:31 gpg.conf
- drwx------ 2 root root 4096 Jan 6 20:02 private-keys-v1.d
- -rw------- 1 root root 674 Jan 6 21:02 pubring.gpg
- -rw------- 1 root root 674 Jan 6 21:02 pubring.gpg~
- -rw------- 1 root root 600 Jan 6 21:02 random_seed
- -rw------- 1 root root 1338 Jan 6 21:02 secring.gpg
- -rw-r--r-- 1 root root 1016 Jan 6 21:05 shang.pubkey
- -rw-------. 1 root root 1280 Jan 6 21:02 trustdb.gpg
- [root@joker-6-01 .gnupg]# cat shang.pubkey
- -----BEGIN PGP PUBLIC KEY BLOCK-----
- Version: GnuPG v2.0.14 (GNU/Linux)
- mI0EWlDI3QEEANDr6fIRum7F1tdaJH6TI+O5QpKf3f1zEacmqqH3iQ+eIJkUZDTG
- yC9k87zKQVaDgbZlcn38Lf/u7X3pRemsGa8ZMkmviHCc7gfW5C0NmMse7/dKGUmt
- 6xOnVRsgk+WYDGwpxI0rhRxzDg0AIpNH20wZpK6bgNwoC8i5zIO605tRABEBAAG0
- H3NoYW5nICh0ZXN0KSA8NzY1MDMwNDQ3QHFxLmNvbT6IuAQTAQIAIgUCWlDI3QIb
- AwYLCQgHAwIGFQgCCQoLBBYCAwECHgECF4AACgkQNG0fpn/6cNkZ7wP+Oy6X24Tt
- WZQ5T4LYc9go7RuU8vDIMHQzEC0CXO9pi3ZU7quEYDfd5N9WD61+jKY9s79L09tK
- 14RQJByWaFFXJwWyrjzu4BzMZiAmjDf9PA5hpUVFOAdQbMKENlXOh3Rxsi65EsAH
- Hem/Plbr75dEN+CQV8emlZ2tDDBYhmbbkzC4jQRaUMjdAQQAzg1FrRac6KyerWt+
- oJ83B9eDLn6Yq9xBA6W1MdRTZKOEKygkbGP7Slr7e7lWHKinlfRL7+9+2IjzurQZ
- ndWO/msAKxdnqIMbg7Xx1eXWr0VwvFAz2AHDQz+Ls3mkJ40ZCPbHKXRb5+7USWKB
- VHCubgMJ49VfTrxBwzCo2M5K3+MAEQEAAYifBBgBAgAJBQJaUMjdAhsMAAoJEDRt
- H6Z/+nDZLW0EAJ3oHrPWIGczgzSZ+ociwsmaVggqAky3BCfWhbUgJBEOAEh+SMSw
- WueVIKNT/bA3SVUak0PmNz1IkXLRYmiPA/81+I9ezUx0ac1noYO2MzYfdmaCFSBM
- sfnfvpbk6/L0FY0aIhcuufCztIs+bsxpsXXYfq4C5hyMI1S1S8YS0pDX
- =DHkG
- -----END PGP PUBLIC KEY BLOCK-----
- [root@joker-6-01 .gnupg]#
將hostB的公鑰shang.pubkey拷貝到hostA
- [root@joker-6-01 .gnupg]# scp shang.pubkey 172.18.30.253:~
- The authenticity of host ‘172.18.30.253 (172.18.30.253)‘ can‘t be established.
- RSA key fingerprint is a4:04:ea:5b:ce:18:0c:e4:e8:b4:51:47:ea:87:0b:2b.
- Are you sure you want to continue connecting (yes/no)? yes
- Warning: Permanently added ‘172.18.30.253‘ (RSA) to the list of known hosts.
- root@172.18.30.253‘s password:
- shang.pubkey 100% 1016 1.0KB/s 00:00
A機器拿到了B的公鑰:shang.pubkey
B機器拿到了A的公鑰:zhang.pubkey
現在在各個機器上導入新拿到的公鑰
- hostA
- [root@newhostname app]# gpg --import ~/shang.pubkey
- gpg: 密鑰 7FFA70D9:公鑰“shang (test) <765030447@qq.com>”已導入
- gpg: 合計被處理的數量:1
- gpg: 已導入:1 (RSA: 1)
- hostB
- [root@joker-6-01 ~]# gpg --import zhang.pubkey
- gpg: key 11F74DDB: public key "zhang (test) <[email protected]>" imported
- gpg: Total number processed: 1
- gpg: imported: 1 (RSA: 1)
現在我們在A機器使用B的公鑰加密/app/fist_encrypt,並將生成的文件拷貝到B
- [root@newhostname app]# gpg -e -r shang fist_encrypt
- gpg: E774B778:沒有證據表明這把密鑰真的屬於它所聲稱的持有者
- pub 1024R/E774B778 2018-01-06 shang (test) <765030447@qq.com>
- 主鑰指紋: 5042 4F44 8DE2 F9EA 1A02 BBC8 346D 1FA6 7FFA 70D9
- 子鑰指紋: BB56 2ED6 84DA 2DB7 A6A6 AFF2 122E ED1C E774 B778
- 這把密鑰並不一定屬於用戶標識聲稱的那個人。如果您真的知道自
- 己在做什麽,您可以在下一個問題回答 yes。
- 無論如何還是使用這把密鑰嗎?(y/N)y
- [root@newhostname app]# ls
- fist_encrypt fist_encrypt.gpg
將新生成的加密文件拷貝到hostB上
- [root@newhostname app]# ls
- fist_encrypt fist_encrypt.gpg
- [root@newhostname app]# scp fist_encrypt.gpg [email protected]:/app
- root@172.18.30.254‘s password:
- fist_encrypt.gpg 100% 222 377.5KB/s 00:00
- [root@newhostname app]#
到B機器上,使用B的私鑰進行解密
- [root@joker-6-01 app]# ls
- fist_encrypt.gpg
- [root@joker-6-01 app]# cat fist_encrypt.gpg
- %qXd46-}a>y?;n$
- jtyY T>AWK}@jBRW1
- ~0>[root@joker-6-01 app]#
- [root@joker-6-01 app]#
- [root@joker-6-01 app]# gpg -d fist_encrypt.gpg > fist_encrypt #對文件進行解密,並輸出到fistt_encrypt內
- gpg: encrypted with 1024-bit RSA key, ID E774B778, created 2018-01-06
- "shang (test) <[email protected]>"
- [root@joker-6-01 app]# ls
- fist_encrypt fist_encrypt.gpg
- [root@joker-6-01 app]# cat fist_encrypt #解密完成
- Ok ok ok ok ok
對稱的加密解密完成了,現在我們刪除秘鑰
hostA上操作:
- [root@newhostname app]# gpg --delete-keys shang #在A上刪除B的公鑰
- gpg (GnuPG) 2.0.22; Copyright (C) 2013 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/7FFA70D9 2018-01-06 shang (test) <765030447@qq.com>
- 要從鑰匙環裏刪除這把密鑰嗎?(y/N)y
- [root@newhostname app]#
- [root@newhostname app]# gpg --delete-secret-keys zhang #在A機器上刪除自己的私鑰
- gpg (GnuPG) 2.0.22; Copyright (C) 2013 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/11F74DDB 2018-01-06 zhang (test) <765030447@qq.com>
- 要從鑰匙環裏刪除這把密鑰嗎?(y/N)y
- 這是一把私鑰!――真的要刪除嗎?(y/N)y
- [root@newhostname app]# gpg --delete-keys zhang #再刪除自己的公鑰
- gpg (GnuPG) 2.0.22; Copyright (C) 2013 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/11F74DDB 2018-01-06 zhang (test) <765030447@qq.com>
- 要從鑰匙環裏刪除這把密鑰嗎?(y/N)y
- 註意:必須先刪除私鑰再刪除公鑰
hostB上操作:
- [root@joker-6-01 app]# gpg --delete-keys zhang #在B上刪除A的公鑰
- 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/11F74DDB 2018-01-06 zhang (test) <765030447@qq.com>
- Delete this key from the keyring? (y/N) y
- [root@joker-6-01 app]#
- [root@joker-6-01 app]# gpg --delete-secret-keys shang #刪除自己的私鑰
- 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/7FFA70D9 2018-01-06 shang (test) <765030447@qq.com>
- Delete this key from the keyring? (y/N) y
- This is a secret key! - really delete? (y/N) y
- [root@joker-6-01 app]# gpg --delete-keys shang #刪除自己的公鑰
- 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/7FFA70D9 2018-01-06 shang (test) <765030447@qq.com>
- Delete this key from the keyring? (y/N) y
命令總結:
對稱加密方法:
1、gpg -c file 生成一個file.gpg的加密文件
2、gpg -d file.gpg > file 解密file.gpg並將輸出存入file文件
非對稱加密方法:
第一步:
A機器:
1、gpg –gen-key 生成公私鑰對
2、gpg -a –export Akey_name.pubkey
3、scp Akey_name.pubkey B機器:~/
B機器:
1、gpg–gen-key 生成公私鑰對
2、gpg -a –export Bkey_name.pubkey
3、scp Bkey_name.pubkey A機器:~/
第二步:
1、gpg –import ~/Bkey_name 在A機器上導入B機器的公鑰
2、gpg –import ~/Akey_name 在B機器上導入A機器的公鑰
第三步:
1、gpg -e -r Bkey_name file1 在A機器使用B的公鑰對文件進行加密
2、gpg -e -r Akey_name file2 在B機器使用A的公鑰對文件進行加密
第四步
1、scp file1.gpg B機器:~ 在A機器,將用B公鑰加密的文件傳輸給B機器
2、scp file2.gpg A機器:~ 在B機器,將用A公鑰加密的文件傳輸給A機器
第五步
1、gpg -d file2.gpg >file2 在A機器,將B傳送來的文件解密(默認會使用A自己的私鑰來解密)
2、gpg -d file1.gpg > file1 在B機器,將A傳送來的文件解密(默認會使用B自己的私鑰來解密)
使用gpg來加密數據