rsa加密
问题 (The Problem)
Alice wants to send a secret message to her friend Bob. The message is the single letter “C.”
爱丽丝想发送秘密消息给她的朋友鲍勃。 该消息是单个字母“ C”。
How can she confidentially deliver this message, assuming that her sworn enemies, Mallory and Eve, might peek at its contents while it is in transit?
假设她发誓的敌人马洛里和夏娃在传送过程中可能会窥视其中的内容,她又如何秘密地传达此消息?
例 (Example)
The “Lock” 🔒 (AKA the public key): (5, 14)
“ Lock”🔒(又称公钥):(5,14 (5, 14)
- This pair of numbers is public, and is used by Alice (the sender) to encrypt messages. 这对数字是公共的,由Alice(发送者)用来加密消息。
The “Key” 🔑 (AKA the private key): (17, 14)
“密钥”🔑(又称私钥):(17,14 (17, 14)
- The first number in this pair of numbers is private, i.e. only known by Bob (the receiver). This pair is used to decrypt messages. 这对数字中的第一个数字是私有的,即只有鲍勃(接收方)知道。 该对用于解密消息。
Alice encrypts the message “C” using
(5, 14)
.爱丽丝使用
(5, 14)
5,14)加密消息“ C”。a. First, “C” is converted into an integer. In ASCII, “C” maps to the integer 67. But for simplicity, let’s say it maps to 3 instead.
一个。 首先,将“ C”转换为整数。 在ASCII中,“ C”映射到整数67。但是为了简单起见,假设它映射到3。
b. Then, 3 is encrypted.
b。 然后,将3加密。
3⁵ mod 14 = 5
.3⁵ mod 14 = 5
。- Alice sends the result, 5, to Bob. If Mallory or Eve peek and see this message, it tells them nothing. 爱丽丝将结果5发送给鲍勃。 如果Mallory或Eve偷看了此消息,则不会告诉他们任何内容。
Bob decrypts the result using (17, 14).
鲍勃使用(17,14)解密结果。
a.
一个。
5¹⁷ mod 14 = 3
.5¹⁷ mod 14 = 3
。b. The result is 3. Bob maps 3 back to a character, which is “C”, to get the original message!
b。 结果是3。Bob将3映射回一个字符“ C”,以获得原始消息!
锁和钥匙生成 (Lock & Key Generation)
Now you may be wondering — how do those two pairs of numbers get generated? Let’s answer that question.
现在您可能想知道-这两个数字如何生成? 让我们回答这个问题。
Note that, in our example, Bob generates these two pairs of numbers. He gives everyone access to the first pair of numbers (5, 14)
, but keeps the second pair secret (specifically the first part 17, the second part 14 is in the first pair and thus is public) so only he can decrypt messages. Now, here’s the process.
请注意,在我们的示例中,Bob生成这两对数字。 他使每个人都可以访问第一对数字(5, 14)
,但是将第二对数字保密(特别是第一部分17,第二部分14在第一对数字中,因此是公开的),因此只有他才能解密消息。 现在,这是过程。
1. Choose two prime numbers, p
and q
. In real life scenarios, they should be large (for security), but we’ll choose small numbers to make things easier.p = 2
q = 7
1.选择两个质数p
和q
。 在现实生活中,它们应该很大(出于安全考虑),但是我们将选择较小的数字来简化操作。 p = 2
q = 7
2. Compute n = p * q
. This is the modulus we used in the previous example.n = 2 * 7 = 14
2.计算n = p * q
。 这是我们在前面的示例中使用的模数。 n = 2 * 7 = 14
3. Compute Φ(n)
, which is known as Euler’s totient. This counts the number of positive integers less than n
which are coprime to n
(i.e. integers whose GCD with n
is 1). There happens to be a nifty equation for this (since p
and q
are relatively prime).Φ(n) = (p — 1) * (q — 1) = 6
3.计算Φ(n)
,这被称为欧拉(Euler)张力。 这个计数的正整数的数小于n
它们是互质n
(即整数,其具有GCD n
为1)。 恰好有一个漂亮的方程式(因为p
和q
是相对质数)。 Φ(n) = (p — 1) * (q — 1) = 6
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 (bolded, italicized integers are coprime with 14)
1,2,3,4,5,6,7,8,9,10,11,12,13,14(粗体,斜体整数是互质与14)
4. Compute e
(for encryption) such that:1 < e < Φ(n)
andGCD(e, Φ(n)) = 1
4.计算e
(用于加密),使得: 1 < e < Φ(n)
且GCD(e, Φ(n)) = 1
Since Φ(n) = 6
in our case, we only have these options: 2,3,4,5. And only 5 meets the second criterion.
由于在我们的情况下Φ(n) = 6
,所以我们只有以下选项:2,3,4,5。 并且只有5个满足第二个条件。
5. Compute d
(for decryption) such that:d * e ≡ 1 mod Φ(n)
d * 5 ≡ 1 mod 6
5.计算d
(用于解密),以便: d * e ≡ 1 mod Φ(n)
d * 5 ≡ 1 mod 6
This means d
can be 5, or 11, or 17, and so on. We’ll choose d = 17
.
这意味着d
可以为5、11或17,依此类推。 我们将选择d = 17
。
That’s it, we’re done! We now have:
就是这样,我们完成了! 我们现在有:
The “Lock” 🔒: (e, n) = (5, 14)
The “Key” 🔑: (d, n) = (17, 14)
“锁”🔒: (e, n) = (5, 14)
5,14)“锁”🔑: (e, n) = (5, 14)
17,14 (d, n) = (17, 14)
As shown before, senders can use the first pair of numbers to encrypt messages, and the receiver can use the second pair of numbers to decrypt messages.
如前所示,发送者可以使用第一对数字来加密消息,而接收者可以使用第二对数字来解密消息。
为什么这样做有效? (Why Does This Work?)
The basic principle underlying RSA is that for all integers 0 ≤ m < n
, it is practical to find positive integers e
, d
, and n
such that:(mᵉ mod n)ᵈ mod n = m
and such that even if e
and n
are made public, it is extremely difficult to find out what d
is.
RSA的基本原理是,对于所有(mᵉ mod n)ᵈ mod n = m
0 ≤ m < n
整数,找到正整数e
, d
和n
使得: (mᵉ mod n)ᵈ mod n = m
并且即使e
和n
由于d
是公开的,因此很难找出d
是什么。
Note: in our example, m
was 3.
注意:在我们的示例中, m
为3。
In other words, this means it’s relatively easy to find three numbers such that, if we apply them in a two-part formula to a number m
, we get that same number back. The magic part is, we can publicize two of those numbers so that anyone can apply the first part of the formula, and we can do so without compromising the third and final number (which is necessary to compute m
).
换句话说,这意味着找到三个数字相对容易,因此,如果我们将它们以两部分式应用于数字m
,则会得到相同的数字。 最神奇的部分是,我们可以公开这些数字中的两个,以便任何人都可以应用公式的第一部分,并且我们可以在不影响第三和最后一个数字的情况下这样做(这对于计算m
是必需的)。
There’s one key point we glossed over on the last page — what makes it so difficult to find d
? This is an important question to answer, as this difficulty is what makes RSA secure.
我们在最后一页上提到了一个关键点-为何很难找到d
? 这是需要回答的重要问题,因为此困难使RSA安全。
Let’s review what information is publicly available: everyone knows what e
and n
are, and everyone knows that d * e ≡ 1 mod Φ(n)
d * e ≡ 1 mod ((p — 1) * (q — 1))
让我们回顾一下公开的信息:每个人都知道e
和n
是什么,每个人都知道d * e ≡ 1 mod Φ(n)
d * e ≡ 1 mod ((p — 1) * (q — 1))
Thus, the most obvious way to find d
is to find p
and q
, and then solve for that equation. However, this requires computing the prime factorization of n
(remember, n = p * q
), and if n
is really big, then this problem is really hard. As for why it’s hard — well, that’s out of our scope, but you can check out the resources to learn more.
因此,找到d
的最明显方法是找到p
和q
,然后求解该方程。 但是,这需要计算 n
的素因式分解 (请记住, n = p * q
),如果n
很大,那么这个问题就很难解决。 至于为什么很难-好吧,这超出了我们的范围,但是您可以查看资源以了解更多信息。
数字签名 (Digital Signatures)
One last thing before we’re done. In the example, we saw how RSA can be used to encrypt a message, so that it can be securely delivered to its recipient.
我们要做的最后一件事。 在该示例中,我们看到了如何使用RSA来加密消息,以便可以将其安全地传递到其收件人。
RSA has another common use case — digital signatures. Continuing with our previous example, if Bob wants to digitally sign a message, i.e. prove that a message comes from him, he can encrypt it with his private key, (17, 14)
. Notice that before, this was used for decryption. Now it is being used for encryption! This means that anyone can decrypt the message with Bob’s public key, (5, 14)
.
RSA还有另一个常见用例-数字签名。 继续前面的示例,如果Bob想要对消息进行数字签名 ,即证明消息来自他,他可以使用自己的私钥对其进行加密(17, 14)
。 请注意,之前,这是用于解密的。 现在,它正在用于加密! 这意味着任何人都可以使用Bob的公钥(5, 14)
5,14)解密消息。
If the message is decrypted with (5, 14)
, the result will be reasonable. If it is decrypted with something else, the result will be gibberish.
如果使用(5, 14)
5,14)解密消息,则结果将是合理的。 如果用其他方式解密,结果将是乱码。
Note that this method assumes that everyone trusts that (5, 14)
is indeed Bob’s public key.
请注意,此方法假定每个人都相信(5, 14)
5,14 (5, 14)
确实是Bob的公钥。
资源资源 (Resources)
Eddie Woo’s RSA videos
吴宇森的RSA视频
Eddie Woo’s RSA videoshttps://www.youtube.com/watch?v=4zahvcJ9glg
埃迪·伍(Eddie Woo)的RSA视频https://www.youtube.com/watch?v=4zahvcJ9glg
Eddie Woo’s RSA videoshttps://www.youtube.com/watch?v=4zahvcJ9glghttps://www.youtube.com/watch?v=oOcTVTpUsPQ
埃迪·伍(Eddie Woo)的RSA视频https://www.youtube.com/watch?v=4zahvcJ9glg https://www.youtube.com/watch?v=oOcTVTpUsPQ
https://sites.google.com/site/danzcosmos/why-rsa-encryption-is-secure
https://sites.google.com/site/danzcosmos/why-rsa-encryption-is-secure
- TCP/IP Illustrated, Vol. 1: The Protocols (Ch. 18) TCP / IP说明,卷 1:协议(第18章)
rsa加密