Public Key Cryptography Simply Explained
By: Brandon Skerritt
Tags: Hacking, Security, Cryptography, Computer Science, Programming

Turning messages into numbers

We want to turn a message into numbers. Previously we assigned a number to each letter, A = 1 and so on. The American Standard Code for Information Interchange (ASCII) is a table of all English letters and most symbols along with their associated ASCII code & Binary output.

When you press a key on the keyboard, the keyboard converts this to Ascii as numbers are easier to work with than letters for a computer. If you want to learn more about ASCII, check out this video.

Let’s encrypt the word “cats”. In binary, according to Ascii, this is:

If you add them all together and convert to base 10, you get 4430123. For our example, we’re going to look at how Rivest–Shamir–Adleman (RSA), a public key cipher, calculates public & private keys. We’re also going to use much smaller numbers, so the maths isn’t as hard to read.

Below is a calculator I created for turning ASCII into Binary. View it better on my website ( https://skerritt.blog/how-does-public-key-cryptography-work/ ).

RSA

Choose 2 large prime numbers, p & q.

Prime numbers are numbers that only have 2 factors, 1 and itself. We’re going to pick 5 & 7, not large prime numbers but small for brevity.

2. Compute n = pq, z = (p-1)(q-1)

3. Choose e (with e < z) such that e has no common factors with z.

e = 5

5 has no common factors with 24, and it is smaller than 24.

4. Choose d such that ed — 1 is exactly divisible by z.

The easiest way to do this would be to loop over all possible values of d in code. This code is written in Functional Python, but the language and paradigm doesn’t matter.

Since we’re using such small numbers, we have overlap. Both e and d are 5. Let’s set d to 29, just so we don’t have this overlap.

d = 29

5. The public key is (n, e). The private key is (n, d)

Below is code to generate RSA keys. Note that we have overlap on d with p = 5 and q = 7, as discussed above.

To send an encrypted message, Bob computes C = m^e mod n for message m and key e. To decrypt the message, Alice computes m = c^d mod n.

Encrypting “cats” gives us 42⁷⁵ mod 35 = 7.

Decrypting “cats” gives us 4430123.

Then to send a message m, Bob computes c=m^e (mod N) and sends it to Alice and Alice decrypts the message using her private key d with m=c^d (mod N)

Why does it work?

Prime factorisation. It’s easy to multiply two prime numbers together, but it’s incredibly hard to find out what prime numbers were used to make that number. You can easily multiply these two together:

But if I gave you 992,474,117 and told you to find the prime numbers that were used to make this number, it’s not computationally feasible. Even more so when you realise the prime numbers used are very, very large.

This is known as a trap-door function or a one-way function. While it is easy to go through one way, it is computationally infeasible to go the other way. Boiling an egg is a one-way function because it is easy to boil an egg, but it is not possible to un-boil an egg. Let’s go deeper into the mathematics and explore modular arithmetic.

Back to modular arithmetic

Imagine a finite range of numbers, for example, 1 to 12. These numbers are arranged in a circle, much like a clock (modular arithmetic is sometimes called clock arithmetic because of this)

Count 13 around this clock. You get to 12 and then you need to count 1 more — so you go back to 1. Modular arithmetic is still defined as the remainder of division, however it can also be defined (and is more commonly defined) as a clock.

Functions using modular arithmetic tend to perform erratically, which in turn sometimes makes them one-way functions. Let’s see this with an example by taking a regular function and seeing how it works when it becomes a modular arithmetic function.

3^x

When we insert 2 into this function, we get ³² = 6. Insert 3 and we get ³³ = 9

This function is easy to reverse. If we’re given 9, we can tell that the function had an input of 3, because of ³³ = 9.

However, with modular arithmetic added, it doesn’t behave sensibly.

Image we had this formula:

3^x mod 7 = 1

How would you find out what x is? You can’t put the mod on the other side, because there isn’t really an inverse of modular arithmetic. What about guessing? Let’s input 5:

³⁵ mod 7 = 5

Okay, that was too big. You might want to go lower, maybe 4 or 3 but actually this is the wrong direction. When x is 6, it is equal to 1.

In normal arithmetic, we can test numbers and get a feel for whether we are getting warmer or colder, but this isn’t the case with modular arithmetic.

Often the easiest way to reverse modular arithmetic is to compile a table for all values of x until the right answer is found. Although this may work for smaller numbers, it is computationally infeasible to do for much larger numbers. This is often why modular arithmetic is known as a one-way function.

If I gave you a number such as 5787 and told you to find the function for it, it would be infeasible. In fact, if I gave you the ability to input any number into the function it would still be hard. It took me a mere few seconds to make this function, but it’ll take you hours or maybe even days to work out what x is.

RSA is a one-way function. While it is relatively easy to carry out this function, it is computationally infeasible to do the reverse of the function and find out what the keys are. Although, it is possible to reverse an RSA encryption if you know some numbers such as N.

Let’s talk about N

Recall earlier where I detailed how the RSA algorithm worked. There was one number, $n$. This n is special because under some circumstances n can make this one-way function reversible

N is a product of 2 prime numbers. As we saw earlier, if we take $5$ and $7$ and multiply them together, we get:

n = 35

In order for Bob to send Alice a message, he encrypts the message using Alice’s public key. Now that the message is encrypted, there has to be some way for Alice to decrypt it. There has to be some way for Alice to reverse this, but only for Alice to reverse it. You can’t have Eve or Niamh or Hannah reversing it — because that beats the point of encrypting it.

RSA is designed so the person who knows P and Q (the two prime numbers that are multiplied together to give N) can decrypt the message.

Although Alice has told the world her public key is n = 35, no one apart from Alice knows that P = 7, Q = 5. Note that the prime numbers are intentionally small for brevity.

You may be thinking “it’s easy to guess that 35’s prime factors are 5 and 7” and you would be right. But, with large enough numbers it is virtually impossible to find p and q.

In fact, with large enough numbers multiplying p and q are essentially one way functions. It is possible that in the future, perhaps the near future (with the invention of quantum computers) that factoring numbers becomes easy. Mathematicians have tried and failed for thousands of years to find an efficient way to factor numbers, so for now it is considered secure.

Let’s look more into the maths

Okay, let’s look at how modulus works in all of this. You understand why multiplication works, and how modulus works. But what about the other equations? What are they for?

Let’s demonstrate the deciphering algorithm using an identity due to Euler and Fermate:

For any integer (M), M is relatively prime to n:

This is the Euler totient function giving the number of positive integers less than n which are relatively prime to n. Relatively prime is where 2 numbers only share the factor 1 with each other. In modern day we use Carmichael’s function over Euler’s function, as Euler’s function can sometimes produce numbers too large to use. However, we’re using Euler’s totient function as it is what the original RSA paper used.

This sounds confusing, but let’s break it down. By elementary properties of the totient function:

Since d is relatively prime to ϕ i (n), it has a multiplicative inverse e in the ring of integers modulo $ϕ (n). What this means is that the formula we used for RSA can be reversed (the trap door can be reversed) given some knowledge about the numbers used.

Without this special mathematical property it wouldn’t be possible to reverse the encryption and find out the ciphertext if you know some of the numbers used.

The modular multiplicative inverse of the encryption algorithm c = m^e mod n is m = c^d mod n. All of this maths has built up to this. Modular arithmetic and one-way functions are heavily involved here. In order to encrypt, you calculate c. In order to decrypt, you calculate m. Both of these require knowledge of n, which is the special number we talked about earlier.

If you want to learn more about the maths of RSA, I highly reccomend the readable, origianl RSA paper.