數據的加密(Encryption)
說明:數據在傳輸過程中,爲了保障數據正確的到達通信的對方,需要把數據進行加密。
e.g: A <--------->B,A與B通信,當A給B傳遞信息時,要保證數據的完整性,還要保證信息的接受者是B,這時就需要對數據進行加密,反之,B給A傳遞信息時亦是如此。
現在一般採用的方法就是:
sender:
1. 計算數據的hash值
2. 使用自己的私鑰加密hash值,然後附加在數據的首部
3. 生成一對對稱密鑰
4. 使用此對稱密鑰加密數據和hash值
5. 使用接收方的公鑰加密加密此對稱密鑰,並附在密文尾部發送給接收方
生成私鑰有兩種方法:
(1)mkae
使用make生成私鑰時,需要切換到/etc/pki/tls/certs/目錄下,下面介紹生成私鑰的步驟:
1.cd /etc/pki/tls/certs/
2.make my.key(在此生成的私鑰必須以.key結尾)
[root@station45 certs]# make my.key
umask 77 ; \
/usr/bin/openssl genrsa -des3 1024 > my.key
Generating RSA private key, 1024 bit long modulus
.........++++++
..++++++
e is 65537 (0x10001)
Enter pass phrase:
Verifying - Enter pass phrase:
[root@station45 certs]#
生成的時候會讓你輸入加密此文件的密碼
(2)、openssl
openssl genrsa 1024 > test.key ,用此種方法生成時不需要選擇特定的地方,後綴名也不沒有特定的格式
[root@station45 test]# openssl genrsa 1024 > test.key
Generating RSA private key, 1024 bit long modulus
...........................++++++
....++++++
e is 65537 (0x10001)
[root@station45 test]# ls
test.key
[root@station45 test]#
提取此兩種方法生成的公鑰可以用同一種方法:
openssl rsa -in (私鑰文件) -out (公鑰文件)
注意:此時也會出現一些安全問題,A和B的公鑰會在互聯網上傳播,假如有第三者拿到A或者B的公鑰作一些改變,此時數據的傳輸依然達不到我們想要的效果。這時一般採用的方法就是把A和B的公鑰放在一個可信任的第三者處(CA),需要對方的公鑰就到CA處取,這時就需要A和B在CA處電子註冊。
(1)make 的註冊如下:
[root@station45 certs]# ls
ca-bundle.crt make-dummy-cert Makefile my.key
[root@station45 certs]# make my.csr
umask 77 ; \
/usr/bin/openssl req -utf8 -new -key my.key -out my.csr
Enter pass phrase for my.key:
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) [GB]:
這時會提示一些註冊信息,然後填上信息就可以註冊了。
[root@station45 certs]# ls
ca-bundle.crt make-dummy-cert Makefile my.csr my.key
[root@station45 certs]#
(2)openssl 的註冊如下:
[root@station45 test]# openssl req -new -key test.key -out my.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) [GB]:
填寫上信息就行可以註冊了。
然後可以查看註冊信息:
[root@station45 test]# openssl req -noout -in my.csr –text
在實驗室環境下我們可以自己製作一個CA:
1.cd /etc/pki/CA
2.openssl genrsa 2048 > private/cakey.pem
3.openssl req –new –x509 –key private/cakey.pem –out ./cacert.pem –days 2000
4.vim /etc/pki/tls/openssl.cnf
[ CA_default ]
dir = /etc/pki/CA # Where everything is kept
5mkdir ./newcerts
6. touch ./{serial,index.txt}
7. echo “XX” > ./serial XX爲兩位數
最後我們自己給自己頒發證書:
openssl ca –in test.csr –out test.csr
可以命令查看最後的證書:
openssl x509 –in test.csr –noout –text