天天看點

加密、解密原理和openssl自建CA

openssl協定簡介

SSL(Secure Socket

Layer)是netscape公司提出的主要用于web的安全通信标準。一般情況下的網絡協定應用中,資料在機器中經過簡單的由上到下的幾次包裝,就進入網絡,如果這些包被截獲的話,那麼可以很容易的根據網絡協定得到裡面的資料。

SSL就是為了加密這些資料而産生的協定,可以這麼了解,它是位與應用層和TCP/IP之間的一層,資料經過它流出的時候被加密,再往TCP/IP送,而資料從TCP/IP流入之後先進入它這一層被解密,同時它也能夠驗證網絡連接配接倆端的身份。是以對網絡中資料的加密解密的安全知識的了解就顯得尤為重要。

一、資訊安全的标準

網絡資訊安全與保密的三個要素(CIA):

保密性

完整性 可用性

除了CIA外,還有另外兩個标準也被經常提醒:

真實性

可追溯性

二、加密方式和算法

(1)對稱加密:采用單鑰密碼系統的加密方法,同一個密鑰可以同時用作資訊的加密和解密。

對稱加密的算法:

DES

: 資料加密标準(56位密鑰)

3DES

AES

:進階加密标準(128,192,256,384,512)

Blowfish

Twofish

IDEA

RC6

CAST5

對稱加密的特性:

a)加密、解密使用同一密碼;

b)将明文分隔成固定大小的塊,逐個進行加密

對稱加密的缺陷:

a)密鑰過多;

b)密鑰傳輸;

密鑰交換、身份驗正、資料完整性

(2)公鑰加密:由對應的一對唯一性密鑰(即公開密鑰和私有密鑰)組成的加密方法。

(公鑰是從私鑰中提取出來的。)

(公鑰加密,隻能私鑰解密。私鑰加密,也隻能公鑰解密。)

密鑰:public

key, secret key (p/s)

常用加密算法:

RSA,

DSA, EIGamal

(DSA:隻能用于身份驗證)

(3)單向加密:不可逆的加密

單向加密特性:

定長輸出:

無論原始資料是多大,結果大小都相同的

雪崩效應:

輸入的微小改變,将會引起結果的巨大改變

單向加密算法:MD5(128位)、SHA1、SHA256、SHA384、SHA512

三、加密過程及原理

四、自建私有CA過程

A

①生成密鑰

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

Generating RSA private key, 2048 bit long modulus

...............+++

....+++

e is 65537 (0x10001)

[root@bogon ~]#

②自簽證書 

 [root@bogon ~]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/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) []:Henan

Locality Name (eg, city) [Default City]:Zhenzhou

Organization Name (eg, company) [Default Company Ltd]:mageedu

Organizational Unit Name (eg, section) []:OPS

Common Name (eg, your name or your server's hostname) []:bogon

Email Address []:[email protected]

 ③初始化環境(第一次必須)

# touch /etc/pki/CA/{index.txt,serial}

# echo 01 >> /etc/pki/CA/serial   (指定序列号從那個數字開始)

B

①節點申請證書:

(1) 節點生成請求

mkdir /etc/httpd/ssl

[root@bogon ~]# ls /etc/httpd/

conf/    conf.d/  logs/    modules/ run/     ssl/ 

②生成密鑰對兒

[root@bogon ~]#  (umask 077; openssl genrsa -out /etc/httpd/ssl/httpd.key 2048)

..................................................................................................................................+++

................+++

[root@bogon ~]#................+++

③、生成證書簽署請求

[root@bogon ~]#  openssl req -new -key /etc/httpd/ssl/httpd.key -out /etc/httpd/ssl/httpd.csr

Country Name (2 letter code) [XX]:CN

Please enter the following 'extra' attributes

to be sent with your certificate request

A challenge password []:00woaioo

An optional company name []:00woaioo

C、 CA簽署證書

①、驗正證書中的資訊;

②、簽署證書

[root@bogon ~]# openssl ca -in /etc/httpd/ssl/httpd.csr -out /etc/httpd/ssl/httpd.crt -days 1000   

Using configuration from /etc/pki/tls/openssl.cnf

Check that the request matches the signature

Signature ok

Certificate Details:

        Serial Number: 1 (0x1)

        Validity

            Not Before: Aug  3 21:05:10 2014 GMT

            Not After : Apr 29 21:05:10 2017 GMT

        Subject:

            countryName               = CN

            stateOrProvinceName       = Henan

            organizationName          = mageedu

            organizationalUnitName    = OPS

            commonName                = bogon

            emailAddress              = [email protected]

        X509v3 extensions:

            X509v3 Basic Constraints:

                CA:FALSE

            Netscape Comment:

                OpenSSL Generated Certificate

            X509v3 Subject Key Identifier:

                CA:6B:7E:1F:CD:78:D7:E6:9D:EE:65:86:E0:F0:8C:A3:64:4D:01:B2

            X509v3 Authority Key Identifier:

                keyid:1E:B0:D5:A7:25:BF:58:40:13:76:10:6B:8E:F6:7B:BA:AB:8D:86:5A

Certificate is to be certified until Apr 29 21:05:10 2017 GMT (1000 days)

Sign the certificate? [y/n]:

③、發送給請求者;