導言:
我們知道在互聯網上進行文件傳輸、電子郵件商務往來存在許多不安全因素,特別是對于一些大公司和一些機密文件在網絡上傳輸,所以為了保證安全,我們必須給文件加密。今天,我們就來談談加密、解密、openssl的使用以及CA的實現過程。
數據的3大加密方式
對稱加密
含義
指加密解密使用同一組密鑰,是按數據分塊以后進行加密的,前后數據塊彼此之間有關聯關系。
特性
加密算法嚴重依賴口令,加密算法很有可能是公開的,所有人都有可能知道這個算法;加密本身不能僅依賴于算法,也依賴于可變化的口令來實現;加密方,解密方使用同一個口令。
對稱加密的相關算法
DES(56bit),3DES,AES(128bit),Blowfish,Twofish,Rc6,CAST5。
非對稱加密
含義
對消息的加密和解密使用不同的密鑰,即用于加密、可以公開的公鑰(public key),和用于解密、需要保密的私鑰(private key)。
特性
非對稱加密算法的保密性比較好,它消除了最終用戶交換密鑰的需要,但加密和解密花費時間長、速度慢,它不適合于對文件加密而只適用于對少量數據進行加密。
非對稱加密的相關算法
RSA,EIGamal,DSA。
單向加密
含義
是抽取數據特征碼來驗證數據的完整性。
特性
雪崩效應:輸入數據的微小改變會導致結果的巨大變化。
定長輸出:無論輸入數據多大,單向加密結果中的輸出長度是相同的。
openssl的使用
openssl的簡介:
openssl是一種多命令行工具,它能夠實現數據的加密、解密,還能當做CA來用,它能保證讓你創建證書、吊銷證書。
組成:
libcrypto:通用功能的加密庫
libssl:用于實現TLX/SSL的功能
openssl:多功能命令工具,用于生成密鑰,創建數字證書,手動加密解密數據
openssl實現的功能
生成密鑰、創建數字證書、創建CA、手動加密解密數據。
下面來介紹openssl的具體使用。
openssl中的命令
1.Standard commands
2.asn1parse ca ciphers cms
3.crl crl2pkcs7 dgst dh
4.dhparam dsa dsaparam ec
5.ecparam enc engine errstr
6.gendh gendsa genpkey genrsa
7.nseq ocsp passwd pkcs12
8.pkcs7 pkcs8 pkey pkeyparam
9.pkeyutl prime rand req
10.rsa rsautl s_client s_server
11.s_time sess_id smime speed
12.spkac ts verify version
13.x509
14.
15.Message Digest commands (see the `dgst' command for more details)
16.md2 md4 md5 rmd160
17.sha sha1
18.
19.Cipher commands (see the `enc' command for more details)
20.aes-128-cbc aes-128-ecb aes-192-cbc aes-192-ecb
21.aes-256-cbc aes-256-ecb base64 bf
22.bf-cbc bf-cfb bf-ecb bf-ofb
23.camellia-128-cbc camellia-128-ecb camellia-192-cbc camellia-192-ecb
24.camellia-256-cbc camellia-256-ecb cast cast-cbc
25.cast5-cbc cast5-cfb cast5-ecb cast5-ofb
26.des des-cbc des-cfb des-ecb
27.des-ede des-ede-cbc des-ede-cfb des-ede-ofb
28.des-ede3 des-ede3-cbc des-ede3-cfb des-ede3-ofb
29.des-ofb des3 desx idea
30.idea-cbc idea-cfb idea-ecb idea-ofb
31.rc2 rc2-40-cbc rc2-64-cbc rc2-cbc
32.rc2-cfb rc2-ecb rc2-ofb rc4
33.rc4-40 seed seed-cbc seed-cfb
34.seed-ecb seed-ofb zlib
說明:standard command中為標準命令,Message Digest commands中為生成信息摘要的命令,Cipher commands中為加密(enc)的命令。
使用enc加密數據:
語法結構:
1.openssl enc -ciphername [-in filename] [-out filename] [-pass arg] [-e] [-d] [-a/-base64] [-A] [-k password] [-kfile filename] [-K key] [-iv IV][-S salt] [-salt] [-nosalt] [-z] [-md] [-p] [-P] [-bufsize number] [-nopad] [-debug] [-none] [-engine id]
使用3DES的對稱加密方式,并使用salt(加點鹽),以增強安全性的方式加密/etc/fstab文件并生成密文文件fstab.cipher:
1.[root - www ~]#>openssl enc -e -des3 -a -salt -in /etc/fstab -out fstab.cipher
查看fstab.cipher已成加密后的文件
1.[root - www ~]#>cat fstab.cipher
2.U2FsdGVkX19hIUb2aVCcypDobX14dY/7FbJsjfIJwSYC/XCSJZ+0i9FyEwzyjawL
3.2uMTLvMD9/A0Py0XgG4WBxtGn5AmeYEdCOE5B8Gib4/iW6xCSCkyq2jDCvYiajJ6
4.o+7BGJfO1vTHyk24Nf2Z+n+u+hzwjtwtJe99Z/supkWBEyDH4McKfOBC8CIJdDci
5.cm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des3 -a -salt -out test-openssl -in fstab.cipher
查看解密后的文件fstab.clear
1.[root - www ~]#>cat test-openssl
2.
3.#
4.# /etc/fstab
5.# Created by anaconda on Wed Nov 9 00:19:14 2016
6.#
7.# Accessible filesystems, by reference, are maintained under '/dev/disk'
8.# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
9.#
10./dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
11.UUID=8d6c4661-f3e4-456f-ba07-96202e9c985a /boot ext4 defaults 1 2
12./dev/mapper/VolGroup-lv_home /home ext4 defaults 1 2
13./dev/mapper/VolGroup-lv_swap swap swap defaults 0 0
14.tmpfs /dev/shm tmpfs defaults 0 0
15.devpts /dev/pts devpts gid=5,mode=620 0 0
16.sysfs /sys sysfs defaults 0 0
17.proc /proc proc defaults 0 0
使用openssl dgst獲取數據的特征碼:
語法結構:
1.openssl dgst [-md5|-md4|-md2|-sha1|-sha|-mdc2|-ripemd160|-dss1] [-c] [-d] [-hex] [-binary] [-out filename] [-sign filename] [-keyform arg][-passin arg] [-verify filename] [-prverify filename] [-signature filename] [-hmac key] [file...][md5|md4|md2|sha1|sha|mdc2|ripemd160] [-c] [-d] [file...]
使用單向加密的算法工具sha512獲取文件fstab.cipher的特征碼
1.[root - www ~]#>openssl dgst -sha512 test-openssl
2.SHA512(test-openssl)= 4ae7b4173d3e13404a82047207c405f7b33b4eacb88e8bac5725e51f3916990c21617d907b8f88109bd3014ed43d7ae0d228ced16650a6fcd026cd4160c3b639
使用openssl passwd生成用戶認證的密碼
語法結構:
1.openssl passwd [-crypt] [-1] [-apr1] [-salt string] [-in file] [-stdin] [-noverify] [-quiet] [-table] {password}
使用openssl passwd生成密碼
1.[root - www ~]#>openssl passwd -1 -salt 1234561
2.Password:
3.$1$1234561$nG4PnFet7QHl/5DWjRb/S1
4.[root - www ~]#>openssl passwd -1 -salt 123456
5.Password:
6.$1$123456$xReuMlWnj5YwaBv2OBHMR0
使用openssl passwd輸入的密碼后生成的密碼一樣,但salt不同,最后加密后的結果則完全不同。
使用openssl speed來測試當前主機的加密速度
1.[root - www ~]#>openssl speed sha512
2.Doing sha512 for 3s on 16 size blocks: 7970707 sha512's in 3.00s
3.Doing sha512 for 3s on 64 size blocks: 8025641 sha512's in 3.00s
4.Doing sha512 for 3s on 256 size blocks: 3094489 sha512's in 3.00s
5.Doing sha512 for 3s on 1024 size blocks: 1101347 sha512's in 3.00s
6.Doing sha512 for 3s on 8192 size blocks: 158289 sha512's in 3.01s
7.OpenSSL 1.0.1e-fips 11 Feb 2013
8.built on: Tue Sep 27 12:27:19 UTC 2016
9.options:bn(64,64) md2(int) rc4(16x,int) des(idx,cisc,16,int) aes(partial) idea(int) blowfish(idx)
10.compiler: gcc -fPIC -DOPENSSL_PIC -DZLIB -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -DKRB5_MIT -m64 -DL_ENDIAN -DTERMIO -Wall -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Wa,--noexecstack -DPURIFY -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DOPENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DMD5_ASM -DAES_ASM -DVPAES_ASM -DBSAES_ASM -DWHIRLPOOL_ASM -DGHASH_ASM
11.The 'numbers' are in 1000s of bytes per second processed.
12.type 16 bytes 64 bytes 256 bytes 1024 bytes 8192 bytes
13.sha512 42510.44k 171213.67k 264063.06k 375926.44k 430798.50k
使用openssl rand生成隨機數,并把它充當salt
1.[root - www ~]#>openssl passwd -1 -salt `openssl rand -hex 5`
2.Password:
3.$1$c9639777$gf7R6uRxD6kCbIBGknzkf.
4.[root - www ~]#>openssl passwd -1 -salt `openssl rand -hex 5`
5.Password:
6.$1$53966a1f$HCpDJJ1izdxwQQhjtkloc0
公鑰加密:(發送方用接收方的公鑰加密,接收方用自己的私鑰解密)
密鑰對兒:(公鑰:pkey和私鑰:skey)
算法:RSA,EIGamal
公鑰加密通常用來實現密鑰交換和身份認證,不是用來數據加密。
公鑰加密的工具:gpg,openssl rsautl
公鑰加密的延伸:
數字簽名:發送方用自己的私鑰加密,接收方用發送方的公鑰解密。
數字簽名會加密語言數據的特征碼,而不會加密語言數據本身。
算法:RSA,EIGamal,DSA(只能用來做簽名,而無法用來加密)
DSA:digital signature algorithm
DSS: digital signature standard(只能私鑰加密,公鑰解密)
密鑰交換:IKE
算法:DH(diffie-Hellman),公鑰加密
數字證書:公鑰加密的另一個應用 。
證書格式:x509
x509證書包含:
1.公鑰和有效期限;
2.持有者的個人合法身份信息;
3.證書的使用方式;
4.CA頒發機構的信息;
5.CA的數字簽名。
用openssl實現私有CA:
openssl的配置文件:/etc/pki/tls/openssl.cnf
CA的工作目錄:/etc/pki/CA
吊銷列表的存放位置:/etc/pki/CA/crl
剛簽署的證書的存放位置:/etc/pki/CA/newcerts
CA自身的公鑰的位置:/etc/pki/CA/cacert.pem
發出的證書的編號位置:/etc/pki/CA/serial
為吊銷的證書編號的位置:/etc/pki/CA/crlnumber
CA自己的私鑰位置:/etc/pki/CA/private/cakey.pem
隨機數文件:/etc/pki/CA/private/.rand
測試為centos創建CA
環境前提
CA服務器主機:centos7(192.168.2.104)
CA客戶機主機:centos6(192.168.2.103)
CA服務器主機:centos7(192.168.2.104)配置搭建CA服務器
1.[root - localhost ~]#>cd /etc/pki/CA/
2.[root - localhost /etc/pki/CA]#>(umask 077;openssl genrsa -out private/cakey.pem 2048)
3.Generating RSA private key, 2048 bit long modulus
4...................................+++
5................................................................................................+++
6.e is 65537 (0x10001)
7.[root - localhost /etc/pki/CA]#>openssl req -new -x509 -key private/cakey.pem -out cacert.pem
8.You are about to be asked to enter information that will be incorporated
9.into your certificate request.
10.What you are about to enter is what is called a Distinguished Name or a DN.
11.There are quite a few fields but you can leave some blank
12.For some fields there will be a default value,
13.If you enter '.', the field will be left blank.
14.-----
15.Country Name (2 letter code) [XX]:CN
16.State or Province Name (full name) []:Beijing
17.Locality Name (eg, city) [Default City]:Beijing
18.Organization Name (eg, company) [Default Company Ltd]:gwx
19.Organizational Unit Name (eg, section) []:OPS
20.Common Name (eg, your name or your server's hostname) []:www.gwx.com
21.Email Address []:admin@gwx.com
22.[root - localhost /etc/pki/CA]#>touch serial index.txt
23.[root - localhost /etc/pki/CA]#>echo 01 >> serial
24.[root - localhost /etc/pki/CA]#>cat serial
25.01
CA客戶機主機:centos6(192.168.2.103)配置發送CA申請
1.[root - localhost ~]#>cd /etc/httpd/
2.conf/ conf.d/ logs/ modules/ run/
3.[root - localhost ~]#>cd /etc/httpd/
4.[root - localhost /etc/httpd]#>mkdir ssl
5.[root - localhost /etc/httpd]#>cd ssl/
6.[root - localhost /etc/httpd/ssl]#>(umask 077;openssl genrsa -out httpd.key 1024)
7.Generating RSA private key, 1024 bit long modulus
8...............++++++
9.................................++++++
10.e is 65537 (0x10001)
11.[root - localhost /etc/httpd/ssl]#>openssl req -new -key httpd.key -out httpd.csr
12.You are about to be asked to enter information that will be incorporated
13.into your certificate request.
14.What you are about to enter is what is called a Distinguished Name or a DN.
15.There are quite a few fields but you can leave some blank
16.For some fields there will be a default value,
17.If you enter '.', the field will be left blank.
18.-----
19.Country Name (2 letter code) [XX]:CN
20.State or Province Name (full name) []:Beijing
21.Locality Name (eg, city) [Default City]:Beijing
22.Organization Name (eg, company) [Default Company Ltd]:gwx
23.Organizational Unit Name (eg, section) []:ops
24.Common Name (eg, your name or your server's hostname) []:www.gwx1.com
25.Email Address []:admin@gwx1.com
26.
27.Please enter the following 'extra' attributes
28.to be sent with your certificate request
29.A challenge password []:
30.An optional company name []:
31.#之后將生成的httpd.csr文件傳送給CA服務器主機
32.[root - localhost /etc/httpd/ssl]#>scp httpd.csr root@192.168.2.104:/tmp/
33.root@192.168.2.104's password:
34.httpd.csr 100% 688 0.7KB/s 00:00
CA服務器主機:centos7(192.168.2.104)配置簽發CA
1.[root - localhost ~]#>cd /etc/pki/CA/
2.[root - localhost /etc/pki/CA]#>openssl ca -in /tmp/httpd.csr -out certs/httpd.crt
3.Using configuration from /etc/pki/tls/openssl.cnf
4.Check that the request matches the signature
5.Signature ok
6.Certificate Details:
7. Serial Number: 1 (0x1)
8. Validity
9. Not Before: Dec 5 09:21:14 2016 GMT
10. Not After : Dec 5 09:21:14 2017 GMT
11. Subject:
12. countryName = CN
13. stateOrProvinceName = Beijing
14. organizationName = gwx
15. organizationalUnitName = ops
16. commonName = www.gwx1.com
17. emailAddress = admin@gwx1.com
18. X509v3 extensions:
19. X509v3 Basic Constraints:
20. CA:FALSE
21. Netscape Comment:
22. OpenSSL Generated Certificate
23. X509v3 Subject Key Identifier:
24. 61:FF:12:1A:11:11:14:C0:90:17:6C:A2:36:6D:56:03:E0:33:55:5E
25. X509v3 Authority Key Identifier:
26. keyid:40:1D:D3:F6:74:8E:AA:4C:94:08:31:4E:C5:56:9F:78:B1:25:26:15
27.
28.Certificate is to be certified until Dec 5 09:21:14 2017 GMT (365 days)
29.Sign the certificate? [y/n]:y
30.
31.1 out of 1 certificate requests certified, commit? [y/n]y
32.Write out database with 1 new entries
33.Data Base Updated
34.[root - localhost /etc/pki/CA]#>scp certs/httpd.crt root@192.168.2.103:/etc/httpd/ssl
35.The authenticity of host '192.168.2.103 (192.168.2.103)' can't be established.
36.RSA key fingerprint is c0:96:8e:a9:f2:92:da:88:31:2e:06:69:4f:a8:65:b9.
37.Are you sure you want to continue connecting (yes/no)? yes
38.Warning: Permanently added '192.168.2.103' (RSA) to the list of known hosts.
39.root@192.168.2.103's password:
40.httpd.crt 100% 3821 3.7KB/s 00:00
訪問測試
為CA客戶機添加mod_ssl模塊
1.[root - localhost /etc/httpd/modules]#>yum install mod-ssl -y
2.#...安裝過程略...
為CA客戶機添加一個測試頁面
1.[root - localhost ~]#>vi /var/www/html/index.html
1.<h1>This is a test page for 192.168.2.103 to ca</h1>
編輯httpd配置文件
1.[root - localhost ~]#>vi /etc/httpd/conf/httpd.conf
1.ServerName www.gwx1.com:443
2.DocumentRoot "/var/www/html"
訪問機設置與訪問
將目標主機添加進本機hosts文件
測試訪問
原創文章,作者:N24-wenxuan,如若轉載,請注明出處:http://www.www58058.com/62693