1. 程式人生 > >使用gpg來加密數據

使用gpg來加密數據

圖片 gin wrapper message sgk HR 接收 VM war

一、數據的加密方式

數據加密有三種方式:

1、對稱加密(算法有:DES、AES、3DES、)加密和解密使用同一個密鑰

2、非對稱加密(RSA、DSA、ELGamal等等)一共四把鑰匙,用公鑰加密數據,只能使用與之配對的私鑰解密;反之亦然

3、單項加密(md5 sha1 sha2 sha128 sha256 sha512等)算出數據的hash值,當數據發生微弱的變化都會產生雪崩效應,所生成的新的hash值將變得完全不一樣,主要用於數據校驗。

二、幾種加密方式的特點

1、對稱加密:

優點:加密、解密使用同一個密鑰,效率高,將原始數據分割成固定大小的塊,逐個進行加密
缺點:密鑰過多,密鑰分發,最主要的:數據來源無法確認

2、非對稱秘鑰加密:

優點:用公鑰加密數據,只能使用與之配對的私鑰解密;反之亦然,加密系數高
缺點:密鑰長,加密解密效率低下,尤其解密效率極其低下。
適用場景:

數字簽名:主要在於讓接收方確認發送方身份

3、單項加密:

類似於指紋,將任意數據縮小成固定大小的“指紋”,任意長度的數據都可以,但是以固定長度輸出,如果修改數據,則會產生雪崩效應,生成新的hash值完全不同。
但是無法從指紋反推回原始數據。
適用場景:

數據完整性校驗,數據可靠性校驗。

三、gpg加密工具

1、實現對稱和非對稱加密

1)、對稱加密演示

  1. [root@newhostname app]# echo ‘Ok ok ok ok ok ‘ > fist_encrypt
  2. [root@newhostname app]# ls
  3. fist_encrypt
  4. [root@newhostname app]# gpg -c fist_encrypt #對文件進行加密
  5. gpg: 已創建目錄‘/root/.gnupg’
  6. gpg: 新的配置文件‘/root/.gnupg/gpg.conf’已建立
  7. gpg: 警告:在‘/root/.gnupg/gpg.conf’裏的選項於此次運行期間未被使用
  8. gpg: 鑰匙環‘/root/.gnupg/pubring.gpg’已建立
  9. [root@newhostname app]# ls
  10. fist_encrypt fist_encrypt.gpg #fist_encrypt.gpg這個就是加密過的文件

執行 gpg -c fist_encrypt會出現兩次如下圖

技術分享圖片

輸入一個密碼,加密就完成了,我們來看一下這個文件fist_encrypt.gpg

  1. [root@newhostname app]# cat fist_encrypt.gpg
  2. gKf_);root@newhostname:/app[root@newhostname app]#

已經實現了加密

現在我們將這個文件拷貝到其他主機,並進行解密

  1. [root@joker-6-01 ~]# cd /app
  2. [root@joker-6-01 app]# rm -rf fist
  3. [root@joker-6-01 app]# gpg -d fist_encrypt.gpg >fist
  4. gpg: CAST5 encrypted data
  5. can‘t connect to `/root/.gnupg/S.gpg-agent‘: No such file or directory
  6. gpg: encrypted with 1 passphrase
  7. Ok ok ok ok ok
  8. gpg: WARNING: message was not integrity protected
  9. [root@joker-6-01 app]# ls
  10. fist_encrypt.gpg
  11. [root@joker-6-01 app]#

解密需要輸入密碼,同上圖

現在我們看一下生成的新文件

  1. [root@joker-6-01 app]# cat fist
  2. Ok ok ok ok ok

總結: 通過gpg -c file 進行文件加密,並生成一個 file.gpg的新文件 ,通過 gpg -d file.gpg >file 可以進行解密

2)通過gpg工具進行非對稱的文件加密(我們需要兩對秘鑰)

hostA上操作:

  1. [root@newhostname app]# gpg --gen-key #創建密鑰對
  2. gpg (GnuPG) 2.0.22; Copyright (C) 2013 Free Software Foundation, Inc.
  3. This is free software: you are free to change and redistribute it.
  4. There is NO WARRANTY, to the extent permitted by law.
  5. gpg: 鑰匙環‘/root/.gnupg/secring.gpg’已建立
  6. 請選擇您要使用的密鑰種類:
  7. (1) RSA and RSA (default)
  8. (2) DSA and Elgamal
  9. (3) DSA (僅用於簽名)
  10. (4) RSA (僅用於簽名)
  11. 您的選擇? 1
  12. RSA 密鑰長度應在 1024 位與 4096 位之間。
  13. 您想要用多大的密鑰尺寸?(2048)1024
  14. 您所要求的密鑰尺寸是 1024
  15. 請設定這把密鑰的有效期限。
  16. 0 = 密鑰永不過期
  17. <n> = 密鑰在 n 天後過期
  18. <n>w = 密鑰在 n 周後過期
  19. <n>m = 密鑰在 n 月後過期
  20. <n>y = 密鑰在 n 年後過期
  21. 密鑰的有效期限是?(0) 0
  22. 密鑰永遠不會過期
  23. 以上正確嗎?(y/n)y

1到這裏會出現下面四個圖,意思是讓我們再對私鑰進行加密,我們一路回車,因為只是測試,所以就不再加密了。

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

一路回車過後,秘鑰生成完畢

  1. You need a user ID to identify your key; the software constructs the user ID
  2. from the Real Name, Comment and Email Address in this form:
  3. "Heinrich Heine (Der Dichter) <[email protected]>"
  4. 真實姓名:zhang
  5. 電子郵件地址:765030447@qq.com
  6. 註釋:test
  7. 您選定了這個用戶標識:
  8. zhang (test) <765030447@qq.com>”
  9. 更改姓名(N)、註釋(C)、電子郵件地址(E)或確定(O)/退出(Q)?o
  10. 您需要一個密碼來保護您的私鑰。
  11. 您不想要有密碼――這個想法實在是遭透了!
  12. 不過,我仍然會照您想的去做。您任何時候都可以變更您的密碼,僅需要
  13. 再次執行這個程序,並且使用“--edit-key”選項即可。
  14. 我們需要生成大量的隨機字節。這個時候您可以多做些瑣事(像是敲打鍵盤、移動
  15. 鼠標、讀寫硬盤之類的),這會讓隨機數字發生器有更好的機會獲得足夠的熵數。
  16. 我們需要生成大量的隨機字節。這個時候您可以多做些瑣事(像是敲打鍵盤、移動
  17. 鼠標、讀寫硬盤之類的),這會讓隨機數字發生器有更好的機會獲得足夠的熵數。
  18. gpg: /root/.gnupg/trustdb.gpg:建立了信任度數據庫
  19. gpg: 密鑰 11F74DDB 被標記為絕對信任
  20. 公鑰和私鑰已經生成並經簽名。
  21. gpg: 正在檢查信任度數據庫
  22. gpg: 需要 3 份勉強信任和 1 份完全信任,PGP 信任模型
  23. gpg: 深度:0 有效性: 1 已簽名: 0 信任度:0-,0q,0n,0m,0f,1u
  24. pub 1024R/11F74DDB 2018-01-06
  25. 密鑰指紋 = E1F8 DE1D B979 48F4 A216 DCB7 D5B9 E6A3 11F7 4DDB
  26. uid zhang (test) <765030447@qq.com>
  27. sub 1024R/710D443B 2018-01-06

我們看一下我們生成的秘鑰,在自己的家目錄下的.gnupg目錄下

  1. [root@newhostname .gnupg]# pwd
  2. /root/.gnupg
  3. [root@newhostname .gnupg]# ll
  4. 總用量 28
  5. -rw-------. 1 root root 7680 16 19:45 gpg.conf
  6. drwx------. 2 root root 6 16 19:45 private-keys-v1.d
  7. -rw-------. 1 root root 675 16 20:12 pubring.gpg
  8. -rw-------. 1 root root 675 16 20:12 pubring.gpg~
  9. -rw-------. 1 root root 600 16 20:12 random_seed
  10. -rw-------. 1 root root 1338 16 20:12 secring.gpg
  11. srwxr-xr-x. 1 root root 0 16 20:12 S.gpg-agent
  12. -rw-------. 1 root root 1280 16 20:12 trustdb.gpg
  13. pubring.gpg 這個是公鑰
  14. secring.gpg 這個是私鑰

我們導出公鑰

  1. [root@newhostname .gnupg]# gpg --list-key #可以查看公鑰
  2. /root/.gnupg/pubring.gpg
  3. ------------------------
  4. pub 1024R/11F74DDB 2018-01-06
  5. uid zhang (test) <765030447@qq.com>
  6. sub 1024R/710D443B 2018-01-06
  7. [root@newhostname .gnupg]# gpg -a --export -o zhang.pubkey #導出公鑰並生成可視的字符
  8. [root@newhostname .gnupg]# ls
  9. gpg.conf private-keys-v1.d pubring.gpg pubring.gpg~ random_seed secring.gpg S.gpg-agent trustdb.gpg zhang.pubkey
  10. [root@newhostname .gnupg]# ll
  11. 總用量 32
  12. -rw-------. 1 root root 7680 16 19:45 gpg.conf
  13. drwx------. 2 root root 6 16 19:45 private-keys-v1.d
  14. -rw-------. 1 root root 675 16 20:12 pubring.gpg
  15. -rw-------. 1 root root 675 16 20:12 pubring.gpg~
  16. -rw-------. 1 root root 600 16 20:12 random_seed
  17. -rw-------. 1 root root 1338 16 20:12 secring.gpg
  18. srwxr-xr-x. 1 root root 0 16 20:12 S.gpg-agent
  19. -rw-------. 1 root root 1280 16 20:12 trustdb.gpg
  20. -rw-r--r--. 1 root root 1020 16 20:37 zhang.pubkey
  21. [root@newhostname .gnupg]# cat zhang.pubkey #查看導出的公鑰
  22. -----BEGIN PGP PUBLIC KEY BLOCK-----
  23. Version: GnuPG v2.0.22 (GNU/Linux)
  24. mI0EWlC9RgEEAMaYQLcdCujOTwdKiBLPfSrhwceFqt7FGT/xBggSNHs8c9EyQ/8T
  25. ls7PiiaK4drm0mdPtIPr/pa6m2T20mFB0DMpeB9SEI+z7v/jWB/Y1xxINjGvFtKP
  26. JSwFSa8qYUNLBYpBIPoa5IlXpzrkG+gRvHh++7yu8e4e06oA9lAzWT2rABEBAAG0
  27. H3poYW5nICh0ZXN0KSA8NzY1MDMwNDQ3QHFxLmNvbT6IuQQTAQIAIwUCWlC9RgIb
  28. AwcLCQgHAwIBBhUIAgkKCwQWAgMBAh4BAheAAAoJENW55qMR903btFMEAIm/9HWT
  29. ofXbTTVUbjFMV8MSYU+TRj1n2xuPKDaWLgJyb2cH9c/57RMS5bHqvqEBxOzrtnXB
  30. 2FPYiS8VHgLhvnUkMqer1WA7RjVESAcp52UKvoI+7yD5K7vABMg9yDPnNag2EKLr
  31. H7db0wspYo5ox3w8AZsJj3dUMLgipAQDUpckuI0EWlC9RgEEAK0PxCRuHLL3XUC6
  32. wDr4gSRj5TVcwVuSOE0ECBZakbDeuljhq9Fn0UR8FfdiPOw4Cakt0RWiYh9oxdsF
  33. R3Y6DL6fzBAwozxz1I5NXtTj68HY+/6bEiHtz4xvFPR9YEwrqzPZWaWMaTRgtAL6
  34. n/ONYQKPHa82p2qIdzv7+y59u9zRABEBAAGInwQYAQIACQUCWlC9RgIbDAAKCRDV
  35. ueajEfdN204xA/9ZVVqFAnFti6vogSCzPMFDj6s4M66EBGEUIKkK9cnw1vzXzsv3
  36. v8YfevZsWGMX7Cag89a3ox+qIVvHhsEkL3TXEAmrYLZn9/T/E1yRQnYpQcI9uvMg
  37. /UAy3j0HtOzui4ofMuGoNJ1V7hOMt8Jlzt2+WQVvVogHcNWmEUSWcmIp8w==
  38. =o0NK
  39. -----END PGP PUBLIC KEY BLOCK-----
  40. [root@newhostname .gnupg]#

將公鑰文件拷貝到hostB機器

  1. [root@newhostname .gnupg]# scp zhang.pubkey 172.18.30.254:~
  2. root@172.18.30.254‘s password:
  3. zhang.pubkey 100% 1020 2.9MB/s 00:00

2)、在hostB上操作:

同樣的方法生成自己的密鑰對

  1. [root@joker-6-01 ~]# gpg --gen-key
  2. gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc.
  3. This is free software: you are free to change and redistribute it.
  4. There is NO WARRANTY, to the extent permitted by law.
  5. Please select what kind of key you want:
  6. (1) RSA and RSA (default)
  7. (2) DSA and Elgamal
  8. (3) DSA (sign only)
  9. (4) RSA (sign only)
  10. Your selection?
  11. RSA keys may be between 1024 and 4096 bits long.
  12. What keysize do you want? (2048) 1024
  13. Requested keysize is 1024 bits
  14. Please specify how long the key should be valid.
  15. 0 = key does not expire
  16. <n> = key expires in n days
  17. <n>w = key expires in n weeks
  18. <n>m = key expires in n months
  19. <n>y = key expires in n years
  20. Key is valid for? (0)
  21. Key does not expire at all
  22. Is this correct? (y/N)
  23. Key is valid for? (0)
  24. Key does not expire at all
  25. Is this correct? (y/N) y
  26. GnuPG needs to construct a user ID to identify your key.
  27. Real name: shang
  28. Email address: 765030447@qq.com
  29. Comment: test
  30. You selected this USER-ID:
  31. "shang (test) <[email protected]>"
  32. Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit?

查看秘鑰,並導出公鑰

  1. [root@joker-6-01 .gnupg]# gpg --list-key
  2. /root/.gnupg/pubring.gpg
  3. ------------------------
  4. pub 1024R/7FFA70D9 2018-01-06
  5. uid shang (test) <765030447@qq.com>
  6. sub 1024R/E774B778 2018-01-06
  7. [root@joker-6-01 .gnupg]# gpg -a --export -o shang.pubkey
  8. [root@joker-6-01 .gnupg]# ls
  9. gpg.conf private-keys-v1.d pubring.gpg pubring.gpg~ random_seed secring.gpg shang.pubkey trustdb.gpg
  10. [root@joker-6-01 .gnupg]# ll
  11. total 36
  12. -rw-------. 1 root root 7856 Nov 7 16:31 gpg.conf
  13. drwx------ 2 root root 4096 Jan 6 20:02 private-keys-v1.d
  14. -rw------- 1 root root 674 Jan 6 21:02 pubring.gpg
  15. -rw------- 1 root root 674 Jan 6 21:02 pubring.gpg~
  16. -rw------- 1 root root 600 Jan 6 21:02 random_seed
  17. -rw------- 1 root root 1338 Jan 6 21:02 secring.gpg
  18. -rw-r--r-- 1 root root 1016 Jan 6 21:05 shang.pubkey
  19. -rw-------. 1 root root 1280 Jan 6 21:02 trustdb.gpg
  20. [root@joker-6-01 .gnupg]# cat shang.pubkey
  21. -----BEGIN PGP PUBLIC KEY BLOCK-----
  22. Version: GnuPG v2.0.14 (GNU/Linux)
  23. mI0EWlDI3QEEANDr6fIRum7F1tdaJH6TI+O5QpKf3f1zEacmqqH3iQ+eIJkUZDTG
  24. yC9k87zKQVaDgbZlcn38Lf/u7X3pRemsGa8ZMkmviHCc7gfW5C0NmMse7/dKGUmt
  25. 6xOnVRsgk+WYDGwpxI0rhRxzDg0AIpNH20wZpK6bgNwoC8i5zIO605tRABEBAAG0
  26. H3NoYW5nICh0ZXN0KSA8NzY1MDMwNDQ3QHFxLmNvbT6IuAQTAQIAIgUCWlDI3QIb
  27. AwYLCQgHAwIGFQgCCQoLBBYCAwECHgECF4AACgkQNG0fpn/6cNkZ7wP+Oy6X24Tt
  28. WZQ5T4LYc9go7RuU8vDIMHQzEC0CXO9pi3ZU7quEYDfd5N9WD61+jKY9s79L09tK
  29. 14RQJByWaFFXJwWyrjzu4BzMZiAmjDf9PA5hpUVFOAdQbMKENlXOh3Rxsi65EsAH
  30. Hem/Plbr75dEN+CQV8emlZ2tDDBYhmbbkzC4jQRaUMjdAQQAzg1FrRac6KyerWt+
  31. oJ83B9eDLn6Yq9xBA6W1MdRTZKOEKygkbGP7Slr7e7lWHKinlfRL7+9+2IjzurQZ
  32. ndWO/msAKxdnqIMbg7Xx1eXWr0VwvFAz2AHDQz+Ls3mkJ40ZCPbHKXRb5+7USWKB
  33. VHCubgMJ49VfTrxBwzCo2M5K3+MAEQEAAYifBBgBAgAJBQJaUMjdAhsMAAoJEDRt
  34. H6Z/+nDZLW0EAJ3oHrPWIGczgzSZ+ociwsmaVggqAky3BCfWhbUgJBEOAEh+SMSw
  35. WueVIKNT/bA3SVUak0PmNz1IkXLRYmiPA/81+I9ezUx0ac1noYO2MzYfdmaCFSBM
  36. sfnfvpbk6/L0FY0aIhcuufCztIs+bsxpsXXYfq4C5hyMI1S1S8YS0pDX
  37. =DHkG
  38. -----END PGP PUBLIC KEY BLOCK-----
  39. [root@joker-6-01 .gnupg]#

將hostB的公鑰shang.pubkey拷貝到hostA

  1. [root@joker-6-01 .gnupg]# scp shang.pubkey 172.18.30.253:~
  2. The authenticity of host ‘172.18.30.253 (172.18.30.253)‘ can‘t be established.
  3. RSA key fingerprint is a4:04:ea:5b:ce:18:0c:e4:e8:b4:51:47:ea:87:0b:2b.
  4. Are you sure you want to continue connecting (yes/no)? yes
  5. Warning: Permanently added ‘172.18.30.253‘ (RSA) to the list of known hosts.
  6. root@172.18.30.253‘s password:
  7. shang.pubkey 100% 1016 1.0KB/s 00:00

A機器拿到了B的公鑰:shang.pubkey
B機器拿到了A的公鑰:zhang.pubkey

現在在各個機器上導入新拿到的公鑰

  1. hostA
  2. [root@newhostname app]# gpg --import ~/shang.pubkey
  3. gpg: 密鑰 7FFA70D9:公鑰“shang (test) <765030447@qq.com>”已導入
  4. gpg: 合計被處理的數量:1
  5. gpg: 已導入:1 (RSA: 1)
  6. hostB
  7. [root@joker-6-01 ~]# gpg --import zhang.pubkey
  8. gpg: key 11F74DDB: public key "zhang (test) <[email protected]>" imported
  9. gpg: Total number processed: 1
  10. gpg: imported: 1 (RSA: 1)

現在我們在A機器使用B的公鑰加密/app/fist_encrypt,並將生成的文件拷貝到B

  1. [root@newhostname app]# gpg -e -r shang fist_encrypt
  2. gpg: E774B778:沒有證據表明這把密鑰真的屬於它所聲稱的持有者
  3. pub 1024R/E774B778 2018-01-06 shang (test) <765030447@qq.com>
  4. 主鑰指紋: 5042 4F44 8DE2 F9EA 1A02 BBC8 346D 1FA6 7FFA 70D9
  5. 子鑰指紋: BB56 2ED6 84DA 2DB7 A6A6 AFF2 122E ED1C E774 B778
  6. 這把密鑰並不一定屬於用戶標識聲稱的那個人。如果您真的知道自
  7. 己在做什麽,您可以在下一個問題回答 yes。
  8. 無論如何還是使用這把密鑰嗎?(y/N)y
  9. [root@newhostname app]# ls
  10. fist_encrypt fist_encrypt.gpg

將新生成的加密文件拷貝到hostB上

  1. [root@newhostname app]# ls
  2. fist_encrypt fist_encrypt.gpg
  3. [root@newhostname app]# scp fist_encrypt.gpg [email protected]:/app
  4. root@172.18.30.254‘s password:
  5. fist_encrypt.gpg 100% 222 377.5KB/s 00:00
  6. [root@newhostname app]#

到B機器上,使用B的私鑰進行解密

  1. [root@joker-6-01 app]# ls
  2. fist_encrypt.gpg
  3. [root@joker-6-01 app]# cat fist_encrypt.gpg
  4. %qXd46-}a>y?;n$
  5. jtyY T>AWK}@jBRW1
  6. ~0>[root@joker-6-01 app]#
  7. [root@joker-6-01 app]#
  8. [root@joker-6-01 app]# gpg -d fist_encrypt.gpg > fist_encrypt #對文件進行解密,並輸出到fistt_encrypt內
  9. gpg: encrypted with 1024-bit RSA key, ID E774B778, created 2018-01-06
  10. "shang (test) <[email protected]>"
  11. [root@joker-6-01 app]# ls
  12. fist_encrypt fist_encrypt.gpg
  13. [root@joker-6-01 app]# cat fist_encrypt #解密完成
  14. Ok ok ok ok ok

對稱的加密解密完成了,現在我們刪除秘鑰

hostA上操作:

  1. [root@newhostname app]# gpg --delete-keys shang #在A上刪除B的公鑰
  2. gpg (GnuPG) 2.0.22; Copyright (C) 2013 Free Software Foundation, Inc.
  3. This is free software: you are free to change and redistribute it.
  4. There is NO WARRANTY, to the extent permitted by law.
  5. pub 1024R/7FFA70D9 2018-01-06 shang (test) <765030447@qq.com>
  6. 要從鑰匙環裏刪除這把密鑰嗎?(y/N)y
  7. [root@newhostname app]#
  1. [root@newhostname app]# gpg --delete-secret-keys zhang #在A機器上刪除自己的私鑰
  2. gpg (GnuPG) 2.0.22; Copyright (C) 2013 Free Software Foundation, Inc.
  3. This is free software: you are free to change and redistribute it.
  4. There is NO WARRANTY, to the extent permitted by law.
  5. sec 1024R/11F74DDB 2018-01-06 zhang (test) <765030447@qq.com>
  6. 要從鑰匙環裏刪除這把密鑰嗎?(y/N)y
  7. 這是一把私鑰!――真的要刪除嗎?(y/N)y
  8. [root@newhostname app]# gpg --delete-keys zhang #再刪除自己的公鑰
  9. gpg (GnuPG) 2.0.22; Copyright (C) 2013 Free Software Foundation, Inc.
  10. This is free software: you are free to change and redistribute it.
  11. There is NO WARRANTY, to the extent permitted by law.
  12. pub 1024R/11F74DDB 2018-01-06 zhang (test) <765030447@qq.com>
  13. 要從鑰匙環裏刪除這把密鑰嗎?(y/N)y
  14. 註意:必須先刪除私鑰再刪除公鑰

hostB上操作:

  1. [root@joker-6-01 app]# gpg --delete-keys zhang #在B上刪除A的公鑰
  2. gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc.
  3. This is free software: you are free to change and redistribute it.
  4. There is NO WARRANTY, to the extent permitted by law.
  5. pub 1024R/11F74DDB 2018-01-06 zhang (test) <765030447@qq.com>
  6. Delete this key from the keyring? (y/N) y
  7. [root@joker-6-01 app]#
  1. [root@joker-6-01 app]# gpg --delete-secret-keys shang #刪除自己的私鑰
  2. gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc.
  3. This is free software: you are free to change and redistribute it.
  4. There is NO WARRANTY, to the extent permitted by law.
  5. sec 1024R/7FFA70D9 2018-01-06 shang (test) <765030447@qq.com>
  6. Delete this key from the keyring? (y/N) y
  7. This is a secret key! - really delete? (y/N) y
  8. [root@joker-6-01 app]# gpg --delete-keys shang #刪除自己的公鑰
  9. gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc.
  10. This is free software: you are free to change and redistribute it.
  11. There is NO WARRANTY, to the extent permitted by law.
  12. pub 1024R/7FFA70D9 2018-01-06 shang (test) <765030447@qq.com>
  13. 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來加密數據