马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
目录
- 网络安全配景
- 数据加密算法
- 普通算法
- 对称加密
- 非对称加密
- 总结
- 如何安全获取公钥
- Openssl软件
- openssl实践
- openssl下令行
- Nginx摆设https
- 摆设https实践
- 使用腾讯云获取证书
网络安全配景
网络就是实现不同主机之间的通讯。网络出现之初利用TCP/IP协议簇的相干协议概念,已经满足了互连两台主机之间可以举行通讯的目的,虽然看似简简朴单几句话,就形貌了网络概念与网络出现的目的,但是为了真正实现两台主机之间的稳定可靠通讯,其实是一件非常困难的事变了,如果还要再通讯的基础上保证数据传输的安全性,可想而知,绝对是难上加难,因此,网络发明之初,并没有太关注TCP/IP互联协议中的安全问题。
对于默认的两台主机而言,早期传输数据信息并没有通过加密方式传输数据,装备两端传输的数据本身实际是明文的,只要能截取到传输的数据包,就可以直接看到传输的数据信息,所以根本没有安全性可言。
数据加密算法
网络安全涉及很多方面,而网络数据的安全传输通常碰面对以下几方面的威胁:
1.数据窃听与机密性: 即怎样保证数据不会因为被截获或窃听而袒露。
2.数据窜改与完备性: 即怎样保证数据不会被恶意窜改。
3.身份假冒与身份验证: 即怎样保证数据交互双方的身份没有被假冒。
针对以上几个问题,可以用以下几种数据加密方式来解决(每种数据加密方式又有多种不同的算法实现):
数据加密方式 | 形貌 | 主要解决的问题 | 常用算法 | 对称加密 | 指数据加密息争密使用相同的密钥 | 数据的机密性 | DES,AES | 非对称加密 | 也叫公钥加密,指数据加密息争密使用不同的密钥-密钥对 | 身份验证 | DSA,RSA | 单向加密 | 指只能加密数据,而不能解密数据 | 数据的完备性 | MD5,SHA系列算法 |
普通算法
1
2
3
4
| 网站此时利用普通常见算法,针对暗码加密,基本保证数据再传输时间,不是明文
但是黑客大概会有一个庞大的算法本,里面记载了常见的暗码和加密后的密文,一一对应,然后可以利用步调大量计算,反解出明文暗码
撞库是黑客通过网络互联网已泄漏的用户和暗码信息,天生对应的字典表,尝试批量登陆其他网站后,得到一系列可以登录的用户。 很多用户在不同网站使用的是相同的帐号暗码,因此黑客可以通过获取用户在A网站的账户从而尝试登录B网址,这就可以理解为撞库攻击。
|
对称加密
1
2
3
4
5
6
7
| 对称加密算法,如其名,就是使用同一个秘钥举行加密息争密。
优点是速度较快,适合对数据量比较大的数据举行加密。
缺点是密钥的保存方式须要保证,一旦加密大概解密的哪一方泄漏了密钥,都会导致信息的泄漏。
常用的对称加密算法有 ES、3DES、DESX、Blowfish、IDEA、RC4、RC5、RC6、AES。
|
非对称加密
1
2
3
4
5
6
| 简朴理解,比喻关系
公钥---锁
私钥---开锁钥匙
私钥只能由一方安全保管,不能外泄,而公钥则可以发给任何请求它的人。非对称加密使用这对密钥中的一个举行加密,而解密则须要另一个密钥。
好比,你向银行请求公钥,银行将公钥发给你,你使用公钥对消息加密,那么只有私钥的持有人--银行才能对你的消息解密。与对称加密不同的是,银行不须要将私钥通过网络发送出去,因此安全性大大提高。
|
常用的非对称假面算法有:RSA,ECC,Diffie-Hellman,El Gamal,DSA(数字签名用)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| 虽然非对称加密很安全,但是和对称加密比起来,它非常的慢,所以我们还是要用对称加密来传送消息,但对称加密所使用的密钥我们可以通过非对称加密的方式发送出去。为了解释这个过程,请看下面的例子:
1.使用对称加密,速度快
2.使用非对称加密的方式,针对第一步的秘钥加密传输
(1) 超哥须要在银行的网站做一笔生意业务,他的浏览器起首天生了一个随机数作为对称密钥。
(2) 超哥的浏览器向银行的网站请求公钥(非对称加密的公钥~~)。
(3) 银行将公钥发送给超哥。
(4) 超哥的浏览器使用银行的公钥将自己的对称密钥加密(这里是重点,已经是【对称加密+非对称加密】)。
(5) 超哥的浏览器将加密后的对称密钥发送给银行。
(6) 银利用用私钥解密得到超哥浏览器的对称密钥。
(7) 超哥与银行可以使用对称密钥来对沟通的内容举行加密与解密了。
|
总结
1
2
3
4
5
| 1.对称加密方式,加密解密使用同样的密钥,速度较快,但是密钥须要在网络中传输,因此大概被截获,不安全
2.非对称加密,使用一对密钥,公钥,私钥,因此安全性很高,但是加密息争密速度很慢
3.常用的方式是,结合两种加密方式,效率与安全性都有又了保障。
|
如何安全获取公钥
1) A和B端起首天生自己的公钥和私钥的密钥对,为了使对方能相信自己的公钥信息,将自己的公钥信息告知给第三方发证机构,利用第三方机构对自己的公钥举行公证。第三方机构会制作一个数字证书(机构 编号 以及发证机构的戳),并且第三方机构也要给自己设置一个合法的公钥和私钥,并且公钥设置为第三方机构的公钥证书。
2) 发证构造计算出数字证书数据的特征码,并用自己的私钥举行加密,并将加密的信息附加到特征码后成为数字签名。
3) A和B两端获得公正过的证书信息,并通过证书信息传递,得到对方的公钥。
4) A和B两端与第三方机构创建连接,获得第三方证书,通过第三方证书获得第三方公钥,利用第三方公钥只要能解密数字签名即可。
回到顶部
Openssl软件
在传输层和应用层之间。ssl层
回到顶部
openssl实践
openssl由三部门构成:
1.libcrpto:通用加密库
2. libssl:TSL/SSL构成库,基于会话实现了身份认证,数据加密和会话完备性。
3. openssl:提供下令行工具,例如模拟创建证书。
回到顶部
openssl下令行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| # 查抄版本
[root@chaogelinux ~]# openssl version
OpenSSL 1.0.2k-fips 26 Jan 2017
# 配置文件
[root@chaogelinux ~]# cat /etc/pki/tls/openssl.cnf
#获取openssl下令操作
[root@chaogelinux ~]# openssl -?
openssl:Error: '-?' is an invalid command.
# 标准下令
Standard commands
asn1parse ca ciphers cms
crl crl2pkcs7 dgst dh
dhparam dsa dsaparam ec
...
# 信息摘要下令,单向加密下令
Message Digest commands (see the `dgst' command for more details)
md2 md4 md5 rmd160
sha sha1
# 加密下令
Cipher commands (see the `enc' command for more details)
aes-128-cbc aes-128-ecb aes-192-cbc aes-192-ecb
aes-256-cbc aes-256-ecb base64 bf
bf-cbc bf-cfb bf-ecb bf-ofb
# 测试机器对openssl支持的加密算法举行速度测试,检测服务器性能
openssl speed
|
【openssl加密下令】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| 语法
openssl enc -加密算法 -in filename -out filename
# 对文件加密
[root@chaogelinux ~]# cat my.pwd
yu:123
[root@chaogelinux ~]#
[root@chaogelinux ~]#
# 输入暗码后加密乐成,超哥这里是888888
[root@chaogelinux ~]# openssl enc -des3 -salt -a -in my.pwd -out my.pwd.des3
enter des-ede3-cbc encryption password:
Verifying - enter des-ede3-cbc encryption password:
#解密文件
[root@chaogelinux ~]# cat my.pwd.des3
U2FsdGVkX19ZDOFtbdZz3QNu+bxm3DKd
[root@chaogelinux ~]#
# 输入暗码后,解密文件
[root@chaogelinux ~]# openssl enc -des3 -d -salt -a -in my.pwd.des3 -out my.pwd.src
enter des-ede3-cbc decryption password:
[root@chaogelinux ~]#
[root@chaogelinux ~]# cat my.pwd.src
yu:123
参数解释:
enc openssl把浩繁的对称加密算法,同一集成在了enc指令,用法是 enc -算法名
-des3 指定算法
-d 指定解密
-e 指定加密
-salt 暗码加盐,防止暗码被破解
-a 基于base64位编码,可选参数
| 【openssl创建私有证书】
1.创建私钥和公钥,模拟证书颁发机构天生证书,天生一对密钥
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| # 指定私钥长度1024,并且将天生的私钥信息保存在文件里,且利用小括号功能,修改子shell的umask
[root@chaogelinux ssl_cert]# (umask 077;openssl genrsa -out server1024.key 1024)
Generating RSA private key, 1024 bit long modulus
..........++++++
...................++++++
e is 65537 (0x10001)
# 读取私钥文件,选择非对称加密算法rsa,天生公钥,写入到文件中
[root@chaogelinux ssl_cert]# openssl rsa -in server1024.key -pubout -out server1024.key.pub
# 查抄文件
[root@chaogelinux ssl_cert]# ls
server1024.key server1024.key.pub
# 天生自签的证书
[root@chaogelinux ssl_cert]# openssl req -new -x509 -key server1024.key -out server.crt -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) []:BJ
Locality Name (eg, city) [Default City]:BJ
Organization Name (eg, company) [Default Company Ltd]:chaoge
Organizational Unit Name (eg, section) []:it
Common Name (eg, your name or your server's hostname) []:pythonav.cn
|

Nginx摆设https
利用证书实现HTTPS访问Nginx服务,须要nginx使用ssl模块配置HTTPS支持,默认环境下ssl模块并未被安装,如果要使用该模块则须要在编译时指定–with-http_ssl_module参数,安装模块依靠于OpenSSL库和一些引用文件,这些文件并不在同一个软件包中,通常这个文件名类似libssl-dev。
nginx的https协议须要ssl模块的支持,我们在编译nginx时使用--with-http_ssl_module参数加入SSL模块。还须要服务器私钥,服务器证书,如果是公司对外环境,这个证书须要购买第三方的权势巨子证书,否则用户体验得不到保障
摆设https实践
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
| 1.创建Nginx须要的证书文件
确保机器安装了openssl和openssl-devel,创建证书
yum install openssl openssl-devel -y
2.确保nginx支持了ssl模块,检察nginx编译信息即可
nginx -V
3.模拟证书颁发机构CA创建证书
进入nginx安装目录,便于管理证书
[root@chaogelinux ~]# cd /opt/ngx112/
[root@chaogelinux ngx112]# mkdir key
[root@chaogelinux ngx112]# cd key/
# 天生私钥文件,利用字shell降低文件权限
[root@chaogelinux key]# (umask 077;openssl genrsa -out server1024.key 1024)
Generating RSA private key, 1024 bit long modulus
.++++++
...++++++
e is 65537 (0x10001)
# 自己签发证书,crt证书扩展名
[root@chaogelinux key]# openssl req -new -x509 -key server1024.key -out server.crt -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) []:BJ
Locality Name (eg, city) [Default City]:BJ
Organization Name (eg, company) [Default Company Ltd]:chaoge
Organizational Unit Name (eg, section) []:it
Common Name (eg, your name or your server's hostname) []:pythonav.cn
Email Address []:yc_uuu@163.com
# 向机构申请证书,我们这里天生的是证书请求文件,而不是直接天生证书了,运维发送该文件给机构,请求合法证书
[root@chaogelinux key]# openssl req -new -key server1024.key -out server.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) []:BJ
Locality Name (eg, city) [Default City]:BJ
Organization Name (eg, company) [Default Company Ltd]:chaoge
Organizational Unit Name (eg, section) []:it
Common Name (eg, your name or your server's hostname) []:pythonav.cn
Email Address []:yc_uuu@163.com
# 针对这个请求文件,做一个加密处理,告知办法机构,可以用这个暗码解密,了解公司信息,也可以直接回车不写暗码
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
# 一般这个证书颁发,须要比及一周内的时间,因此我们直接使用本地的自己签发的证书即可,举行训练
4.配置Nginx,加载私钥,证书
修改nginx.conf,添加
include extra/443.conf;
# 创建https配置文件
vim 443.conf 写入
[root@web01 extra]# cat 443.conf
server {
server_name _;
listen 443 ssl;
ssl_certificate /opt/nginx/key/server.crt;
ssl_certificate_key /opt/nginx/key/server1024.key;
charset utf-8;
location / {
root html;
index index.html index.htm;
}
}
~
# 修改80端口假造主机,举行请求转发给443
}
server {
listen 80;
server_name www.chaoge.com;
charset utf-8;
rewrite ^(.*)$ https://$host$1 permanent;
location / {
root html;
index index.html index.htm;
}
}
include extra/443.conf;
}
# 检测语法,重启nginx
[root@web01 extra]# nginx -t
nginx: the configuration file /opt/nginx-1.16.0//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx-1.16.0//conf/nginx.conf test is successful
|

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |