加密和解密技術應用

加密方式

1、對稱加密:加解密使用相同的密鑰。算法:DES/ 3DES / AES。優點,加密效率高。缺點,密鑰的傳輸和保管不方便。

2、非對稱加密:公鑰加密、私鑰解密。算法:RSA。優點,密鑰傳輸保管方便。缺點,加密效率低。

3、單向加密:加密只能向一個方向進行,不能回推。哈希算法。MD5SHA

 一、使用gpg實現對稱加密

gpg  -c +文件  代表對文件進行加密命令

gpg+文件    代表對文件進行解密

1#  [root@fanlj ~]# ssh -X [email protected]

[fanlj@fanlj ~]$ echo 'this is from fanlj' >fanlj.txt
[fanlj@fanlj ~]$ gpg -c -a fanlj.txt

[fanlj@fanlj ~]$ ls
fanlj.txt  fanlj.txt.gpg
[fanlj@fanlj ~]$ cp fanlj.txt.gpg /tmp/

2、換另一個終端,fanxiaohui ssh上去

[root@fanlj ~]# ssh -X [email protected]
[fanxiaohui@fanlj ~]$ cp /tmp/fanlj.txt.gpg .

[fanxiaohui@fanlj ~]$ gpg fanlj.txt.gpg
[fanxiaohui@fanlj ~]$ cat fanlj.txt
this is from fanlj

二、使用gpg實現非對稱加密

1、非對稱密鑰成對出現,使用收件人的公鑰加密,發給收件人,收件人有一個與之對應的私鑰,才能解密。

 2fanxiaohui要給fanlj發一個加密的文件,需要使用fanlj的公鑰加密

2.1 fanlj生成一對密鑰

[fanlj@fanlj ~]$ 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.
請選擇您要使用的密鑰種類:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (僅用於簽名)
   (4) RSA (僅用於簽名)
您的選擇?
RSA 密鑰長度應在 1024 位與 4096 位之間。
您想要用多大的密鑰尺寸?(2048)
您所要求的密鑰尺寸是 2048 位
請設定這把密鑰的有效期限。
         0 = 密鑰永不過期
      <n>  = 密鑰在 n 天后過期
      <n>w = 密鑰在 n 周後過期
      <n>m = 密鑰在 n 月後過期
      <n>y = 密鑰在 n 年後過期
密鑰的有效期限是?(0)
密鑰永遠不會過期
以上正確嗎?(y/n)y
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]>"
真實姓名:fanlj
電子郵件地址:[email protected]
註釋:
您選定了這個用戶標識:
    “fanlj <[email protected]>”
更改姓名(N)、註釋(C)、電子郵件地址(E)或確定(O)/退出(Q)?O
您需要一個密碼來保護您的私鑰。

查看當前用戶導入的公鑰

[fanlj@fanlj ~]$ gpg --list-keys
/home/fanlj/.gnupg/pubring.gpg
------------------------------
pub   2048R/BA0F7D31 2015-10-21
uid                  fanlj <[email protected]>
sub   2048R/D67B4937 2015-10-21
查看當前用戶的私鑰

[fanlj@fanlj ~]$ gpg --list-secret
/home/fanlj/.gnupg/secring.gpg
------------------------------
sec   2048R/BA0F7D31 2015-10-21
uid                  fanlj <[email protected]>
ssb   2048R/D67B4937 2015-10-21

查看密鑰的指紋信息

[fanlj@fanlj ~]$ gpg --fingerprint
/home/fanlj/.gnupg/pubring.gpg
------------------------------
pub   2048R/BA0F7D31 2015-10-21
密鑰指紋 = CAED 8DB0 D6C7 E4BE 1EE4  C862 2643 245A BA0F 7D31
uid                  fanlj <[email protected]>
sub   2048R/D67B4937 2015-10-21
2.2 fanlj把公鑰傳遞給fanxiaohui

[fanlj@fanlj ~]$ gpg  -a --export   fanlj > /tmp/fanlj.pub
2.3 fanxiaohui收到fanlj的公鑰後,用其加密文檔

[fanxiaohui@fanlj ~]$ gpg --import /tmp/fanlj.pub
gpg: 已創建目錄‘/home/fanxiaohui/.gnupg’
gpg: 新的配置文件‘/home/fanxiaohui/.gnupg/gpg.conf’已建立
gpg: 警告:在‘/home/fanxiaohui/.gnupg/gpg.conf’裏的選項於此次運行期間未被使用
gpg: 鑰匙環‘/home/fanxiaohui/.gnupg/secring.gpg’已建立
gpg: 鑰匙環‘/home/fanxiaohui/.gnupg/pubring.gpg’已建立
gpg: /home/fanxiaohui/.gnupg/trustdb.gpg:建立了信任度數據庫
gpg: 密鑰 BA0F7D31:公鑰“fanlj <[email protected]>”已導入
gpg: 合計被處理的數量:1
gpg:           已導入:1  (RSA: 1)
查看導入的公鑰

[fanxiaohui@fanlj ~]$ gpg --list-keys
/home/fanxiaohui/.gnupg/pubring.gpg
-----------------------------------
pub   2048R/BA0F7D31 2015-10-21
uid                  fanlj <[email protected]>
sub   2048R/D67B4937 2015-10-21

加密文件:
[fanxiaohui@fanlj ~]$ echo my name is fanxiaohui > fanxiaohui.txt
[fanxiaohui@fanlj ~]$ gpg -ear fanlj fanxiaohui.txt
gpg: D67B4937:沒有證據表明這把密鑰真的屬於它所聲稱的持有者
pub  2048R/D67B4937 2015-10-21 fanlj <[email protected]>
 主鑰指紋: CAED 8DB0 D6C7 E4BE 1EE4  C862 2643 245A BA0F 7D31
 子鑰指紋: 4555 DF93 37C8 2D8E 13D3  593D A990 AB34 D67B 4937
這把密鑰並不一定屬於用戶標識聲稱的那個人。如果您真的知道自
己在做什麼,您可以在下一個問題回答 yes。
無論如何還是使用這把密鑰嗎?(y/N)y
2.4 bob把加密後的文件發給alice

[fanxiaohui@fanlj ~]$ cp fanxiaohui.txt.asc /tmp/
2.5 alice解密文件

[fanlj@fanlj ~]$ cp /tmp/fanxiaohui.txt.asc .
[fanlj@fanlj ~]$ gpg fanxiaohui.txt.asc
您需要輸入密碼,才能解開這個用戶的私鑰:“fanlj <[email protected]>”
2048 位的 RSA 密鑰,鑰匙號 D67B4937,建立於 2015-10-21 (主鑰匙號 BA0F7D31)
can't connect to `/home/fanlj/.gnupg/S.gpg-agent': 沒有那個文件或目錄
gpg: 由 2048 位的 RSA 密鑰加密,鑰匙號爲 D67B4937、生成於 2015-10-21
      “fanlj <[email protected]>”
[fanlj@fanlj ~]$ ls
fanxiaohui.txt  fanxiaohui.txt.asc
[fanlj@fanlj ~]$ cat fanxiaohui.txt
my name is fanxiaohui

三、使用GPG實現簽名

1、簽名可實現的功能:

1)身份認證,表明用戶確實是他聲稱的那個人

2)數據完整性,數據一旦被篡改,簽名就會失效

3)認可:不可抵賴

2fanlj發送簽名文件給fanxiaohui

2.1 fanlj簽名一個純文本文件

[fanlj@fanlj ~]$ echo 'fanlj file' > fanlj.txt
[fanlj@fanlj ~]$ gpg -b fanlj.txt
您需要輸入密碼,才能解開這個用戶的私鑰:“fanlj <[email protected]>”
2048 位的 RSA 密鑰,鑰匙號 BA0F7D31,建立於 2015-10-21
can't connect to `/home/fanlj/.gnupg/S.gpg-agent': 沒有那個文件或目錄
[fanlj@fanlj ~]$ ls
fanlj.txt  fanlj.txt.sig

2.2  把原文和簽名文件發送給bob

[fanlj@fanlj ~]$ cp fanlj.txt* /tmp/
2.3  驗證簽名

[fanxiaohui@fanlj ~]$ cp /tmp/fanlj.txt* .
[fanxiaohui@fanlj ~]$ ls
fanlj.txt  fanlj.txt.sig

[fanxiaohui@fanlj ~]$ gpg --verify fanlj.txt.sig
gpg: 於 2015年10月21日 星期三 11時28分54秒 CST 創建的簽名,使用 RSA,鑰匙號 BA0F7D31
gpg: 完好的簽名,來自於“fanlj <[email protected]>”
gpg: 警告:這把密鑰未經受信任的簽名認證!
gpg:       沒有證據表明這個簽名屬於它所聲稱的持有者。
主鑰指紋: CAED 8DB0 D6C7 E4BE 1EE4  C862 2643 245A BA0F 7D31

2.4  把alice.txt的內容稍做修改存盤,再次驗證

[fanxiaohui@fanlj ~]$ vim fanlj.txt
[fanxiaohui@fanlj ~]$ gpg --verify fanlj.txt.sig
gpg: 於 2015年10月21日 星期三 11時28分54秒 CST 創建的簽名,使用 RSA,鑰匙號 BA0F7D31
gpg: 已損壞的簽名,來自於“fanlj <[email protected]>”

總結:

       用戶可以通過加密、解密、簽名等技術,保證文件傳輸的安全保證.

加密的目的及方式:確保數據的機密性,保證信息的完整性


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章