天天看點

PKI架構的簡介,如何使用OPENSSL完成加密與解密,如何自建CA完成證書的簽署

        PKI(Public Key Infrastructure ) 即"公鑰基礎設施",是一種遵循既定标準的密鑰管理平台,它能夠為所有網絡應用提供加密和數字簽名等密碼服務及所必需的密鑰和證書管理體系。

        認證中心CA 作為PKI 的核心部分,CA 實作了PKI 中一些很重要的功能,概括地說,認證中心(CA)的功能有:證書發放、證書更新、證書撤銷和證書驗證。CA 的核心功能就是發放和管理數字證書,具體描述如下: 

(1)接收驗證最終使用者數字證書的申請。 

(2)确定是否接受最終使用者數字證書的申請-證書的審批。 

(3)向申請者頒發、拒絕頒發數字證書-證書的發放。 

(4)接收、處理最終使用者的數字證書更新請求-證書的更新。 

(5)接收最終使用者數字證書的查詢、撤銷。 

(6)産生和釋出證書廢止清單(CRL)。 

(7)數字證書的歸檔。 

(8)密鑰歸檔。 

(9)曆史資料歸檔。

我們先來看看如何使用OPENSSL對資料進行加密與解密:

openssl enc -ciphername(算法名稱)

    -in filename

        the input filename, standard input by default. 輸入檔案名,标準的預設輸入。

    -out filename

        the output filename, standard output by default. 輸出檔案名,标準的預設輸出。

    -e 

        encrypt the input data: this is the default. 預設加密輸入的資料。

    -d

        decrypt the input data. 解密輸入的資料。

現在我們對/tmp下的fstab檔案進行加密:

# openssl idea-ofb -in fstab -e -out fstab.secret

這樣就對檔案完成加密了,目錄下是不是多了一個fstab.secret這個就是加密後的檔案。

我們看看加密檔案的内容:

# cat fstab.secret

全身亂碼看不懂吧,這時我們把源檔案fstab删除掉,這樣就很好的起到了保密工作。

# rm -f fstab

現在我們用這個加密的檔案,把我們源檔案給還原回來。

# openssl idea-ofb -in fstab.secret -d -out fstab

檔案回來了吧,而且内容也正常。

其他擴充的openssl指令:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<code>openssl: </code>

<code>    </code><code># openssl version</code>

<code>    </code> 

<code>    </code><code># 加密檔案</code>

<code>     </code><code>openssl enc -des3 -</code><code>in</code> <code>/path/to/somefile</code> <code>-e -out </code><code>/path/to/somefile</code><code>.des3</code>

<code>    </code><code># 解密檔案 </code>

<code>     </code><code>openssl enc -des3 -</code><code>in</code> <code>/path/to/somefile</code><code>.des3 -d -out </code><code>/path/to/somefile</code>

<code>    </code><code># 一緻性校驗</code>

<code>     </code><code>openssl dgst -md5 -hex </code><code>/path/to/somefile</code>

<code>     </code><code>md5sum </code>

<code>        </code> 

<code>        </code><code># 評估加密算法的加密速度 </code>

<code>     </code><code>openssl speed </code>

<code>        </code><code># 加密密碼  -salt 參數是在加密前給密碼加上其他字元一并加密</code>

<code>     </code><code>man</code> <code>sslpasswd</code>

<code>     </code><code>openssl </code><code>passwd</code> <code>-1 -salt </code>

<code>    </code><code># 生成随機密碼</code>

<code>     </code><code>openssl rand -base64 num</code>

<code>     </code><code>openssl rand -hex num</code>

如何使用openssl生成一個私鑰:

openssl genrsa -out filename [字元位數]

        generate an RSA private key  生成一個RSA 私鑰。

我們現在來生成一個2048位的私鑰:

# openssl genrsa -out mytckey 2048

# chmod 600 mytckey

私鑰是不能随便讓人看的,是以改下權限,除了自己,所有人都不能看。

提取公鑰:

# openssl rsa -in mytckey  -pubout

制作一個證書的簽署請求指令介紹:

openssl

    req

        PKCS#10 X.509 Certificate Signing Request (CSR) Management. 證書簽署請求管理。

    -new

        this option generates a new certificate request. 這個選項是生成一個新證書的請求。

    -key

        This specifies the file to read the private key from. It also accepts PKCS#8 format private keys for PEM format files.  這是指定讀取的私鑰在哪,也支援PKCS#8格式的私鑰,PEM格式的檔案。

    -out

        This specifies the output filename to write to or standard output by default  指定輸出檔案名

開始制作:

# openssl req -new -key mytckey  -out myreq.csr

接下來要輸入所在國家,省份,城市,部門等資訊。

這就建立好了一個證書的簽署請求。

CA才可以簽署證書

是以我們現在來看下,如何自建CA。

openssl配置檔案CA的部配置設定置:

# cd /etc/pki/CA/

1、為CA生成一個私鑰:

# (umask 077; openssl genrsa -out private/cakey.pem 4096)

 # openssl req -new -x509 -key private/cakey.pem  -out cacert.pem -days 3656 

自簽證書

# touch serial index.txt

# echo 01 &gt; serial

建立簽證資料庫檔案,和序列号檔案,并且給serial一個初始序列。

CA自建好了,現在可以給别人簽證了。

我們先來簽一個剛剛我們剛剛做好的請求

# openssl ca -in myreq.csr  -out mycert.crt -days 3655

這就給别人簽好了一張證書。

 接下來,我們在腳本裡實作自建CA和簽署證書的操作:

以下是小菜寫的腳本:

這一塊内容完成,有問題歡迎與我交流QQ1183710107。

本文轉自qw87112 51CTO部落格,原文連結:http://blog.51cto.com/tchuairen/1415425

繼續閱讀