加载中


Cryptography can be a hard subject to understand. It’s full of mathematical proofs. But unless you are actually developing cryptographic systems, much of that complexity is not necessary to understand what is going on at a high level.

If you opened this article hoping to create the next HTTPS protocol, I’m sorry to say that pigeons won’t be enough. Otherwise, brew some coffee and enjoy the article.

密码学是一门难以理解的学科,因为它充满了数学定理。但是除非你要实际开发出一套加密算法系统,否则你是没必要强制理解那些深奥的数学定理的。

如果你阅读本文的目的是想设计下一套 HTTPS 协议,那我只能抱歉的说本文的知识还远远不够;如果不是的话,那么就煮杯咖啡,轻松愉悦的阅读本文吧。

Alice, Bob and … pigeons?

Any activity you do on the Internet (reading this article, buying stuff on Amazon, uploading cat pictures) comes down to sending and receiving messages to and from a server.

This can be a bit abstract so let’s imagine that those messages were delivered by carrier pigeons. I know that this may seem very arbitrary, but trust me HTTPS works the same way, albeit a lot faster.

Also instead of talking about servers, clients and hackers, we will talk about Alice, Bob and Mallory. If this isn’t your first time trying to understand cryptographic concepts you will recognize those names, because they are widely used in technical literature.

爱丽丝、鲍伯和 ... 信鸽?

你在互联网上从事的任何活动(阅读这篇文章、在亚马逊上购物、上传图片等)归结到底都是从某台服务器上发送和接收信息。

这个说起来可能有点抽象,不如让我们假设这些消息都是由信鸽来传递的。我知道这个假设有些太过随意,但相信我 HTTPS 就是这样工作的,尽管它的速度快的多。

我们先不谈服务器、客户端或者黑客攻击,先来聊一下爱丽丝、鲍伯和马洛里。如果这已不是你第一次接触密码学理论,你应该会认识这些名字,因为他们经常在各种密码学文献中被提及。

A first naive communication

If Alice wants to send a message to Bob, she attaches the message on the carrier pigeon’s leg and sends it to Bob. Bob receives the message, reads it and it’s all is good.

But what if Mallory intercepted Alice’s pigeon in flight and changed the message? Bob would have no way of knowing that the message that was sent by Alice was modified in transit.

This is how HTTP works. Pretty scary right? I wouldn’t send my bank credentials over HTTP and neither should you.

一个简单的通信方式

如果爱丽丝想给鲍伯发个消息,她会把消息绑在信鸽的腿上寄给鲍伯。然后鲍伯收到了消息,并阅读了它。这一切都是美好的。

但如果马洛里拦截了爱丽丝飞翔中的信鸽并且修改消息内容呢?鲍伯将无法知道爱丽丝发来的消息已经在传输过程中被修改了。

这就是 HTTP 的工作方式,很可怕吧?我绝不会通过 HTTP 发送我的银行凭证,希望你也不会。

A secret code

Now what if Alice and Bob are very crafty. They agree that they will write their messages using a secret code. They will shift each letter by 3 positions in the alphabet. For example D → A, E → B, F → C. The plain text message “secret message” would be “pbzobq jbppxdb”.

Now if Mallory intercepts the pigeon she won’t be able to change the message into something meaningful nor understand what it says, because she doesn’t know the code. But Bob can simply apply the code in reverse and decrypt the message where A → D, B → E, C → F. The cipher text “pbzobq jbppxdb” would be decrypted back to “secret message”.

Success!

This is called symmetric key cryptography, because if you know how to encrypt a message you also know how to decrypt it.

The code I described above is commonly known as the Caesar cipher. In real life, we use fancier and more complex codes, but the main idea is the same.

隐蔽的密码

那么如果爱丽丝和鲍勃都非常的机智。他们一致认同使用一种隐蔽的密码来书写他们的信息。他们会将信息中的每个字母按照字母表中的顺序前移三位。比如,D→A,E→B,F→C。如此一来,原文为 “secret message” 的信息就变成了 “pbzobq jbppxdb” 。

那现在如果马洛里再截获了信鸽,她既不能做出有意义的修改同时也不会知道信息的内容,因为她不知道隐蔽的密码到底是什么。然而鲍勃却可以很容易反转密码,依靠 A → D, B → E, C → F 之类的规则破译信息的内容。加密后的信息 “pbzobq jbppxdb” 会被破解还原为 “secret message” 。

搞定!

这就是对称密匙加密,因为如果你知道如何加密一段信息那么你同样可以解密这段信息。

上述的密码通常被称为凯撒码。在现实生活中,我们会使用更为奇特和复杂的密码,但原理相同。

How do we decide the key?

Symmetric key cryptography is very secure if no one apart from the sender and receiver know what key was used. In the Caesar cipher, the key is an offset of how many letters we shift each letter by. In our example we used an offset of 3, but could have also used 4 or 12.

The issue is that if Alice and Bob don’t meet before starting to send messages with the pigeon, they would have no way to establish a key securely. If they send the key in the message itself, Mallory would intercept the message and discover the key. This would allow Mallory to then read or change the message as she wishes before and after Alice and Bob start to encrypt their messages.

This is the typical example of a Man in the Middle Attack and the only way to avoid it is to change the encryption system all together.

我们如何决定密匙?

如果除了发信者和收信者之外没有人知道使用的是什么密匙,对称密匙加密是非常安全的。在凯撒加密中,密匙就是每个字母变到加密字母需要移动多少位的偏移量。我之前的距离中,使用的偏移量是 3 ,但是也可以用 4 或者 12 。

问题是如果爱丽丝和鲍勃在开始用信鸽传信之前没有碰过头,他们没有一个安全的方式来确立密匙。如果他们自己来在信中传递密匙,马洛里就会截获信息并发现密匙。这就使得马洛里可以在爱丽丝和鲍勃开始加密他们的信息之前或之后,阅读到他们信息的内容并按照她的意愿来篡改信息。

这是一个中间人攻击的典型例子,避免这个问题的唯一方法就是收发信的两方一起修改他们的编码系统。

Pigeons carrying boxes

So Alice and Bob come up with an even better system. When Bob wants to send Alice a message she will follow the procedure below:

  • Bob sends a pigeon to Alice without any message.

  • Alice sends the pigeon back carrying a box with an open lock, but keeping the key.

  • Bob puts the message in the box, closes the locks and sends the box to Alice.

  • Alice receives the box, opens it with the key and reads the message.

This way Mallory can’t change the message by intercepting the pigeon, because she doesn’t have the key. The same process is followed when Alice wants to send Bob a message.

Alice and Bob just used what is commonly known as asymmetric key cryptography. It’s called asymmetric, because even if you can encrypt a message (lock the box) you can’t decrypt it (open a closed box).
In technical speech the box is known as the public key and the key to open it is known as the private key.

通过信鸽传递盒子

所以爱丽丝和鲍勃就想出了一个更好的系统。当鲍勃想要给爱丽丝发送信息时,他会按照如下的步骤来进行:

  • 鲍勃向爱丽丝送一只没有携带任何信息的鸽子。

  • 爱丽丝给鲍勃送回鸽子,并且这只鸽子带有一个有开着的锁的盒子,爱丽丝保管着锁的钥匙。

  • 鲍勃把信放进盒子中,把锁锁上然后把盒子送给爱丽丝。

  • 爱丽丝收到盒子,用钥匙打开然后阅读信息。

这样马洛里就不能通过截获鸽子来篡改信息了,因为她没有打开盒子的钥匙。当爱丽丝要给鲍勃发送消息的时候同样按照上述的流程。

爱丽丝和鲍勃所使用的流程通常被称为非对称密钥加密。之所以称之为非对称,是因为即使是你把信息编码(锁上盒子)也不能破译信息(打开锁住的盒子)。

在术语中,盒子被称为公匙而用来打开盒子的钥匙被称为私匙

How do I trust the box?

If you paid attention you may have noticed that we still have a problem. When Bob receives that open box how can he be sure that it came from Alice and that Mallory didn’t intercept the pigeon and changed the box with one she has the key to?

Alice decides that she will sign the box, this way when Bob receives the box he checks the signature and knows that it was Alice who sent the box.

Some of you may be thinking, how would Bob identify Alice’s signature in the first place? Good question. Alice and Bob had this problem too, so they decided that, instead of Alice signing the box, Ted will sign the box.

Who is Ted? Ted is a very famous, well known and trustworthy guy. Ted gave his signature to everyone and everybody trusts that he will only sign boxes for legitimate people.

Ted will only sign an Alice box if he’s sure that the one asking for the signature is Alice. So Mallory cannot get an Alice box signed by Ted on behalf of her as Bob will know that the box is a fraud because Ted only signs boxes for people after verifying their identity.

Ted in technical terms is commonly referred to as a Certification Authorityand the browser you are reading this article with comes packaged with the signatures of various Certification Authorities.

So when you connect to a website for the first time you trust its box because you trust Ted and Ted tells you that the box is legitimate.

如何信任盒子

如果你稍加注意你就会发现还是存在问题。当鲍勃收到盒子时他如何能确定这个盒子来自爱丽丝而不是马洛里截获了鸽子然后换了一个她有钥匙能打开的盒子呢?

爱丽丝决定签名标记一下盒子,这样鲍勃收到盒子的时候就可以检查签名来确定是爱丽丝送出的盒子了。

那么你们之中的一些人可能就会想了,鲍勃如何打一开始就能识别出爱丽丝的签名呢?这是个好问题。爱丽丝和鲍勃也确实有这个问题,所以他们决定让泰德代替爱丽丝来标记这个盒子。

那么谁是泰德呢?泰德很有名的,是一个值得信任的家伙。他会给任何人签名并且所有人都信任他只会给合法的人签名标记盒子。

如果泰德可以确认索要签名的人是爱丽丝,他就会在爱丽丝的盒子上签名。因此马洛里就不可能搞到一个有着泰德代表爱丽丝签了名的盒子,因为鲍勃知道泰德只会给他确认过的人签名,从而识破马洛里的诡计。

泰德的角色在术语中被称为认证机构。而你阅读此文时所用的浏览器打包存有许多认证机构的签名。

所以当你首次接入一个网站的时候你可以信任来自这个站点的盒子因为你信任泰德而泰德会告诉你盒子是合法的。

Boxes are heavy

Alice and Bob now have a reliable system to communicate, but they realize that pigeons carrying boxes are slower than the ones carrying only the message.

They decide that they will use the box method (asymmetric cryptography) only to choose a key to encrypt the message using symmetric cryptography with (remember the Caesar cipher?).

This way they get the best of both worlds. The reliability of asymmetric cryptography and the efficiency of symmetric cryptography.

In the real world there aren’t slow pigeons, but nonetheless encrypting messages using asymmetric cryptography is slower than using symmetric cryptography, so we only use it to exchange the encryption keys.

Now you know how HTTPS works and your coffee should also be ready. Go drink it you deserved it

沉重的盒子

现在爱丽丝和鲍勃有了一个可靠的系统来进行交流,然他们也意识到让鸽子携带盒子比原本只携带信件要慢一些。

因此他们决定只有在选择用对称加密来给信息编码(还记得凯撒加密法吧?)的密匙时,使用传递盒子的方法(非对称加密)。

这样就可以二者的优点兼具了,非对称加密的可靠性和对称加密的高效性。

现实世界中我们不会用信鸽这样慢的送信手段,但用非对称加密来编码信息仍要慢于使用对称加密技术,所以我们只有在交换编码密匙的时候会使用非对称加密技术。

现在你已经了解了HTTPS是如何工作的了,你的咖啡也应该准备好了。好好享用吧你受之无愧。

返回顶部
顶部