標籤:數量級 linux 加密 基礎知識
一、基礎知識
對稱式加密:
加密和解密方使用同一個密鑰,用來解決資料機密性,但是密鑰通過何種方式傳遞給對方不容易實現;
公開金鑰加密:
密鑰是成對出現的,分別為Secret key(密鑰)和Public key(公開金鑰)公開金鑰加密必須使用與其相對應的私密金鑰進行解密並且公開金鑰是從私密金鑰中提取出來的,有私密金鑰可以知道公開金鑰是什麼,但是知道公開金鑰是不能知道私密金鑰的,公開金鑰是公開的,而私密金鑰是不公開的,但是公開金鑰加密比對稱式加密慢3個數量級(1000倍),加密速度相當的慢,所以單獨用此加密方式也比較困難;公開金鑰加密功能:
單向加密:
不是真正來實現加密,而是用來提取資料特徵碼,驗證資料完整性,他有雪崩效應((蝴蝶效應):來源資料有一點點不同,結果大不相同)和定長輸出(通過單相加密後的結果長度一樣)的特徵;
單向加密常見演算法:
md5:Message Degist,由麻省理工學院人工智慧實驗室研發,固定定長輸出
128bit;
md5sum:計算檔案md5格式的特徵值;
sha1:Secure Hash Algorithm 1,固定定長輸出160bit;
SHA256:長度越長變化越大;
SHA512: 長度越長變化越大;
二、加密過程:
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="wps_clip_image-9482" border="0" alt="wps_clip_image-9482" src="http://img1.51cto.com/attachment/201408/2/8400375_1406970986AxKC.png" height="303" />
1、發送方用單向密碼編譯演算法計算資料的特徵碼;
2、發送方用自己的私密金鑰加密這段特徵碼,並加在資料尾部;
3、發送方產生一個對稱金鑰;
4、發送方用此對稱金鑰密碼編譯資料和加密後特徵碼;
5、發送方用接收方的公開金鑰加密這個對稱金鑰,附加在密文的尾部,並發送之;
解密過程:
1、接收方用自己的私密金鑰解密加密過的對稱密碼;
2、接收方用密碼解密密文;
3、接收方用發送方的公開金鑰解密發送方私密金鑰加密的特徵碼;
4、接收方用同樣的單向密碼編譯演算法計算出未經處理資料的特徵碼;
5、接收方比較兩段特徵碼;
發送方: 計算資料特徵值----> 使用私密金鑰加密特徵值 ---> 隨機產生密碼對稱式加密整個資料 ---> 使用接受方公開金鑰加密密碼
接收方: 使用私密金鑰解密密碼 ----> 解密整個資料 ----> 使用公開金鑰驗證身份 ----> 比較資料特徵值
三、CA認證的作用
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="wps_clip_image-29214" border="0" alt="wps_clip_image-29214" src="http://img1.51cto.com/attachment/201408/2/8400375_1406970993USJ9.png" height="244" />
Ca 認證模型
1、金鑰交換金鑰交換,也稱為IKE:Internet Key Exchange 互連網金鑰交換;
2、數位簽章,用於身分識別驗證,防抵賴;
我們發現上述加密方式中嚴重依賴對方的公開金鑰,而怎樣才能確定是對方的公開金鑰呢,所以這就需要有一個專門機構來確保公開金鑰來源的合法性並給予對方一個數位憑證,其實數位憑證就像我們自己的***一樣,而那個專門機構就像是給我們頒發***的公安機關一樣,下面來說一下
數位憑證和CA(憑證授權單位);
數位憑證(Digital Certificate)包含認證擁有者資訊及公開金鑰還有CA的簽名並且附加CRL(憑證撤銷清單);
CA(憑證授權單位):對認證利用單向加密的演算法提取認證資料的特徵碼,然後用自己的私密金鑰加密特徵碼並附加在資料的尾部發送之;CA分為公用CA和私人CA兩類
PKI:
Public Key Infrastructure 是實現現代互連網電子商務的核心架構,只是一種規範,其核心就是CA,還有密鑰驗證和密碼交換器制,保證驗證過程是可靠的。
ssl(Secure Socket Layer)安全通訊端層;
四、linux加解密技術
在pcp5層模型中的AL(應用程式層)和TL(傳輸層)中間增加了半層就是ssl,其實ssl只是一種協議,需要靠其它軟體來現,這種軟體在linux中最通用和流行的就是openssl,目前openssl軟體版本
有一下幾種:
sslv1 已經廢棄;
sslv2 目前再用;
sslv3 目前再用;
TLS:
Transport Layer security(傳輸層安全)協議,最常用的是tlsv1版本(與 sslv3同時代);
linux上常用的加解密的工具有兩個:
1、gpg 實現rpm包簽名比較常用;
2、openssl ssl的開源版本的實現,一共有一下三個組件組成:
1)libcrypto 通用庫;
2)libssl 實現ssl和tls協議的;
3)openssl 多用途的加密組件;
這裡我們重點講解openssl:
openssl 是多用途的加密組件,命令列工具,其可以實現對稱式加密演算法、非對稱式加密演算法、單向密碼編譯演算法、作為一個簡單的CA (OpenCA),其依賴的設定檔預設在/etc/pki/tls/openssl.cnf中,也可以自己定義;openssl作為簡單的CA其頒發的認證格式有x509(比較通用,定義了認證的格式,類似於pkcs#10),pkcs#12和pkcs#17定義了如何將認證存諸的格式;openssl也可以實現認證格式的轉換。
openssl的常用工具:
1.資料的保密性
資訊加密就是把明碼的輸入檔案用密碼編譯演算法轉換成加密的檔案以實現資料的保密。加密的過程需要用到密匙來加密資料然後再解密。沒有了密鑰,就無法解開加密的資料。資料加密之後,只有密匙要用一個安全的方法傳送。加密過的資料可以公開地傳送。
2.資料的一致性
加密也能保證資料的一致性。例如:訊息驗證碼(MAC),能夠校正使用者提供的加密資訊,接收者可以用MAC來校正加密資料,保證資料在傳輸過程中沒有被篡改過。
3.安全驗證
加密的另外一個用途是用來作為個人的標識,使用者的密匙可以作為他的安全驗證的標識。SSL是利用公開密鑰的加密技術(RSA)來作為使用者端與伺服器端在傳送機密資料時的加密通訊協定。
五、實驗
openssl建立私人CA:
1、產生密鑰
2、自簽署認證
節點:
1、產生金鑰組兒
2、產生認證簽署請求
3、把請求發送給CA
CA:
1、驗正要求者資訊;
2、簽署認證;
3、把簽好的認證發送給要求者;
一、建立CA伺服器:
1、產生密鑰
# (umask 077; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="wps_clip_image-25335" border="0" alt="wps_clip_image-25335" src="http://img1.51cto.com/attachment/201408/2/8400375_14069710020hEG.png" height="252" />
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="wps_clip_image-31320" border="0" alt="wps_clip_image-31320" src="http://img1.51cto.com/attachment/201408/2/8400375_1406971017q7Vx.png" height="202" />
2、自簽認證
req: 產生認證簽署請求
-news: 新請求
-key /path/to/keyfile: 指定私密金鑰檔案
-out /path/to/somefile:
-x509: 產生自簽署認證
-days n: 有效天數
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="wps_clip_image-24547" border="0" alt="wps_clip_image-24547" src="http://img1.51cto.com/attachment/201408/2/8400375_1406971027mu3R.png" height="269" />
3、初始化工作環境
# touch /etc/pki/CA/{index.txt,serial}
# echo 01 > /etc/pki/CA/serial
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="wps_clip_image-12267" border="0" alt="wps_clip_image-12267" src="http://img1.51cto.com/attachment/201408/2/8400375_14069710313NSX.png" height="64" />
二、節點申請認證:
(一) 節點產生請求
1、產生金鑰組兒
# (umask 077; openssl genrsa -out /etc/httpd/ssl/httpd.key 2048)
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="wps_clip_image-29385" border="0" alt="wps_clip_image-29385" src="http://img1.51cto.com/attachment/201408/2/8400375_1406971042lqYD.png" height="190" />
2、產生認證簽署請求
# openssl req -new -key /etc/httpd/ssl/httpd.key -out /etc/httpd/ssl/httpd.csr
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="wps_clip_image-12925" border="0" alt="wps_clip_image-12925" src="http://img1.51cto.com/attachment/201408/2/8400375_14069710537UoE.png" height="245" />
3、把簽署請求檔案發送給CA服務
# scp
(二) CA簽署認證
1、驗正認證中的資訊;
2、簽署認證
# openssl ca -in /path/to/somefile.csr -out /path/to/somefile.crt -days N
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="wps_clip_image-14060" border="0" alt="wps_clip_image-14060" src="http://img1.51cto.com/attachment/201408/2/8400375_1406971059shSs.png" height="206" />
3、發送給要求者;
三、吊銷認證
(一)節點
1、擷取認證serial
# openssl x509 -in /path/to/certificate_file.crt -noout -serial -subject
(二) CA
2、根據節點提交的serial和subject資訊來驗正與index.txt檔案中的資訊是否一致;
3、吊銷認證
# openssl ca -revoke /etc/pki/CA/newcerts/SERIAL.pem
4、產生吊銷認證的編號(如果是第一次吊銷)
# echo 00 > /etc/pki/CA/crlnumber
5、更新憑證撤銷清單
# cd /etc/pki/CA/crl/
# openssl ca -gencrl -out thisca.crl
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="wps_clip_image-2476" border="0" alt="wps_clip_image-2476" src="http://img1.51cto.com/attachment/201408/2/8400375_1406971067q1Dy.png" height="195" />
如果需要,查看crl檔案的內容:
# openssl crl -in /path/to/crlfile.crl -noout -text
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="wps_clip_image-12592" border="0" alt="wps_clip_image-12592" src="http://img1.51cto.com/attachment/201408/2/8400375_1406971075PvB3.png" height="156" />
650) this.width=650;" style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;border-top:0px;border-right:0px;padding-top:0px;" title="wps_clip_image-23438" border="0" alt="wps_clip_image-23438" src="http://img1.51cto.com/attachment/201408/2/8400375_14069710836suC.png" height="181" />