Skip to main content

Public Key Cryptography: The Mathematics of RSA

 Initially, when I created this blog, I stated that only opinions would be contained here. However, it has become very clear to me that I am obviously better at explaining things than I am at expressing complex opinions. And beyond that, the articles I enjoyed writing the most were the explanations about topics in computer science.

That brings us to now. Moving forward, I'd like to shift my focus toward explaining topics I am interested in as well as continuing to express my concerns and opinions. I am, however, going to try to move more toward opinion articles that require some explanation as to the principles at work.

On to the Maths!

 Note: If, at this point, you haven't read my article on public-key cryptography, I would highly suggest that you read it before continuing. You can find it here.

In this article, I will be explaining the mathematics of RSA. RSA is but one example of public-key cryptography and there are many out there, but today we'll only be looking at RSA.

In the RSA cryptosystem, we'll be dealing with modulo-arithmetic quite a lot, so let's get the basics out of the way:

Let $a$, $b$, and $n$ be natural numbers. Then, $a$ is said to be congruent to $b \mod n$ if the remainder after dividing $a$ by $n$ is equal to $b$. This is written as:

$$a \equiv b\ mod\ n$$

You can think of this as a clock with $n$ hours on it's face. To say that $a$ and $b$ are congruent modulo $n$ is equivalent to saying that if you start at the topmost point of the clock and move clockwise $a$ hours, you will land on the same spot as if you had moved $b$ hours.

First, we'll need to generate two prime numbers, $p$ and $q$. We'll multiply those together to get $n$. Next, we calculate the Euler totient of n (written $\phi(n)$). This is as simple as calculating $ (p-1)(q-1) $. We then choose an exponent $e$ such that $e$ and $\phi(n)$ are co-prime. The tuple $(e, n)$ is our public key.

Now, to derive our private key, we need to calculate the multiplicative inverse of $e \mod n$. That is, we need to find some $d$ for which $e \cdot d  \equiv 1 \mod \phi(n)$. I won't go into it, but you can accomplish this using the extended euclidean algorithm.

The tuple $(d, n)$ is now our private key.

Now that we have a public and private key, how do we actually encrypt and decrypt messages?

To encrypt a message $M$, we use this simple formula:

$$C=M^e \mod n$$

Where $C$ is the ciphertext and $M$ the message. We can encode our message as a number very easily by just interpreting it's binary representation as a number. There is a bit more complexity to it than that, but for now let's just assume this can be done.

To decrypt some ciphertext $C$, we have an analogous formula utilizing our private key:

$$M = C^d \mod n$$

Now, you might say that, since we could calculate the private key from the public key, this is not a very secure system, but to calculate the private key we had to know the factors of $n$ since we are actually calculating $(p-1)(q-1)$ where $n=p \cdot q$ and $p$ and $q$ are primes. And since we are discarding the totient after deriving the keys, we would need to factor $n$ to recalculate the totient. For small numbers, this doesn't seem like too difficult a task, but for a large enough $n$, it could take longer than the expected lifetime of the universe to calculate. (At least for the moment!) This is simply because we don't really have any better way of factoring a number than simply guessing a factor and checking if it works.

In the public-key cryptography article, I explained how it could be useful to reverse the roles of the keys in this system, and you can see from the two formulas I've given here that you could just as easily encrypt a message using $(d, n)$ as you could using $(e, n)$.

That wraps up this brief explainer of the mathematics of RSA cryptography. In a future article, I hope to dive deeper into the world of public-key cryptography and explain some concepts like digital signatures and certificates, more thoroughly. I'd also like to expand a bit on exactly how RSA and other public-key cryptography is used in practice.

I hope you all enjoyed the article and I hope to write another one soon.


Comments

Popular posts from this blog

The case for unmanaged programming languages: data management

 Welcome back! This is the second and final part in a series of articles about why unmanaged programming languages are the best. In this article, I will be covering the ways in which unmanaged programming languages, such as C++, are far superior to managed languages when it comes to data management and manipulation. Pointers Pointers are literally the best thing since sliced bread...  They seem like such a simple concept, but oh man are they powerful! Basically, a pointer is a variable that holds a memory address. It's actually a little more simple than that. A pointer, at it's lowest level, just holds an integer. This means you can do integer arithmetic with it. It might not be immediately obvious why this is useful, but I'll get to that in a minute. The first thing pointers allow you to do that is pretty neat is to change the data type of a variable very easily. Now, this isn't strictly speaking converting the data to another ty...

Why you (yes, you!) should be using Telegram

 If you've not been living under a rock these past few months, you will have heard about the (now postponed) changes to WhatsApp's privacy policy. There are some problematic things about their privacy policy already, but I'll not be spending your precious time discussing legal and bureaucratic nonsense. Instead, I want to talk about and highlight some things that may or may not allow you to come to the (obviously correct) conclusion that you should be using an open-source messaging app like Telegram or Signal. Disclaimer: I am not sponsored by, nor was this article commissioned by, Telegram, LLC or any other company. I just happen to really like open-source software and think that Telegram is a great app. Now, as for why you shouldn't be using WhatsApp. Proprietary software is hazardous at best. In general, there are two types of software: proprietary and open-source. Proprietary being software where the underlying source code is not available to the end-user. Open-sour...
Creative Commons Licence
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.