Tag: generation

RSA (cryptosystem) – Part 3 (Encryption and Decryption)

This is the process of transforming a plaintext message into ciphertext, or vice-versa. The RSA function, for message m and key k is evaluated as follows:


There are two cases:

  1. Encrypting with the public key, and then decrypting with the private key.
  2. Encrypting with the private key, and then decrypting with the public key.

The two cases above are mirrors. I will explain the first case, the second follows from the first

Encryption: F(m,e)=memodn=c, where m is the message, e is the public key and c is the cipher.

Decryption: F(c,d)=cdmodn=m, where m is the message, d is the private key and c is the cipher.


Following the example from the previous post:

  1. We choose two distinct prime numbers, such asp=61 and  q=53
  2. Compute n = pq giving n=61.53=3233
  3. Compute the totient of the product as ϕ(n) = lcm(p − 1, q − 1) giving ϕ(3233)= lcm(60,52)=780
  4. Choose any number 1 < e < 780 that is coprime to 780. Choosing a prime number for e leaves us only to check that e is not a divisor of 780.Let e=17
  5. Compute d, the modular multiplicative inverse of e (mod ϕ(n)) yielding,d=413

The public key is (n = 3233, e = 17). For a padded plaintext message m, the encryption function is

c(m) = m17 mod 3233

The private key is (n = 3233, d = 413). For an encrypted ciphertext c, the decryption function is

m(c) = c413 mod 3233

For instance, in order to encrypt m = 65, we calculate

c = 6517 mod 3233 =2790

To decrypt c = 2790, we calculate

m = 2790413 mod 3233 = 65

Please note the whole security of RSA system depends on the selection of p and q for the algorithm. A real world example for p and q could be:



and as per the algorithm, you can calculate other parameters.

RSA is very helpful in building secure products and I hope you understood the basic concept of RSA with these posts:

RSA – Part 1

RSA – Part 2

RSA – Part 3

Sources: https://en.wikipedia.org/wiki/RSA_(cryptosystem)

RSA (cryptosystem) – Part 2 (Key Generation Algorithm)

The mathematics described in the previous post is enough to describe RSA and show how it works. RSA is actually a set of two algorithms:

  1. Key Generation: A key generation algorithm.
  2. RSA Function Evaluation: A function F, that takes as input a point x and a key k and produces either an encrypted result or plain text, depending on the input and the key.

Key Generation

The key generation algorithm is the most complex part of RSA. The aim of the key generation algorithm is to generate both the public and the private RSA keys.This has to be done correctly in order to generate secure RSA keys, else it opens up the system for many different attacks (We will talk about the attacks later). I will break down the steps involved to generate keys with the mathematics functions used within that:

  1. Large Prime Number Generation: Two large prime numbers p and q need to be generated. These numbers are very large up to 1024 digits or even 2048 digits. For security purposes, the integers p and q should be chosen at random, and should be similar in magnitude but differ in length by a few digits to make factoring harder.
  2. Modulus: From the two large numbers, a modulus n is generated by multiplying p and q.
  3. Totient: The totient of n, ϕ(n)is calculated.
  4. Public Key: A prime number is calculated from the range [1 , ϕ(n)] that has a greatest common divisor of 1 with ϕ(n).
  5. Private Key: Because the prime in step 4 has a gcd of 1 with ϕ(n), we are able to determine its inverse with respect to modϕ(n).

Let’s see how each step works with an example:

Large Prime Number Generation

RSA security depends on two very large prime numbers that are quite far apart. Generating composite numbers, or even prime numbers that are close together makes RSA totally insecure.

So how to generate large prime numbers? The answer is to pick a large random number and test for primness. If that number fails the prime test, then add 1 and start over again until we have a number that passes a prime test. For testing a big prime number we have many online/offline tools available which are also known as primality test (Example: Rabin-Miller primality test etc.).

Example: In this post, I am not going to show an example with 2 very large prime numbers. For ease of understanding, lets take two prime numbers as:

p=61 and q=53


Once we have our two prime numbers, we can generate a modulus very easily:


RSA’s main security foundation relies upon the fact that given two large prime numbers, a composite number (in this case n) can very easily be deduced by multiplying the two primes together. But, given just n, there is no known algorithm to efficiently determining n’s prime factors. It is considered a hard problem, many people throughout history had tried and failed to find the solution of this problem.

Example: Compute n = pq giving

n = 61 x 53 = 3233


With the prime factors of n, the totient can be very quickly calculated:



I talked about Totient in the last post. The reason why the RSA becomes vulnerable if one can determine the prime factors of the modulus is because then one can easily determine the totient.

Example: The actual totient calculation is done by taking in LCM but sometimes we may even use just multiplication of (p-1).(q-1) as per our prime numbers but with large prime numbers taking LCM would be much better.

ϕ(n)= LCM(61-1).(53-1) = 780

Public Key

The public key, normally expressed as e, it is a prime number chosen in the range [1,ϕ(n)]. You may think that 1 is a little small, and yes, I agree, if any small number is chosen, it could lead to security flaws. So in practice, the public key is normally set at 65537. Note that because the public key is prime, it has a high chance of a gcd equal to 1 with ϕ(n). If this is not the case, then we must use another prime number.

An interesting observation: If in practice, the number above is set at 65537, then it is not picked at random; surely this is a problem? Actually, no, it isn’t. As the name implies, this key is public and therefore is shared with everyone. As long as the private key cannot be deduced from the public key, we are secure. The reason why the public key is not randomly chosen in practice is because it is desirable not to have a large number. This is because it is more efficient to encrypt with smaller numbers than larger numbers.

The public key is actually a key pair of the exponent e and the modulus n and is present as follows


Example: Choose any number 1 < e < 780 that is coprime to 780. Choosing a prime number for e leaves us only to check that e is not a divisor of 780.

Let e= 17

Then our public key pair is (17,3233)

Private Key

The public key has a gcd of 1 with ϕ(n), the multiplicative inverse of the public key with respect to ϕ(n) can be efficiently and quickly determined using the Extended Euclidean Algorithm. This multiplicative inverse is the private key(d). So , we have the following equation (one of the most important equations in RSA):


Just like the public key, the private key is also a key pair of the exponent d and modulus n:


One of the absolute fundamental security assumptions behind RSA is that given a public key, one cannot efficiently determine the private key.

Example: Compute d, the modular multiplicative inverse of e (mod λ(n)) :


d = 413

So the private key pair is (413, 3233)

In the next post, I will give a brief about RSA Function Evaluation and a big example to showcase how RSA works.