Computer network extension application (a) HTTPS

This article, we will start HTTP transmitted in the clear, step by step to explain how to achieve HTTPS encrypted communications on the basis of HTTP

1, cleartext (HTTP)

In the beginning, HTTP was originally designed only to solve the problem of communication, data is transmitted in the clear

In this case, some malicious people very easily without the knowledge of the parties in the case, steal data

To ensure that the data is not stolen, the only way is to transfer data encryption

So that even if someone steals the data, we do not know the content delivery, as just a bunch of meaningless gibberish looks he received

2, symmetric encryption

(1) symmetric encryption

The two sides agreed a communication key, which only the communicating parties know, use this key to encrypt data, only using this key can decrypt

(2) symmetric encryption problem

Symmetric encryption there is a problem, the problem occurs at the beginning of the communication process, and that is how the two sides agreed to communicate the key

If the key is transmitted over the network, it is likely the key at the outset stolen by others

3, asymmetric encryption

(1) Asymmetric Encryption

To communicate with the host first generates a key pair (a public key and a private key), which we all know is the public key, the private key is only known to himself

Data encrypted with the public key, only the private key corresponding to decrypt data encrypted with the private key, only with the corresponding public key can decrypt

(2) asymmetric encryption problem

Asymmetric encryption is also a problem, the problem and the problem is somewhat similar symmetric encryption, public key is how to safely handed the other hand

If the network is transmitted through, it may be replaced with a public key (middle attack)

Asymmetric encryption solves the problem of password hacking, because the public key is we all know, there is no question of whom to steal

The question now is how to prove "you are you" (Alice and myself know how Bob is communication rather than Hacker)

4, the digital signature

Let us put aside the above issues aside, let's talk about the digital signature technology

The advent of digital signatures is to solve two problems, one is to verify whether the content has been tampered with, and second, to determine who generated content

It specifically how to do it? Do not worry, we first look at the two procedures using digital signature, signature generation and signature verification

  • Generating a signature

    The sender hash algorithm summary data generation section, with its own private key and then encrypts the digest, a digital signature is formed

    The digital signature is appended to the data sent along to the receiver

  • Verify signatures

    接收方收到后,重新使用哈希算法对数据生成一段摘要 1,同时使用发送方的公钥解密数字签名得到摘要 2

    对比摘要 1 和摘要 2 是否相同,如果相同就说明内容没有被修改

怎么验证内容没有被修改,看完上面的两个过程相信大家也已经知道,那么确定内容是谁生成的又是怎么做到的呢

我们注意到一个细节,对摘要进行加密使用的是发送方的私钥,而私钥是自己才有的,这就是唯一性的证明

由于私钥只有自己知道,也就是说只有自己才能签名,而公钥是大家都知道的,所以大家都能验证签名

实际上,数字签名是常规的非对称加密的逆应用,在数字签名中,使用私钥加密,使用公钥解密

5、数字证书

好,我们重新回到之前的问题,怎么证明 “你就是你”,问题的本质其实是公钥怎么安全分发

这时候数字证书就可以派上用场,数字证书是由权威机构颁发给服务器的身份凭证,其实就相当于我们的身份证

服务器拿着身份证就可以证明 “我就是我,是独一无二的烟火”,并且通过数字证书我们还能安全的分发公钥

下面我们先来看看一个服务器是怎么申请证书的,以及客户端是怎么根据证书得到可信的公钥的

  • 服务器申请证书

    服务器向权威机构(Certificate Authority,简称 CA)申请一个证书

    权威机构把服务器使用的公钥、服务器的信息、权威机构的信息等作为原始数据

    权威机构用哈希算法对原始数据计算一个哈希值,然后用自己的私钥加密原始数据,得到数字签名

    权威机构把数字签名附加在原始数据后,得到数字证书

    权威机构返回数字证书给服务器

  • 客户端验证证书

    客户端收到服务器的数字证书

    客户端得到该数字证书的颁发机构的公钥,并用公钥解密数字签名,得到原始数据的哈希值

    客户端用哈希算法对数字证书的原始数据计算一个哈希值

    如果两个哈希值相同,说明数字证书没有被修改,这样就可以得到服务器的公钥

针对上面的两个过程,我们来回答几个问题

1、为什么数字证书不能伪造?

因为数字证书包含权威机构的数字签名,数字签名的特点就是只有自己才能签名,但是大家都能验证签名

2、为什么权威机构是可信的?

这个问题真要从密码学的角度的确难讲,但是这个道理就相当于在现实生活中我们为什么要相信银行一样

3、在验证证书时,需要使用到权威机构的公钥,这个公钥是怎么传输的?

又回到这个问题,公钥怎么安全分发,按照以前的思路,就是让另一个权威机构给这个权威机构颁发证书

那这就是一个无限递归,怎么搞呀?要想结束递归,那就要给递归设置初始条件

权威机构是一个树状分层的结构,高层的权威机构会给低层的权威机构颁发证书

某些顶层权威机构的证书会内置在操作系统或浏览器中,默认使用的人自动相信他们

6、对称加密 + 非对称加密 + 数字签名 + 数字证书(HTTPS)

兜兜转转,一路过来,终于到最后一步了

把我们之前讲的对称加密、非对称加密、数字签名、数字证书综合应用起来,就是 HTTPS 所使用的加密技术

最后,我们把这些东西串联起来,讲一下 HTTPS 通信的全过程

  1. 客户端请求建立加密连接

  2. 服务器给客户端发送证书

  3. 客户端查看证书的有效日期,如果证书已经过期,那么提示失效,如果证书没有过期,继续下面步骤

  4. 客户端查看证书的颁发机构

    如果客户端知道这个颁发结构,那么就会用对应的公钥验证证书,得到服务端的公钥

    若客户端不知道这个颁发机构,那么就会提示这个证书存在风险,让用户选择是否要相信这个证书

  5. 如果验证通过,也就意味着客户端已经得到服务端的公钥

    那么客户端随机生成一个对称密钥,并用服务端的公钥加密,发送给服务端

  6. 服务端收到后,用自己的私钥解密,得到对称密钥

  7. 此时,双方都已知道对称密钥 ,之后用它进行加密通信

【 阅读更多计算机网络系列文章,请看 计算机网络复习

Guess you like

Origin www.cnblogs.com/wsmrzx/p/12386791.html