From 8f87b64d26d4ccaa9e140ed4e6a75a7f29b25351 Mon Sep 17 00:00:00 2001 From: mac Date: Sat, 7 Mar 2020 16:38:30 +0800 Subject: [PATCH] trans fini --- common_knowledge/Cryptology.md | 177 +++++++++++++++++ ...06\347\240\201\346\212\200\346\234\257.md" | 179 ------------------ 2 files changed, 177 insertions(+), 179 deletions(-) create mode 100644 common_knowledge/Cryptology.md delete mode 100644 "common_knowledge/\345\257\206\347\240\201\346\212\200\346\234\257.md" diff --git a/common_knowledge/Cryptology.md b/common_knowledge/Cryptology.md new file mode 100644 index 0000000000..88e0925e7b --- /dev/null +++ b/common_knowledge/Cryptology.md @@ -0,0 +1,177 @@ +Speaking of cipher, the first thing that comes to our mind is the password to login, but from the point of cryptography, it is the unqualified cipher. + +Why? Because the password of our account depends on crypticity, that is I keep the password in my mind and do not let you know,so you can not login in with my account. + +However,cryptography says confidential information will be revealed some day, so encryption algorithm should not keep the secret to become confidential, Conversely, even you know the encryption algorithm, you can do nothing. Magically speaking,I tell you my ciphers, but you still do not know my ciphers. + +The most metaphysical algorithm is Diffie-Hellman key exchange algorithm.I am so surprised in such a case that two people exchange some numbers in the front of you and they can share a common secret,but you can not figure out what it is.The following will focus on the algorithm. + +In the article,cryptography mainly solve the problem of encrypt and decrypt in the process of information exchange.Assume that the process of information exchange is insecurity and all the information is being eavesdropped,so sender encrypt the information before sending and receiver must know how to decrypt after receiving.The interesting thing is that +if the receiver know the way to decrypt,the eavesdropper should also know. + +In the following **we will introduce symmetric encryption algorithm、key exchange algorithm、asymmetrical encryption algorithm、digital signature、public-key certificate** to explain how we solve the problem of secure transmission. + +### First、Symmetric Encryption + +symmetric ciphers,also called shared key cipher,as the name suggests,it uses the same key to encrypt and decrypt. + +First of all,we know information consists of 0/1 bits sequence and the xor of two identical bits sequence is 0. + +For example,one of the simplest symmetric encryption is that we can generate a random bits key that is the same length as original word ,then get the xor of key and origin as encrypted words. +Conversely,we can xor this encrypted words with the key to recovery the origin words. + +This simple example has some problems because of its simplicity.For example,if the original words is large,the key is just as large and the overhead of generating large random bits sequence is also high. + +Of course,there are many sophisticated and excellent symmetric encryption algorithm that solves these problems.For example, Rijndael cryptographic algorithm,triple DES algorithm and so on. +**These algorithm is invulnerable,that is,they have huge key space,are impossible to brute force and encryption process is relatively fast**. + +**However,the weakness of these symmetric encryption algorithm is the distribution of keys**.when using the same key to encrypt and decrypt,sender must send the key to receiver.If the hacker can overhead the encrypted text and the key,these +invulnerable algorithm is broken. + +Therefore, the two most common algorithms to solve the key distribution problem are diffie-hellman key exchange algorithm and asymmetric encryption algorithm. + +### Second、Key exchange algorithm + +The secret key as we say is a big number, the algorithm use this number to encrypt and decrypt. The problem is that transition is insecure and the data can be eavesdropped.In other words, is there a way that it can let two people exchange the key in the front of others? + +Diffie-Hellman key exchange algorithm can achieve it. **Precisely speaking, this algorithm do not send the secret to the receiver, but by some sharing number, both sides generate the same secret in their mind and the secret can not be generated by the third party eavesdropper.** + +Perhaps this is the legend of the heart has a sharp touch of it. + +This algorithm is not complicated, you can try it with your friends and I will show the flow.In the beginning, we should define one thing: **Not all operations have inverses.** + +The simplest case is the well-known one-way hash function.That is given a number `a` and a hash function `f`, you can calculate `f(a)`,but if given `f(a)` and `f`,finding `a` is basically impossible.The reason why the key exchange algorithm looks so mysterious is that it takes advantage of this irreversible property. + +Below, let me show the flow of exchange key algorithm. Follow naming conventions, the parties who are going to perform the key exchange algorithm are called Alice and Bob,the bad guy who is trying to steal their communication in the Internet is called hack. + +First, Alice and Bob negotiate two number `N` and `G` as the origin.Of course the negotiation can be eavesdropped by Hack,so I put these two numbers in the middle presenting all three parties know. + +![](../pictures/密码技术/1.jpg) + +Now Alice 和 Bob **in mind** generate a number separately `A` and `B`: + +![](../pictures/密码技术/2.jpg) + +Now Alice do some calculation using `A` and `G` and get the result `AG`, then send it to Bob; Bob also do some calculation using `B` and `G` and get the result `BG`,then send it to Alice: + +![](../pictures/密码技术/3.jpg) + +Now the situation is as following: + +![](../pictures/密码技术/4.jpg) + +Note that,as the hash function case above, knowing `AG` and `G` can not figure out `A`, the same as `BG`. + +So, Alice can use `BG` and his `A` generate a number `ABG` by some calculation. Bob also can use `AG` and his `B` get the number `ABG` by some calculation. This number is the shared secret of Alice and Bob. + +As for Hack, He can eavesdrop `G`,`AG`,`BG`, but because the calculation is irreversible, he can not calculate `ABG`. + +![](../pictures/密码技术/5.jpg) + +Above is the basic flow, as for picking which number, it is exquisite and I won't write specifically for the space. + +Under the premise of the third party's eavesdropping, the algorithm can calculate a secret which can not be calculated by others as the key of symmetric encryption algorithm and start the communication of symmetric encryption. + +About this algorithm, Hack come up with a crack way, not to eavesdrop Alice and Bob's communication, but to pretend to be Alice and Bob at the same time, that is man-in-middle attack. + +![](../pictures/密码技术/6.jpg) + +In this way, both parties can't realize that they are sharing secrets with Hack. As a result, Hack can decrypt or even modify data. + +**So, key exchange algorithm can not perfectly resolve the distribution of the key, the weakness lies in not being able to verify the identity of the receiver**.So, before using exchange key algorithm, we must verify the identity. For example, using the digital signature. + +### Third、Asymmetrical encryption + +The thinking of the asymmetrical encryption is that don't sneak around with the transmission key. I separate the encryption key from the decryption key and use the public key to encrypt and the private to decrypt.Only send the public key to the receivers and they can send me encrypted data that I can use my private key to decrypt.About the eavesdropper,it is useless to get the data and the public key, because only the private key can be used to decrypt. + +You can think like this, **the private key is the key and the public key is the lock.We can public the lock and let others lock the data before send to me,but the key is in my hand to unlock.** Our common RSA algorithm is a typical asymmetric encryption algorithm, the implementation is complicated,I will skip here and you can find in google. + +In practice, the speed of asymmetrical encryption is slow compared to the symmetric encryption, so when it comes to transit huge amount of data,we do not use the public key to encrypt the data,but encrypt the symmetric encryption key and send it to receiver and then use symmetric encryption algorithm to encrypt data. + +It should be noted that, like Diffie-Hellman algorithm, **asymmetrical encryption algorithm can not verify the identity of sender and receiver,is still be cracked by man-in-middle attack.** For example, Hack block public key released by Bob, then send Alice her public key as Bob.So, without knowing it, Alice will encrypt the private data with Hack's public key, which can be decrypted and eavesdropped by Hack. + +So, the Diffie-Hellman algorithm and the RSA asymmetrical encryption algorithm can partially resolve the distribution of the key and both have the same weakness.What is the different scenario of applying them? + +Simply speaking, according to the basic principles of the two algorithms, we can see that: + +If both sides have a symmetric encryption plan and hope to encrypt the communication and not let others get the key,they can use Diffie-Hellman key exchange algorithm. +If you hope everyone can encrypt the data,but only you can decrypt, you can use RSA asymmetrical encryption algorithm and release the public key. + +In the following, we try to resolve the problem of verifying the identity of the sender. + +## Fourth、Digital signature + +About asymmetrical encryption algorithm as said,it release the public key so that others can send the encrypted date to you and it can only be decrypted by the private key kept by you. Actually, **private key can be used to encrypt the data,as RSA algorithm,the data encrypted by the private key can open be decrypted by the public key.** + +Digital signature takes the advantage of asymmetrical encryption,but reverses the process of public key encrypting. **Releasing the public key,but you use the private key to encrypt data and then public the encrypted data,this is the digital signature.** + +You may ask what is the purpose? The public key can decrypt the data,but I still encrypt the data and release.Isn't that a superfluous act? + +Yes,but ** the digital signature is not used to ensure the confidentiality of the data,but is used to verify your identity,** to prove that the data comes from you. + +You can image the data encrypted by you private key can only be decrypted by you public key,so if the encrypted data can be decrypted by you public key,can't it prove that the data comes from yourself? + +Of course,the encrypted data is just a signature that should be released with the data at the same time.The specific process is: + +1 Bob generates the public key and the secret key and releases the public key and keep the secret key himself. + +2 **making the data encrypted by the private key as signature and sends the data with this signature.** + +3 Alice receives the data the the signature and needs to check does the data come from Bob.So he use the public key released by Bob to decrypt and compare the decrypted data with the received data.If they are the same,it proves that the data is origin and comes from Bob. + +Why Alice can conclude? After all,the data and the signature,either can be exchanged.The reason is as follows: +1 If someone modify the data,Alice will know after he decrypt the data and find the difference. + +2 If someone exchange the signature,Alice will get the wrong code after decryption and it is obviously different from the original data. + +3 someone may tend to modify the data and regenerate the signature so that Alice can not find the difference;but he can not generate the signature because he do not have the private key of Bob. + +In Summary,**digital signature can verify the origin of the data to some degree**.The reason is that it can be cracked by man-in-middle attack. Once it comes to the distribution of the public key, the receiver may receive the fake public key and make the wrong verification, Which can not be avoided. + +Ridiculously, digital signature is a way verify the identity of others with the assumption that the identity of others is real.It seems like a dead cycle.**There must exist a trusted origin to verify the identity of others,Or no matter how many processes are used, they are just transferring problems, not really solving them.** + +### Fifth、Public-key certificate + +**The public-key certificate is the public key plus the signature,issued by a trusted third party certification authority**。Introducing the trusted third part is one of the feasible solution of dependency cycle. + +The process of certificate is as follows: + +1 Bob goes to the trusted certification authority to verify the identity of himself and provide his public key. + +2 Alice who wants to communicate with Bob, request the public key of Bob from the certification authority and then certification authority will give the certificate of Bob(it contains Bob's public key and the signature of his public key) to Alice. + +3 Alice check the signature adn verify that the public key comes from the certificate authority and not tampered in halfway. + +4 Alice encrypts the data through this public key and starts to communicate with Bob. + +![图片来自《图解密码技术》](../pictures/密码技术/7.jpg) + +PS: the above is for description.In real,certificate is only installed once instead of request from certificate authority every time and it is the server sends the certificate to client not the certificate authority. + +Some people may ask if Alice want to verify the validity of the certificate, he must have the public key of the authority.Isn't it the dead cycle mentioned just now? + +The regular browser we pre-installed contains trusted certificate to verity the identity of certificate authority,so the certificate is credible. + +when Bob provide the public key, he must provide many personal information to strictly verify his identity,so it can be considered trusted. + +Except for the trusted public key of Bob, the communication of Alice and Bob is protected by the secure algorithm and is invulnerable. + +Most of the regular websites nowadays apply HTTPS protocol, that adds a SSL/TLS secure layer between the HTTP protocol and the TCP protocol. After the TCP handshake, SSL protocol layer also handshake to exchange secure information including the certificate of the website, so that the browser can verify the website. After SSL layer finish the verification, the data in the HTTP protocol is encrypted to guarantee secure transmission. + +### Sixth、Summary + +Symmetric encryption algorithm use the same secret key to encrypt and decrypt, is hard to crack, encrypt quickly, but has the problem of secret key transmission. + +Diffie-Hellman key exchange algorithm can it like a Vulcan mind meld,partially solve the problem of the distribution of the secret key,but it can not verify the identity of senders and receivers and can be cracked by man-in-middle attack. + +asymmetrical encryption algorithm generates a pair of secrete keys, separate encryption and decryption. + +RSA algorithm is a classical asymmetrical encryption algorithm and it has two purpose:One is to encrypt,that is releasing a public key to encrypt and hold a secret key itself to decrypt,for the confidentiality of the data.Another one is for digital signature,that is +releasing the public key and generate the digital signature by private key, to prove that the data comes from the secret key holder.But either way can not avoid man-in-middle attack because of the distribution of the public key. + +The public-key certificate is the public key plus the signature,issued by a trusted third party certification authority.Because regular browsers pre-install the public key of the trusted certification authority, it can avoid man-in-middle attack. + +The SSL/TLS secure layer in HTTPS protocol includes these encryption methods above.**So do not install irregular browser and certificate of unknown source**. + +Cryptography is a little part of the security.Even though the HTTPS websites certified by a formal authority are not totally trusted,it only indicates the transition of the data is safe.Technology can not protect you.The most important thing is to improve personal safety awareness,pay more attention and handle sensitive data carefully. + diff --git "a/common_knowledge/\345\257\206\347\240\201\346\212\200\346\234\257.md" "b/common_knowledge/\345\257\206\347\240\201\346\212\200\346\234\257.md" deleted file mode 100644 index a8ade205d3..0000000000 --- "a/common_knowledge/\345\257\206\347\240\201\346\212\200\346\234\257.md" +++ /dev/null @@ -1,179 +0,0 @@ -说到密码,我们第一个想到的就是登陆账户的密码,但是从密码学的角度来看,这种根本就不算合格的密码。 - -为什么呢,因为我们的账户密码,是依靠隐蔽性来达到加密作用:密码藏在我心里,你不知道,所以你登不上我的账户。 - -然而密码技术认为,「保密」信息总有一天会被扒出来,所以加密算法不应该依靠「保密」来保证机密性,而应该做到:即便知道了加密算法,依然无计可施。说的魔幻一点就是,告诉你我的密码,你依然不知道我的密码。 - -最玄学的就是 Diffie-Hellman 密钥交换算法,我当初就觉得很惊奇,两个人当着你的面互相报几个数字,他们就可以拥有一个共同的秘密,而你却根本不可能算出来这个秘密。下文会着重介绍一下这个算法。 - -本文讨论的密码技术要解决的主要是信息传输中的加密和解密问题。要假设数据传输过程是不安全的,所有信息都在被窃听的,所以发送端要把信息加密,接收方收到信息之后,肯定得知道如何解密。有意思的是,如果你能够让接收者知道如何解密,那么窃听者不是也能够知道如何解密了吗? - -下面,**我们会介绍对称加密算法、密钥交换算法、非对称加密算法、数字签名、公钥证书**,看看解决安全传输问题的一路坎坷波折。 - -### 一、对称性加密 - -对称性密码,也叫共享密钥密码,顾名思义,这种加密方式用相同的密钥进行加密和解密。 - -比如我说一种最简单的对称加密的方法。首先我们知道信息都可以表示成 0/1 比特序列,也知道相同的两个比特序列做异或运算的结果为 0。 - -那么我们就可以生成一个长度和原始信息一样的随机比特序列作为密钥,然后用它对原始信息做异或运算,就生成了密文。反之,再用该密钥对密文做一次异或运算,就可以恢复原始信息。 - -这是一个简单例子,不过有些过于简单,有很多问题。比如密钥的长度和原始信息完全一致,如果原始信息很大,密钥也会一样大,而且生成大量真随机比特序列的计算开销也比较大。 - -当然,有很多更复杂优秀的对称加密算法解决了这些问题,比如 Rijndael 算法、三重 DES 算法等等。**它们从算法上是无懈可击的,也就是拥有巨大的密钥空间,基本无法暴力破解,而且加密过程相对快速**。 - -**但是,一切对称加密算法的软肋在于密钥的配送**。加密和解密用同一个密钥,发送方必须设法把密钥发送给接收方。如果窃听者有能力窃取密文,肯定也可以窃取密钥,那么再无懈可击的算法依然不攻自破。 - -所以,下面介绍两种解决密钥配送问题最常见的算法,分别是 Diffie-Hellman 密钥交换算法和非对称加密算法。 - -### 二、密钥交换算法 - -我们所说的密钥一般就是一个很大的数字,算法用这个数加密、解密。问题在于,信道是不安全的,所有发出的数据都会被窃取。换句话说,有没有一种办法,能够让两个人在众目睽睽之下,光明正大地交换一个秘密,把对称性密钥安全地送到接收方的手中? - -Diffie-Hellman 密钥交换算法可以做到。**准确的说,该算法并不是把一个秘密安全地「送给」对方,而是通过一些共享的数字,双方「心中」各自「生成」了一个相同的秘密,而且双方的这个秘密,是第三方窃听者无法生成的**。 - -也许这就是传说中的心有灵犀一点通吧。 - -这个算法规则不算复杂,你甚至都可以找个朋友尝试一下共享秘密,等会我会简单画出它的基本流程。在此之前,需要明确一个问题:**并不是所有运算都有逆运算**。 - -最简单的例子就是我们熟知的单向散列函数,给一个数字 `a` 和一个散列函数 `f`,你可以很快计算出 `f(a)`,但是如果给你 `f(a)` 和 `f`,推出 `a` 是一件基本做不到的事。密钥交换算法之所以看起来如此玄幻,就是利用了这种不可逆的性质。 - -下面,看下密钥交换算法的流程是什么,按照命名惯例,准备执行密钥交换算法的双方称为 Alice 和 Bob,在网络中企图窃取他俩通信内容的坏人称为 Hack 吧。 - -首先,Alice 和 Bob 协商出两个数字 `N` 和 `G` 作为生成元,当然协商过程可以被窃听者 Hack 窃取,所以我把这两个数画到中间,代表三方都知道: - -![](../pictures/密码技术/1.jpg) - -现在 Alice 和 Bob **心中**各自想一个数字出来,分别称为 `A` 和 `B` 吧: - -![](../pictures/密码技术/2.jpg) - -现在 Alice 将自己心里的这个数字 `A` 和 `G` 通过某些运算得出一个数 `AG`,然后发给 Bob;Bob 将自己心里的数 `B` 和 `G` 通过相同的运算得出一个数 `BG`,然后发给 Alice: - -![](../pictures/密码技术/3.jpg) - -现在的情况变成这样了: - -![](../pictures/密码技术/4.jpg) - -注意,类似刚才举的散列函数的例子,知道 `AG` 和 `G`,并不能反推出 `A` 是多少,`BG` 同理。 - -那么,Alice 可以通过 `BG` 和自己的 `A` 通过某些运算得到一个数 `ABG`,Bob 也可以通过 `AG` 和自己的 `B` 通过某些运算得到 `ABG`,这个数就是 Alice 和 Bob 共有的秘密。 - -而对于 Hack,可以窃取传输过程中的 `G`,`AG`,`BG`,但是由于计算不可逆,怎么都无法结合出 `ABG` 这个数字。 - -![](../pictures/密码技术/5.jpg) - -以上就是基本流程,至于具体的数字取值是有讲究的,运算方法在百度上很容易找到,限于篇幅我就不具体写了。 - -该算法可以在第三者窃听的前提下,算出一个别人无法算出的秘密作为对称性加密算法的密钥,开始对称加密的通信。 - -对于该算法,Hack 又想到一种破解方法,不是窃听 Alice 和 Bob 的通信数据,而是直接同时冒充 Alice 和 Bob 的身份,也就是我们说的「**中间人攻击**」: - -![](../pictures/密码技术/6.jpg) - -这样,双方根本无法察觉在和 Hack 共享秘密,后果就是 Hack 可以解密甚至修改数据。 - -**可见,密钥交换算法也不算完全解决了密钥配送问题,缺陷在于无法核实对方身份**。所以密钥交换算法之前一般要核实对方身份,比如使用数字签名。 - -### 三、非对称加密 - -非对称加密的思路就是,干脆别偷偷摸摸传输密钥了,我把加密密钥和解密密钥分开,公钥用于加密,私钥用于解密。只把公钥传送给对方,然后对方开始给我发送加密的数据,我用私钥就可以解密。至于窃听者,拿到公钥和加密数据也没用,因为只有我手上的私钥才能解密。 - -可以这样想,**私钥是钥匙,而公钥是锁,可以把锁公开出去,让别人把数据锁起来发给我;而钥匙一定要留在自己手里,用于解锁**。我们常见的 RSA 算法就是典型的非对称加密算法,具体实现比较复杂,我就不写了,网上很多资料。 - -在实际应用中,非对称性加密的运算速度要比对称性加密慢很多的,所以传输大量数据时,一般不会用公钥直接加密数据,而是加密对称性加密的密钥,传输给对方,然后双方使用对称性加密算法传输数据。 - -需要注意的是,类似 Diffie-Hellman 算法,**非对称加密算法也无法确定通信双方的身份,依然会遭到中间人攻击**。比如 Hack 拦截 Bob 发出的公钥,然后冒充 Bob 的身份给 Alice 发送自己的公钥,那么不知情的 Alice 就会把私密数据用 Hack 的公钥加密,Hack 可以通过私钥解密窃取。 - -那么,Diffie-Hellman 算法和 RSA 非对称加密算法都可以一定程度上解决密钥配送的问题,也具有相同的缺陷,二者的应用场景有什么区别呢? - -简单来说,根据两种算法的基本原理就可以看出来: - -如果双方有一个对称加密方案,希望加密通信,而且不能让别人得到钥匙,那么可以使用 Diffie-Hellman 算法交换密钥。 - -如果你希望任何人都可以对信息加密,而只有你能够解密,那么就使用 RSA 非对称加密算法,公布公钥。 - -下面,我们尝试着解决认证发送方身份的问题。 - -### 四、数字签名 - -刚才说非对称加密,把公钥公开用于他人对数据加密然后发给你,只有用你手上对应的私钥才能将密文解密。其实,**私钥也可用用来加密数据的,对于 RSA 算法,私钥加密的数据只有公钥才能解开**。 - -数字签名也是利用了非对称性密钥的特性,但是和公钥加密完全颠倒过来:**仍然公布公钥,但是用你的私钥加密数据,然后把加密的数据公布出去,这就是数字签名**。 - -你可能问,这有什么用,公钥可以解开私钥加密,我还加密发出去,不是多此一举吗? - -是的,但是**数字签名的作用本来就不是保证数据的机密性,而是证明你的身份**,证明这些数据确实是由你本人发出的。 - -你想想,你的私钥加密的数据,只有你的公钥才能解开,那么如果一份加密数据能够被你的公钥解开,不就说明这份数据是你(私钥持有者)本人发布的吗? - -当然,加密数据仅仅是一个签名,签名应该和数据一同发出,具体流程应该是: - -1、Bob 生成公钥和私钥,然后把公钥公布出去,私钥自己保留。 - -2、**用私钥加密数据作为签名,然后将数据附带着签名一同发布出去**。 - -3、Alice 收到数据和签名,需要检查此份数据是否是 Bob 所发出,于是用 Bob 之前发出的公钥尝试解密签名,将收到的数据和签名解密后的结果作对比,如果完全相同,说明数据没被篡改,且确实由 Bob 发出。 - -为什么 Alice 这么肯定呢,毕竟数据和签名是两部分,都可以被掉包呀?原因如下: - -1、如果有人修改了数据,那么 Alice 解密签名之后,对比发现二者不一致,察觉出异常。 - -2、如果有人替换了签名,那么 Alice 用 Bob 的公钥只能解出一串乱码,显然和数据不一致。 - -3、也许有人企图修改数据,然后将修改之后的数据制成签名,使得 Alice 的对比无法发现不一致;但是一旦解开签名,就不可能再重新生成 Bob 的签名了,因为没有 Bob 的私钥。 - -综上,**数字签名可以一定程度上认证数据的来源**。之所以说是一定程度上,是因为这种方式依然可能受到中间人攻击。一旦涉及公钥的发布,接收方就可能收到中间人的假公钥,进行错误的认证,这个问题始终避免不了。 - -说来可笑,数字签名就是验证对方身份的一种方式,但是前提是对方的身份必须是真的... 这似乎陷入一个先有鸡还是先有蛋的死循环,**要想确定对方的身份,必须有一个信任的源头,否则的话,再多的流程也只是在转移问题,而不是真正解决问题**。 - -### 五、公钥证书 - -**证书其实就是公钥 + 签名,由第三方认证机构颁发**。引入可信任的第三方,是终结信任循环的一种可行方案。 - -证书认证的流程大致如下: - -1、Bob 去可信任的认证机构证实本人真实身份,并提供自己的公钥。 - -2、Alice 想跟 Bob 通信,首先向认证机构请求 Bob 的公钥,认证机构会把一张证书(Bob 的公钥以及自己对其公钥的签名)发送给 Alice。 - -3、Alice 检查签名,确定该公钥确实由这家认证机构发送,中途未被篡改。 - -4、Alice 通过这个公钥加密数据,开始和 Bob 通信。 - -![图片来自《图解密码技术》](../pictures/密码技术/7.jpg) - -PS:以上只是为了说明,证书只需要安装一次,并不需要每次都向认证机构请求;一般是服务器直接给客户端发送证书,而不是认证机构。 - -也许有人问,Alice 要想通过数字签名确定证书的有效性,前提是要有该机构的(认证)公钥,这不是又回到刚才的死循环了吗? - -我们安装的正规浏览器中都预存了正规认证机构的证书(包含其公钥),用于确认机构身份,所以说证书的认证是可信的。 - -Bob 向机构提供公钥的过程中,需要提供很多个人信息进行身份验证,比较严格,所以说也算是可靠的。 - -获得了 Bob 的可信公钥,Alice 和 Bob 之间的通信基于加密算法的保护,是完全无懈可击的。 - -现在的正规网站,大都使用 HTTPS 协议,就是在 HTTP 协议和 TCP 协议之间加了一个 SSL/TLS 安全层。在你的浏览器和网站服务器完成 TCP 握手后,SSL 协议层也会进行 SSL 握手交换安全参数,其中就包含该网站的证书,以便浏览器验证站点身份。SSL 安全层验证完成之后,上层的 HTTP 协议内容都会被加密,保证数据的安全传输。 - -这样一来,传统的中间人攻击就几乎没有了生存空间,攻击手段只能由技术缺陷转变为坑蒙拐骗。事实上,这种手段的效果反而更高效,比如我就发现**网上不少下载网站发布的浏览器,不仅包含乱七八糟的导航和收藏网址,还包含一些不正规的认证机构证书。任何人都可以申请证书,这些不正规证书很可能造成安全隐患**。 - -### 六、最后总结 - -对称性加密算法使用同一个密钥加密和解密,难以破解,加密速度较快,但是存在密钥配送问题。 - -Diffie-Hellman 密钥交换算法可以让双方「心有灵犀一点通」,一定程度解决密钥配送问题,但是无法验证通信方的身份,所以可能受到中间人攻击。 - -非对称性加密算法生成一对儿密钥,把加密和解密的工作分开了。 - -RSA 算法作为经典的非对称加密算法,有两种用途:如果用于加密,可以把公钥发布出去用于加密,只有自己的私钥可以解密,保证了数据的机密性;如果用于数字签名,把公钥发布出去后,用私钥加密数据作为签名,以证明该数据由私钥持有者所发送。但是无论那种用法,涉及公钥的发布,都无法避免中间人攻击。 - -公钥证书就是公钥 + 签名,由可信任的第三方认证机构颁发。由于正规浏览器都预装了可信的认证机构的公钥,所以可以有效防止中间人攻击。 - -HTTPS 协议中的 SSL/TLS 安全层会组合使用以上几种加密方式,**所以说不要安装非正规的浏览器,不要乱安装未知来源的证书**。 - -密码技术只是安全的一小部分,即便是通过正规机构认证的 HTTPS 站点,也不意味着可信任,只能说明其数据传输是安全的。技术永远不可能真正保护你,最重要的还是得提高个人的安全防范意识,多留心眼儿,谨慎处理敏感数据。 - -坚持原创高质量文章,致力于把算法问题讲清楚,欢迎关注我的公众号 labuladong 获取最新文章: - -![labuladong](../pictures/labuladong.jpg)