What the Heck Is the RSA Algorithm?
By: Shivansh Sinha
Tags: Crypto, Computer Science, Computer Networking, Computer Security, Cryptography

Photo by Markus Spiske on Unsplash

ICE BREAKER:-

RSA algorithm is one of the profoundly discussed topics in the crypto field. There are several articles and videos that one could easily find related to it. My main motive to write this article is not just to explain the steps of the RSA algorithm but also to explain the underlying mathematics or logic in an intuitive way so that no one has to memorize the steps anymore.

So, let’s begin with a formal introduction of the RSA algorithm. RSA is an asymmetric cryptography technique that uses public/private keys to encrypt and decrypt data (We will see soon how they are generated). As the name suggests, the public key is shared publicly with everyone whereas the private key is always kept private and safe. As it is asymmetric cryptography so a document encrypted via a public key can only be unlocked via a private key and vice versa. RSA algorithm is named after its inventors, Ron Rivest, Adi Shamir, and Len Adleman.

Now let’s dive into the RSA algorithm and understand the steps involved and the mathematics/logic backing those steps.

UNDERSTANDING THE STEPS INVOLVED:-

1> Selecting two large prime numbers.

The first step in the RSA algorithm is to choose two large prime numbers. The strength of the algorithm directly depends upon the size of the prime numbers used. The bigger the primes chosen, the stronger is our crypto.

Now let us simply break it and understand the need of choosing two large primes. So basically the whole idea of an asymmetric algorithm works on a trapdoor function. Let us understand it via an example.

Say, p and q are the two large primes chosen. We can calculate another variable n = p*q (also called the modulo). So how is this a trapdoor function?

Calculating the value of n is pretty straight forward if you know the two primes. But, if you know the value of ‘n’, can you calculate the value of the two primes used? If we use large prime numbers, then n will only have two factors (other than 1 and itself ) that are p and q. So it will be very difficult to break n into its factors as there are only two factors which are very very large numbers. So brute-forcing it would be computationally costly and that is how it acts as a trapdoor, as it is easier from one end to calculate the value ’n’ but very difficult to reverse engineer it. And that is the secret power of the RSA algorithm. Moreover, that is the reason why it is always recommended to choose large primes.

2> Calculating ϕ(n), the Euler Totient function of n.

ϕ(n) simply means the count of all the numbers ≤ n which are coprime to n. So for example, if we assume n to be prime, then ϕ(n) would simply be n-1 (as all the numbers from 1 to n-1 would be coprime to n).

Now we face two questions, firstly how to find the value and why to find it. How is it useful? Now both of them could be answered using different Euler’s theorems. To calculate the value we use:-

The formula used to calculate the Euler totient function.

In the above-mentioned formula, p is nothing but the prime factors of n. So in our case n has only two prime factors, p, and q.

ϕ(n) = n*(1–1/p)*(1–1/q), now using n = p*q, we can easily prove ϕ(n) = (p-1)*(q-1).

Now, how is it relevant? To answer this, we need to know about another Euler’s theorem

m^ϕ(n) ≡ 1 (mod n) where m is any integer.

This formula will later be used to solve the final equation we have for encrypting and decrypting messages. Also, Fermat Little Theorem is a special case of this theorem. As if we take n as a prime number then ϕ(n) will be equal to n-1. And the above theorem reduces to Fermat Little Theorem.

3> Calculating the primary key ‘e’.

Finally, now we are in a position to calculate our primary key ‘e’. So our primary key should satisfy two conditions.

1 < e < ϕ(n)

gcd(e, ϕ(n)) = 1 (That is e must be a coprime to ϕ(n)).

Now we can pick any value that satisfies this condition and that will be our primary key.

4> Calculating the private key ‘d’.

So once we have our primary key, we can now find our private key. Our private key should also satisfy one condition, that is,

(e*d) ≡ 1 mod(ϕ(n)), and this is the reason why we have gcd(e, ϕ(n)) = 1 in our previous step. We will get a ‘d’ only if gcd(e, ϕ(n)) = 1, else there won’t be any such ‘d’ possible (modulo inverse concept).

Now if e*d mod (ϕ(n)) = 1, then we can simplify it as e*d = k*ϕ(n) + 1 where k is some integer. Now we will choose a k value s.t. we get an Integer value for ‘d’ (our secret private key).

5> Finally it is time, to encrypt and decrypt!!

Now when we have our public key ‘e’ and private key ‘d’, we can encrypt and decrypt data. Let’s use ‘c’ for the ciphertext and ‘m’ for the original message. Therefore,

To encrypt our plain text, we use c= (m^e)mod(n). To decrypt our cipher text, we use m= (c^d)mod(n).

For all the maths geeks who are interested to know how the above-mentioned formula works, I would strongly recommend them to visit https://brilliant.org/wiki/rsa-encryption/.

I hope this article didn’t get too complex. I tried my best to keep things simple and logical.

References