加密解密基礎、PKI及SSL、創建私有CA

基本概念:

 

加密作爲數據安全保障的一種方式,它不是近代才產生的,歷史已經相當久遠;加密算法就是加密的方法.

 

數據加密解密:

數據加密的基本過程就是對原來爲明文的文件或數據按特定算法進行處理,使其成爲不可讀的一段代碼,通常稱爲"密文",使其只能在輸入相應的密鑰之後才能顯示出本來內容,通過這樣的途徑來達到保護數據不被非法人竊取、閱讀的目的。該過程的逆過程爲解密,即將該編碼信息轉化爲其原來數據的過程

 

爲什麼需要對數據加密:加密在網絡上的作用就是防止有用或私有化信息在網絡上被攔截和竊取;比如個人的銀行賬戶信息,電話號碼等泄露,引來的後果可想而知。

 

明文和密文

密碼學中,明文是指傳送方想要接收方獲得的可讀信息。 明文經過加密所產生的信息被稱爲密文,而密文經過解密而還原得來的信息被稱爲明文。

 

加密技術通常分爲兩大類:"對稱式"和"非對稱式"

 

對稱式加密:

對稱加密就是將信息使用一個密鑰進行加密解密時使用同樣的密鑰,同樣的算法進行解密。

 

特點:1.速度快,一般用於加密數據

  2.將原始數據分割成固定大小的塊,逐個進行加密;       

常用的加密算法:

  DES56bits):數據加密標準

  3DES

  AES128bits):高級加密標準

  Blowfish 對稱加密

  Twofish

  IDEA

  RC6

  CAST5

  Serpent

 

非對稱式加密:

 

加密和解密用的匙不同,通常一個是公開的,稱,另一個保密,稱

加密和解密所使用的不是同一個密,通常有兩個密,稱"公"和"私",它兩個必需配使用,否

能打開加密文件。裏的"公"是指可以外公佈的,"私"不能,只能由持有人一個人知道。它的優越性就

在這裏,因爲對稱式的加密方法如果是在網絡上傳輸加密文件就很難把密鑰告訴對方,不管用什麼方法都有可能

被別竊聽到。而非對稱式的加密方法有兩個密鑰,且其中的"公"是可以公開的,也就不怕人知道,收件人解

只要用自己的私即可以,這樣就很好地避免了密傳輸安全性問題

特點:相稱加密速度要慢,

算法:RSA,EIGmal,DSA

 

單向加密

一般不用於加密,而是提取數據的特徵碼。

特點:不可逆,通過算法計算從數據中提取到特徵碼,不能由特徵碼算出源數據

          算法:

      md5128bits

     sha1160bits

      sha512512bits

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

PKI概念:

 

公開密(英:Public Key Infrastructure,寫:PKI)

是一由硬件、件、參與者、管理政策與流程成的基架構,其目的在於造、管理、分配、使用、存

及撤數字證書

 

CA

數字證書認證機構(英:Certificate Authority,CA),也稱爲電子商務認證中心、子商務認證授權

機構,是負責發放和管理數字證書威機構,並作爲電子商交易中受信任的第三方,承擔公體系中公

合法性檢驗任。

 

CA 也有一個證書(內含公和私)。網上的公衆用過驗證 CA 的字從而信任 CA ,任何人都可以得到

CA 的證書(含公),用以驗證它所簽發證書

如果用想得到一份屬於自己的證書,他先向 CA 提出申。在 CA 判明申者的身份後,便他分配一個公

,並且 CA 將與申者的身份信息在一起,並字後,便形成證書發給者。

 

如果一個用鑑別另一個證書的真,他就用 CA 的公鑰對那個證書上的驗證,一旦驗證該證

就被認爲是有效的。證書實際是由證書籤證機關(CA)簽發的公認證

證書的內容包括:子簽證機關的信息、信息、公威機構的字和有效期等。

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

SSL介紹:

安全套接字(Secure Socket LayerSSL)協議是Web瀏覽器與Web服務器之間安全交換信息的協議,提供兩個基本的安全服務:鑑別與保密。

SSLNetscape1994年開發的,後來成爲了世界上最著名的web安全機制,所有主要的瀏覽器都支持SSL協議。

目前有三個版本:233.1,最常用的是第3版,是1995年發佈的。

SSL協議的三個特性

 保密:在握手協議中定義了會話密鑰後,所有的消息都被加密。

 鑑別:可選的客戶端認證,和強制的服務器端認證。

 完整性:傳送的消息包括消息完整性檢查(使用MAC)。

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

Openssl創建私有CA:

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

CA服務器自簽名

創建私有CA服務器

1.生成密鑰對

[root@ca ~]# cd /etc/pki/CA
[root@ca CA]#  (umask
077; openssl genrsa -out private/cakey.pem 2048)

2.CA服務器自簽名證書

[root@ca CA]#
openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365
You are about to be
asked to enter information that will be incorporated
into your
certificate request.
What you are about
to enter is what is called a Distinguished Name or a DN.
There are quite a
few fields but you can leave some blank
For some fields
there will be a default value,
If you enter '.',
the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN   #國家
State or Province Name (full name) []:Bei Jing  
#省份
Locality Name (eg, city) [Default City]:Bei Jing  
#城市
Organization Name (eg, company) [Default Company Ltd]:RA Company  
#公司
Organizational Unit Name (eg, section) []:IT  
#部門
Common Name (eg, your name or your server's hostname)
[]:ca.server.com   #主機名
Email Address []:[email protected]    #管理員郵箱


 

-new: 生成新的證書籤署請求;
-x509:直接輸出自簽署的證書文件,通常只有構建CA時才這麼用;
-key:私鑰文件路徑,用於提取公鑰;
-days N: 證書有效時長,單位爲“天”;
-out:輸出文件保存位置;

3.創建需要的文件:

 
[root@ca CA]# touch index.txt serial crlnumber  
#分別創建 :索引文件,證書序號,吊銷證書序號
[root@ca CA]# echo 01 >serial  #創建證書序列號 
-----------------------------------------------------------------------------------

1.客戶機申請證書步驟

1 生成私鑰;

[root@client tmp]#
(umask 077; openssl genrsa -out httpd.key 1024)
Generating RSA
private key, 1024 bit long modulus
........++++++
......++++++
e is 65537 (0x10001)
-------------------------------------------------------

2.生成證書籤署請求:

[root@client tmp]#
openssl req -new -key httpd.key -out httpd.csr
You are about to be
asked to enter information that will be incorporated
into your
certificate request.
What you are about
to enter is what is called a Distinguished Name or a DN.
There are quite a
few fields but you can leave some blank
For some fields
there will be a default value,
If you enter '.',
the field will be left blank.
-----
Country Name (2
letter code) [XX]:CN
State or Province
Name (full name) []:China
Locality Name (eg,
city) [Default City]:Shenzhen
Organization Name
(eg, company) [Default Company Ltd]:IT company
Organizational Unit
Name (eg, section) []:IT ops
Common Name (eg,
your name or your server's hostname) []:ca.server.com
Email Address
[]:[email protected]
 
Please enter the
following 'extra' attributes
to be sent with your
certificate request
A challenge password
[]:
An optional company
name []:

 

3.將請求文件發往CA服務器;

[root@client tmp]#
scp httpd.csr 10.76.249.100:/tmp/
The authenticity of
host '10.76.249.100 (10.76.249.100)' can't be established.
RSA key fingerprint
is 52:f6:17:d0:d7:cf:42:96:11:ae:45:ba:2e:fa:0f:61.
Are you sure you
want to continue connecting (yes/no)? yes
Warning: Permanently
added '10.76.249.100' (RSA) to the list of known hosts.
[email protected]'s
password:
httpd.csr                                                                   
100%  708     0.7KB/s  
00:00   
 
-----------------------------------------------------------------------------------------

CA服務器給客戶機簽署證書:

 

CA服務器簽證

[root@ca tmp]# openssl ca -in httpd.csr -out httpd.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 2 (0x2)
        Validity
            Not Before: Sep 24 15:47:13 2015 GMT
            Not After : Sep 23 15:47:13 2016 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = Bei Jing
            organizationName          = RA Company
            organizationalUnitName    = IT company
            commonName                = ca.server.com
            emailAddress              = [email protected]
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                93:7C:A2:62:5F:31:28:BC:BC:EA:D9:52:D1:3E:12:09:7E:DF:62:65
            X509v3 Authority Key Identifier: 
                keyid:3D:30:B1:5B:BF:68:40:53:4F:93:E0:74:75:AF:16:1F:1D:13:E5:4D
Certificate is to be certified until Sep 23 15:47:13 2016 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

 

 

將簽證好的cacert.pem證書發給win客戶端修改後綴名爲cacert.crt 即可導入

 

 

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

吊銷證書

 

(4) 吊銷證書

(a) 客戶端獲取要吊銷的證書的serial

# openssl x509 -in /PATH/FROM/CERT_FILE -noout -serial -subject

 

(b) CA

先根據客戶提交的serialsubject信息,對比檢驗是否與index.txt文件中的信息一致;

 

吊銷證書:

# openssl ca -revoke /etc/pki/CA/newcerts/SERIAL.pem

 

(c) 生成吊銷證書的編號(第一次吊銷一個證書)

# echo 01 > /etc/pki/CA/crlnumber

 

(d) 更新證書吊銷列表

# openssl ca -gencrl -out thisca.crl

 

查看crl文件:

# openssl crl -in /PATH/FROM/CRL_FILE.crl -noout -text

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


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