天天看點

加解密技術

               加解密技術

一.加密/解密

1.什麼是算法?什麼是密鑰?

算法:加密/解密所使用的轉換規則

密鑰:加密/解密所使用的指令或代碼

2.加密的目的和方式

--確定資料的機密性

加密的方式:

   對稱加密:加密/解密用同一個密鑰

   非對稱加密:加密/解密用不同的密鑰(公鑰/私鑰)

--保護資訊的完整性

   資訊摘要:基于輸入的資訊生成長度較短 位數固定的散列值

3.常見的加密算法:

對稱加密:

--DES,Data Encryption Standard 資料加密算法

--AES,Advanced Encryption Standard 進階加密算法

非對稱加密:

--RSA,Rivest Shamirh Adleman 由發明者的三個人名組成

--DSA,Digital Signature Algorithm 數字簽名算法

資訊摘要:

--MD5,Message Digest Algorithm 5

--SHA,Secure Hash Algorithm

4.MD5完整性檢驗

系統中的每個檔案都有一個唯一的MD5值,當檔案被修改之後,MD5值也會随之改變;

我們可以利用這一特性,通過檢視檔案的MD5值來判斷檔案是否經過修改,防止别人的“惡作劇”。

root@localhost test]# md5sum a.txt    //建立一個測試檔案,檢視一下MD5值 

bf7b0cb3765580ec2f58c215319aeea8  a.txt

[root@localhost test]# vim a.txt     //做不容易被發現的更改,比如在行尾添加一個空格

[root@localhost test]# md5sum a.txt     //再次檢視其MD5值

456466dcc2b97adf52a50c94183e7266  a.txt   //發現MD5值已經改變

5.GnuPG,加密工具

1)GnuPG,GUN Privacy Guard,最流行的資料加密,數字簽名工具軟體

[root@localhost test]# gpg --version    //檢視軟體版本

gpg (GnuPG) 2.0.14

libgcrypt 1.4.5

Copyright (C) 2009 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.

Home: ~/.gnupg

支援的算法:

公鑰:RSA, ELG, DSA

對稱加密:3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH, CAMELLIA128, 

              CAMELLIA192, CAMELLIA256

散列:MD5, SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224

壓縮:不壓縮, ZIP, ZLIB, BZIP2

二.使用GPG加/解密檔案

對稱方式:

--加密操作:-c或者--symmetric

--解密操作:-d或者--decrypt

[root@localhost test]# gpg -c test.txt

[root@localhost test]#ls    //加密後會生成一個gpg的檔案

test.txt test.txt.gpg

[root@localhost test]# file test.txt*    //檢視檔案類型

test.txt:     ASCII text

test.txt.gpg: data

[root@localhost test]# gpg -d test.txt.gpg > dtest.txt   //解密-d

非對稱方式:

工作原理:

公鑰解密,私鑰解密

 使用者A和使用者B加密通信,使用者B先生成一對密鑰對,然後使用者B把自己的公鑰上傳到網際網路上,使用者A擷取到使用者B的公鑰之後,用使用者B的公鑰進行加密,生成加密的資料,最後使用者B用自己的私鑰揭開加密資料,其他人由于沒有使用者B的私鑰,是以接不開加密資料。

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

2、bob要給alice發一個加密的檔案,需要使用alice的公鑰加密

*************************** 前期準備************************************

用ssh -X 的方式分别登入到bob和alice,不要用su的方式

--alice建立密鑰對:--gen-key

--alice導出公鑰:--export --armor 或-a

--bob導入公鑰:--import

2.1

[alice@rhel6-1 ~]$ gpg --gen-key      //按向導回答問題

gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc.

請選擇您要使用的密鑰種類:

  (1) RSA and RSA (default)

  (2) DSA and Elgamal

  (3) DSA (僅用于簽名)

  (4) RSA (僅用于簽名)

您的選擇?             //設定使用者資訊,私鑰密碼

... 

我們需要生成大量的随機位元組。這個時候您可以多做些瑣事(像是敲打鍵盤、移動

滑鼠、讀寫硬碟之類的),這會讓随機數字發生器有更好的機會獲得足夠的熵數。

我們需要生成大量的随機位元組。這個時候您可以多做些瑣事(像是敲打鍵盤、移動    

                       //過程中讓你填寫的密碼是用于保護私鑰的

公鑰和私鑰已經生成并經簽名。

gpg: 正在檢查信任度資料庫

gpg: 需要 3 份勉強信任和 1 份完全信任,PGP 信任模型

gpg: 深度:0 有效性:  1 已簽名:  0 信任度:0-,0q,0n,0m,0f,1u

pub   2048R/96A314F4 2014-04-26

密鑰指紋 = B9FE F46D 26AF B55F D0A3  94D7 1E4B 2764 96A3 14F4

uid                  alice <[email protected]>

sub   2048R/8805B9B4 2014-04-26

                       //看到類似資訊,表示密鑰對生成成功

[alice@rhel6-1 ~]$ gpg --list-keys

/root/.gnupg/pubring.gpg            //檢視公鑰詳細資訊

------------------------

[alice@rhel6-1 ~]$ gpg --list-secret        //檢視私鑰相關資訊

2.2  alice把公鑰傳遞給bob

[alice@rhel6-1 ~]$ gpg -a --export > /tmp/alice.pub    //上傳公鑰

選項-a代表用ASSIC碼的方式加密

2.3  bob收到alice的公鑰後,用其加密文檔

[bob@rhel6-1 ~]$ gpg --import /tmp/alice.pub         //導入公鑰

gpg: 密鑰 96A314F4:“alice <[email protected]>”未改變

gpg: 合計被處理的數量:1

gpg:           未改變:1

[bob@rhel6-1 ~]$ gpg --list-keys        //bob檢視導入的公鑰

/home/bob/.gnupg/pubring.gpg

----------------------------

pub   2048R/A3820BAE 2013-12-04

uid                  alice <[email protected]>

sub   2048R/60AFF7EC 2013-12-04

*************************加解密操作****************************************

--加密操作:--encrypt 或-e

--指定目标使用者:--recipient(受體)或-r

--解密操作:--decrypt或-d

加密檔案:

[bob@rhel6-1 ~]$ gpg -ear alice bob.txt

2.4  bob把加密後的檔案發給alice

[bob@rhel6-1 ~]$ cp bob.txt.asc /tmp/

2.5  alice解密檔案

[alice@rhel6-1 ~]$ cp /tmp/bob.txt.asc .

[alice@rhel6-1 ~]$ gpg bob.txt.asc        //用私鑰解密

[alice@rhel6-1 ~]$ ls

bob.txt  bob.txt.asc

[alice@rhel6-1 ~]$ cat bob.txt

this is from bob

一次加密傳輸至此完成!

三、使用GPG實作簽名

1、簽名可實作的功能:

(1)身份認證,表明使用者确實是他聲稱的那個人

(2)資料完整性,資料一旦被篡改,簽名就會失效

(3)認可:不可抵賴

2、alice發送簽名檔案給bob

2.1  alice簽名一個純文字檔案

[alice@rhel6-1 ~]$ echo 'alice file' > alice.txt

[alice@rhel6-1 ~]$ gpg -b alice.txt    //-b選項建立簽名

alice.txt  alice.txt.sig

2.2  把原文和簽名檔案發送給bob

[alice@rhel6-1 ~]$ cp alice.txt* /tmp/

2.3  驗證簽名

[bob@rhel6-1 ~]$ cp /tmp/alice.txt* .

[bob@rhel6-1 ~]$ ls

[bob@rhel6-1 ~]$ gpg --verify alice.txt.sig 

gpg: 于 2013年12月04日 星期三 15時59分51秒 CST 建立的簽名,使用 RSA,鑰匙号 A3820BAE

gpg: 完好的簽名,來自于“alice <[email protected]>”

gpg: 警告:這把密鑰未經受信任的簽名認證!

gpg:       沒有證據表明這個簽名屬于它所聲稱的持有者。

主鑰指紋: D780 8E96 D660 D394 1001  BF02 22BF 7AF9 A382 0BAE

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

[bob@rhel6-1 ~]$ vim alice.txt

gpg: 已損壞的簽名,來自于“alice <[email protected]>”

      本文轉自Jx戰壕  51CTO部落格,原文連結:,http://blog.51cto.com/xujpxm/1403527如需轉載請自行聯系原作者

繼續閱讀