Encryption: the fated story of Alice and Bob
Tags: Cryptography, Number Theory, Encryption

Encryption: the fated story of Alice and Bob

The secrets of how we keep information secure

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?

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?

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:

'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.

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.

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.

That’s how RSA encryption works.

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.

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.

In Episode IV, Princess Leia needs to send a message to 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.

This is a concept known as “signing”. It adds a layer of identity verification, where otherwise anyone could claim to 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:

Key generation Key distribution Encryption 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.

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

Step 1: Generate Keys

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

Choose 2 prime numbers, p and q.

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.

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

WLOG = Without Loss of Generality

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:

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:

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:

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.

In our case,

Now we choose an integer e with these restrictions:

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

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

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:

Numbers mod k are always less than k, since you can’t have a remainder more than the divisor.

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.

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.

5*5 = 25, which is one more than 24.

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.

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.

Let’s encrypt a message!

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.

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:

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.

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!

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.

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.

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:

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).

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.

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

The mathematical operations performed in RSA encryption can only be reversed by the private key

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

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