ctf:xls加密_加密:爱丽丝和鲍勃的故事

这篇博客介绍了CTF挑战中的一个Excel文件加密问题,来源于https://medium.com/swlh/encryption-the-fated-story-of-alice-and-bob-fa2a40637636。内容涉及如何解密加密的Excel文件。
摘要由CSDN通过智能技术生成

ctf:xls加密

Today’s world runs on data. We’ve generated more data in the past two years than in our entire history combined. This data revolution has also allowed us to communicate more than ever before. Humanity revolves around communication, especially private communication — but how do we keep communication data private?

牛逼 ODAY的世界上运行的数据。 在过去两年中,我们生成的数据超过了整个历史的总和。 数据革命也使我们比以往任何时候都可以进行更多的交流。 人类围绕通讯,尤其是私人通讯展开,但是我们如何保持通讯数据的私密性呢?

Gone are the days where spies secretly exchanged envelopes with secret codes in them at a nondescript park bench. Gone are the days of the Enigma machine and substitution ciphers. Encryption has evolved quite a bit, and it holds some very interesting math. I love math, and the math involved in encryption is very elegant — and very clever.

间谍在不加描述的公园长凳上秘密交换带有秘密代码的信封的日子已经一去不复返了。 谜机器和替代密码的时代已经一去不复返了。 加密已经发展了很多,并且其中包含一些非常有趣的数学运算。 我喜欢数学,并且加密涉及的数学非常优雅-而且非常聪明。

什么是加密? (What is encryption?)

The goal of encryption is to take a message (in plain text) and scramble it so no one except the person you want to send the message to can read it. To understand this better, let’s take the example of the immortal Alice and Bob.

加密的目的是获取一条消息(以纯文本格式)并对其进行加密,因此,除了要发送该消息的人之外,其他任何人都无法读取它。 为了更好地理解这一点,让我们以不朽的爱丽丝和鲍勃为例。

Alice is madly in love with Bob, and she wants to tell him. However, she doesn’t want anyone else to know, just in case Bob doesn’t love her back. How can she send the message to Bob?

爱丽丝疯狂地爱上了鲍勃,她想告诉他。 但是,她不想让其他人知道,以防万一鲍勃不爱她。 她如何将消息发送给Bob?

One way would be to use a simple 1-to-1 encoding system — we map each character to another character and encrypt the message. Let’s say a = 1 , b = 2 and so on. So:

一种方法是使用简单的1对1编码系统-我们将每个字符映射到另一个字符并加密消息。 假设a = 1b = 2 ,依此类推。 所以:

'i love you' = 9 0 12 15 ...

Cool! Alice now has a nice, garbled message that no one can read! The only problem, of course, is that Bob can’t read it either. How does she tell Bob how to decrypt the message? Simple encryption schemes like this fail because there is no way to send the common key without using a more advanced encryption system. If Alice tries to encrypt the key, then the key to that key has to be sent too. So unless Alice is willing to pluck up the courage to talk to Bob in-person (secretly at a nondescript park bench of course), she can’t encrypt her message in such a simple way.

凉! 爱丽丝现在有一条很好的,乱码的消息,没人能读! 当然,唯一的问题是鲍勃也无法阅读。 她如何告诉鲍勃如何解密消息? 像这样的简单加密方案失败了,因为如果不使用更高级的加密系统就无法发送公用密钥 。 如果爱丽丝(Alice)尝试加密密钥,则也必须发送该密钥的密钥。 因此,除非爱丽丝(Alice)愿意鼓起勇气与鲍勃(Bob)面对面交谈(当然是秘密地坐在公园长椅上),否则她无法以这种简单的方式加密信息。

This is how kids make “secret codes”. Both parties have to be in on the cipher for it to work, but this throws us into the realm of paradox.

这就是孩子们制作“秘密密码”的方式。 双方都必须掌握密码才能起作用,但这使我们陷入了悖论的境地。

Public-key encryption introduces asymmetry to a simple cipher system. The idea is that the key used to encrypt the message is different from the key used to decrypt the message. Public-key encryption is notoriously difficult to wrap one’s head around, but I’ll try my best to explain. Here’s one analogy that I find helpful:

公钥加密将不对称性引入了简单的密码系统。 这个想法是,用于加密消息的密钥不同于用于解密消息的密钥。 众所周知,公钥加密很难让人,舌,但是我会尽力解释。 这是一个我认为有用的类比:

Imagine both Alice and Bob have a mailbox. Each mailbox has 2 different locks. One lock opens a small slot to put letters in, and the other opens the entire mailbox for mail to be taken out.

想象一下,爱丽丝和鲍勃都有一个邮箱。 每个邮箱有2个不同的锁。 一个锁打开一个小插槽以放入信件,另一个打开整个邮箱以取出邮件。

Image for post
The blue door is the only way to get mail out of the mailbox. Beautiful drawing by yours truly:)
蓝门是从邮箱中取出邮件的唯一方法。 真正的精美图画:)

The principle of public-key encryption is that everyone has the key used to encrypt messages (i.e. put mail through the red slot). This means that Bob has made many copies of the red key, and has even left a red key on top of the mailbox. However, only Bob has the blue key that is needed to actually see Bob’s mail. Thus, for Alice to send a secret message all she would have to do was encrypt it using Bob’s public key, and no one would be able to decipher it except Bob.

公钥加密的原理是每个人都有用于加密消息的密钥(即,将邮件放入红色插槽)。 这意味着Bob制作了许多红色密钥副本,甚至在邮箱顶部留下了红色密钥。 但是, 只有鲍勃拥有才能看到鲍勃的邮件的蓝键 。 因此,要让Alice发送一个秘密消息,她所要做的就是使用Bob的公共密钥对它进行加密,除了Bob之外没有人可以解密它

That’s how RSA encryption works.

这就是RSA加密的工作方式。

Let’s turn this into a more formal definition. Public key encryption revolves around an asymmetric system with two keys. The keys are related in such that a message encrypted by one key can only be decrypted with the other key. However, the keys cannot be derived from one another (we’ll get into that later). One of these keys is designated as the public key, and the other is the private key. The private key is never shared with anyone except its owner. The public key is available for everyone to see.

让我们将其转化为更正式的定义。 公钥加密围绕具有两个密钥的非对称系统展开。 密钥之间的关系是这样的: 用一个密钥加密的消息只能用另一个密钥解密 。 但是,密钥不能彼此派生(稍后再讨论)。 这些密钥之一被指定为公共密钥,另一个被称为私有密钥。 私有密钥永远不会与除其所有者以外的任何人共享。 公开密钥可供所有人查看。

This is great for sending encrypted messages, but it has some other interesting applications. For example, say Alice uses her private key to encrypt the message. When she sends it, anyone can decrypt it using her public key — but the fact that it can be decrypted by her private key means that it was definitely Alice who sent that message. Alice can even encrypt using her private key and then Bob’s public key after that! This would mean that only Bob can see the message, but he knows that Alice sent it.

这对于发送加密的消息非常有用,但是它还有其他一些有趣的应用程序。 例如,假设爱丽丝使用她的私钥来加密消息。 当她发送消息时,任何人都可以使用她的公钥对其进行解密-但是可以用她的私钥对其进行解密的事实意味着, 肯定是爱丽丝发送了该消息 。 爱丽丝甚至可以使用其私钥进行加密,然后再使用鲍勃的公钥进行加密! 这意味着只有Bob可以看到该消息,但是他知道Alice发送了该消息。

Image for post

This is where it can start to get a little confusing to just deal with Alice and Bob. Here’s a Star Wars analogy with something a little more concrete.

在这里,与Alice和Bob打交道可能会引起一些混乱。 这是《星球大战》的类比,但有些具体。

In Episode IV, Princess Leia needs to send a message to Obi-Wan Kenobi.

在第4集中,Leia公主需要向Obi-Wan Kenobi发送消息。

help me obi-wan kenobi! you're my only hope. signed, leia

She uses her private key to encrypt this message and then uses Obi-Wan’s public key to encrypt in once again. She puts it in our friend R2-D2, who successfully delivers it to Obi-Wan. Obi-Wan decrypts this with his private key. Now he sees the garbled mess of a message, so he can’t read it. The last step he has to do is decrypt the message one more time with Leia’s public key, confirming that this message is definitely from Leia.

她使用她的私钥对该消息进行加密,然后使用Obi-Wan的公钥再次进行加密。 她把它放在我们的朋友R2-D2中,后者成功地将它交付给Obi-Wan。 Obi-Wan用他的私钥对此解密。 现在,他看到了一条乱七八糟的消息,因此他看不懂它。 他要做的最后一步是使用Leia的公钥再次解密该消息,确认此消息肯定来自Leia。

This is a concept known as “signing”. It adds a layer of identity verification, where otherwise anyone could claim to Leia.

这是一个称为“签名”的概念。 它增加了一层身份验证,否则任何人都可以要求Leia。

好吧好吧,我明白了。 但是它如何工作? (Ok ok, I get it. But how does it work?)

When I first learned about encryption, it drove me absolutely mad. I perfectly understood the whole public/private key system, but I just couldn’t wrap my head around how the actual messages get garbled, why we could be sure the keys couldn’t be derived from each other, and how the messages actually get scrambled. It turns out it’s non-trivial but very clever. If you’d rather not go through a lot of math, then I suggest you move on now. However, I think all of this is super interesting, so let’s dive in.

当我第一次学习加密时,它使我非常疯狂。 我完全理解了整个公钥/私钥系统,但是我无法理解实际消息是如何乱码的,为什么我们可以确定不能从彼此派生密钥以及消息如何真正获得,乱七八糟。 事实证明,这很简单但很聪明。 如果您不想学习大量数学,那么我建议您现在继续。 但是,我认为所有这些都非常有趣,因此让我们开始吧。

RSA works, as we said, on a system of 2 keys: public and private. It has 4 different steps:

正如我们所说,RSA在包含两个密钥的系统上工作:公共密钥和私有密钥。 它具有4个不同的步骤:

  1. Key generation

    密钥生成

  2. Key distribution

    密钥分配

  3. Encryption

    加密

  4. Decryption

    解密方式

The public key is represented by two integers: n and e. The private key is represented by d. We’re going to be using some interesting mathematical notation later on, but I’ll explain as we encounter them.

公钥由两个整数表示: ne 。 私钥用d表示。 稍后我们将使用一些有趣的数学符号,但是在遇到它们时我将进行解释。

Key distribution is fairly trivial, so I won’t go into it in this article.

密钥分发是相当琐碎的,因此在本文中我将不再赘述。

步骤1:产生金钥 (Step 1: Generate Keys)

At a fundamental level, RSA is an algorithm. That means we have steps that we need to follow.

从根本上讲,RSA是一种算法。 这意味着我们需要遵循一些步骤。

Choose 2 prime numbers, p and q.

选择2个质数pq

This might seem hard, but prime numbers are pretty common. We also have blazingly fast algorithms to test if a number is prime (called primality tests), so finding primes isn’t that hard. For security, p and q should be chosen randomly. They are also very big primes, but for the sake of this example, we’ll keep them fairly small.

这看似很难,但素数却很常见。 我们还拥有出众的快速算法来测试数字是否为质数(称为素数测试),因此查找质数并不难。 为了安全起见,应随机选择pq 。 它们也是非常大的素数,但是为了这个例子,我们将它们保持得很小。

Next, we calculate n as the product of p and q.

接下来,我们将n计算为pq的乘积。

Image for post
WLOG = Without Loss of Generality
WLOG =不失一般性

Cool? Great.

凉? 大。

Now we take the totient of n. Euler’s totient function is a function that returns the number of numbers that are coprime to n that are less than n. For example:

现在我们取n总和 。 Euler的totient函数是一个函数,该函数返回与n互质数小于n的数。 例如:

Image for post

The only number less than 6 that is coprime (or relatively prime) to 6 is 5. Coprime means that the two numbers share no factors. In this specific case:

唯一小于6的互质数(或相对质数)为6的数字是5。互质数意味着这两个数字不共享任何因数。 在这种情况下:

Image for post

Intuitively, this is easy to understand. n is the product of two primes p and q, so any other number is trivially coprime. This expression expands to:

直观上,这很容易理解。 n是两个质数pq的乘积,因此任何其他数都是平凡的互质数。 该表达式扩展为:

Image for post
There are p multiples of q and q multiples of p, hence subtracting p and q. We add 1 to get rid of the overcounting of pq.
有q的p倍和p的q倍,因此减去p和q。 我们加1以消除pq的过度计数。

In our case,

就我们而言

Image for post

Now we choose an integer e with these restrictions:

现在我们选择一个具有以下限制的整数e

Image for post

That second one just means that e and totient n are coprime. e is the public key exponent. e = 5 for our example.

第二个仅表示e和总n是互素的。 e是公用密钥指数。 在我们的示例中, e = 5。

Now we need to find d. d needs to have a specific value:

现在我们需要找到dd需要具有特定值:

Image for post

Now we’re getting into the thick of things. This is modular arithmetic. You can think of mod as “math on a clock”. A number is congruent to a number mod k when they leave the same remainder when divided by k. That three-lined equal sign stands for congruence. For example:

现在我们正在深入研究。 这是模块化算法。 您可以将mod视为“时钟上的数学”。 一个数字是全等的数模k,当他们离开相同的余数时,除以k。 那三行等号代表全等。 例如:

Image for post
Numbers mod k are always less than k, since you can’t have a remainder more than the divisor.
数字mod k始终小于k,因为您的余数不能超过除数。

For example, when we track time we do so mod 60. We count seconds from 1 to 59, but as soon as we hit 60 seconds we go back to 0 (and add 1 to the minute count). Modular arithmetic is very useful, especially in number theory.

例如,当我们跟踪时间时,我们将其设置为60。我们将秒数从1计数为59,但是一旦达到60秒,我们便返回0(并将分钟数加1)。 模块化算术非常有用,尤其是在数论中。

We have to find a number d so that when we multiply d by 3, we get a number that is 1 more than a multiple of 4. You can do this using the Extended Euclidean Algorithm, but I’ll leave it out for the sake of simplicity. For now, it’s pretty easy to find a d that works.

我们必须找到一个数字d,以便将d乘以3时,我们得到的数字比4的倍数大1。您可以使用Extended Euclidean Algorithm来执行此操作,但为了方便起见,我将其省略简单。 现在,找到一个可行的d很容易。

Image for post
5*5 = 25, which is one more than 24.
5 * 5 = 25,比24多1。

To recap, we have now found both the private and public keys. d is the private key, so we have to keep that secret. p, q and ϕ(n) also have to be kept secret, since they are used in the derivation of d. Given any of these, d can be calculated with the already public e. In fact, these numbers can just be discarded once d is calculated.

回顾一下,我们现在已经找到了私钥和公钥。 d是私钥,因此我们必须保密。 pqϕ(n)也必须保持秘密,因为它们用于d的派生。 给定其中任何一个,可以使用已经公开的e来计算d 。 实际上,一旦计算出d,就可以丢弃这些数字。

Notice that while creating a private key is very easy, deriving the private key from a public key is very, very hard. The only way to do so is to find the prime factors p and q — this is a notoriously hard problem. It’s like mixing two paints and then trying to derive the exact colours of paint used, not to mention getting the exact proportion.

请注意,虽然创建私钥非常容易,但是从公钥派生私钥却非常困难。 这样做的唯一方法是找到素因子pq-这是一个众所周知的难题。 这就像混合两种涂料,然后尝试得出所用涂料的确切颜色一样,更不用说获得确切的比例了。

让我们加密一条消息! (Let’s encrypt a message!)

Image for post

The first step is to take this plain text and convert it into a sequence of numbers. Let’s use a simple encoding system, where a=1, b=2 and so on. To clarify, this step isn’t to make the message harder to read or anything, it’s just so that we can perform mathematical operations on the text.

第一步是获取纯文本并将其转换为数字序列。 让我们使用一个简单的编码系统,其中a = 1,b = 2,依此类推。 要澄清的是,此步骤并不是要使消息更难阅读或其他任何内容,只是为了使我们可以对文本执行数学运算。

Let’s say Obi-Wan is trying to get a message to Master Yoda. He has his text in numbers, so the next step is to encrypt! He needs to use Yoda’s public key, which we’ll say is the example we worked above. He raises all the numbers to the power of e, giving:

假设Obi-Wan试图向Yoda大师发送消息。 他的数字带有文字,因此下一步就是加密! 他需要使用Yoda的公钥,我们将说这是我们上面的示例。 他将所有数字提高到e的幂,得到:

Now he takes all the numbers mod n. Sidenote: when these steps are combined together, they can be run very fast even on very large numbers. Recall the clock analogy of modular arithmetic — numbers mod n are represented on the clock face. Instead of calculating everything and then taking mod n, we can instead just spin the hands on the clock face to get our result.

现在,他将所有数字取为mod n。 旁注:将这些步骤组合在一起时,即使数量很大,它们也可以非常快速地运行。 回想一下模块化算术的时钟类比-在时钟面上表示数字mod n。 不用计算所有数据然后取mod n,我们可以只旋转表盘上的指针来获得结果。

Perfect! Now Obi-Wan sends this to Master Yoda. Yoda has his private key d, which will give us the right message back. We raise the numbers to the power d and take mod n again, and voila!

完善! 现在,欧比旺将其发送给尤达大师。 尤达(Yoda)有他的私钥d ,它将给我们正确的信息。 我们将数字提高到d ,再取mod n ,瞧!

Yay! Although it would’ve been less work to just use the Force…

好极了! 尽管仅使用原力将减少工作量……

免责声明 (Disclaimers)

Before we move on, it’s important to know that this is a simplified version. RSA uses a different function called Carmichael’s totient function instead of Euler’s totient function. Euler’s function is still mathematically sound, but in some cases, it generates larger numbers than necessary. Carmichael’s totient function fixes that.

在继续之前,重要的是要知道这是一个简化的版本。 RSA使用另一个称为Carmichael的totient函数的函数,而不是Euler的totient函数。 欧拉函数在数学上仍然是可靠的,但是在某些情况下,它会生成比所需数量更大的数字。 Carmichael的totient函数可以解决此问题。

There are also a couple of problems that our basic algorithm has. For one, using small e makes this very weak. Our small choices for p and q make it weak already, but this is different. If you have a small integer value for the message, the encrypted message will just be the message m to the power e. This can easily be broken just by taking the e-th root of the encrypted message. Once e gets past a certain threshold, however, it becomes impossible to decrypt without the private key.

我们的基本算法还存在一些问题。 例如,使用小e会使它非常弱。 我们对pq的小选择已经使它变弱了,但这是不同的。 如果消息的整数值较小,则加密的消息将仅是幂为e的消息m仅通过采用加密消息的第e个根即可轻松解决此问题。 但是,一旦e超过某个阈值,就没有私钥就无法解密。

Our basic RSA is also completely deterministic, in that if you put the same text in, you will get the same encryption out. This makes it vulnerable to chosen-ciphertext attacks. If you’re interested, read this:

我们的基本RSA也是完全确定性的,因为如果您输入相同的文本,则会得到相同的加密。 这使其容易受到选择密文攻击 。 如果您有兴趣,请阅读以下内容:

To combat this, actual implementations of RSA employ random padding, where a message is randomly padded to make it semantically secure (i.e. the encryption is a bit different every time).

为了解决这个问题,RSA的实际实现采用随机填充,即随机填充一条消息以使其在语义上安全(即每次加密都略有不同)。

It’s also very important to understand that while our public and private keys were the same in this case, normally this wouldn’t be so. Since our numbers are small, we’re limited in our choices.

同样重要的是要了解,尽管在这种情况下我们的公钥和私钥是相同的,但通常情况并非如此。 由于我们的人数很少,因此我们的选择有限。

And, for the final note, we have to talk about quantum. Shor’s algorithm (an algorithm I’d like to write about in the future) makes factoring really big numbers trivial, and thus completely breaks this RSA system. However, as of right now and for the foreseeable future, quantum computers are nowhere close to being able to factor the massive 2048-bit integers that are commonly in use today. Besides, we have other cryptographic systems (that I will hopefully discuss in the future) that are more resistant to the quantum revolution.

而且,最后一点,我们必须谈谈量子。 Shor的算法(我将来会写一个算法)使分解大量数字变得微不足道, 从而完全破坏了这个RSA系统 。 但是,从现在开始以及在可预见的将来,量子计算机还无法接近当今普遍使用的2048位整数 。 此外,我们还有其他的密码系统(我希望将来会讨论这些密码系统)更能抵抗量子革命。

就是这样! (And that’s it!)

Thanks so much for reading! Here are some key takeaways:

非常感谢您的阅读! 以下是一些关键要点:

  • Encryption is all about garbling messages in such a way that only the intended reader can read it

    加密就是为了以一种只有目标读者才能阅读的方式来欺骗消息
  • Simple key-based ciphers can be easily broken since the key itself needs to be transmitted securely as well

    基于密钥的简单密码很容易被破解,因为密钥本身也需要安全地传输
  • Public-key encryption solves this by having two keys, public and private

    公钥加密通过拥有两个密钥(公钥和私钥)解决了这一问题
  • The public key encrypts information so that only the private key (which is only known by the intended target) can decrypt it

    公钥对信息进行加密,以便只有私钥(只有目标目标才知道)才能解密它
  • RSA is an algorithm, based around concepts in number theory

    RSA是一种基于数论概念的算法
  • The mathematical operations performed in RSA encryption can only be reversed by the private key

    RSA加密中执行的数学运算只能由私钥反转
  • The private key can only be found by breaking down a product of 2 very big primes into its factors — something that we have yet to do efficiently

    只能通过将2个非常大的素数的乘积分解成其因数来找到私钥,而这是我们尚未有效完成的工作

Feel free to reach out to me at dron@intellear.com. Stay safe!

请随时通过dron@intellear.com与我联系。 注意安全!

翻译自: https://medium.com/swlh/encryption-the-fated-story-of-alice-and-bob-fa2a40637636

ctf:xls加密

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值