DEV Community

Ray Jones
Ray Jones

Posted on

How RSA Works (for Dummies)


RSA stands for Rivest Shamir Adleman, and it was invented by:

  1. Ron Rivest
  2. Adi Shamir
  3. Leonard Adleman

RSA comes from the first letters of their last names.


There are 2 types of encryption algorithms:

  1. Symmetric
  2. Asymmetric

Symmetric encryption algorithms use one key to encrypt and decrypt.

Asymmetric encryption algorithms use two keys — a public key and a private key.

RSA works by creating a pair of “commutative keys” — meaning if you encrypt with one, you can decrypt with the other.

In RSA, if you use the public key to encrypt, you must use the private key to decrypt (and vice versa).


RSA Math

Let's look at the mathematics behind RSA.

Here are the basic math concepts you need to know first:


0. Basic Math Operations

You should know how addition (+), subtraction (-), multiplication (×), and division (÷) work.


1. Factor

A factor — numbers you multiply to get the original number.

If N is the original number, and multiplying a and b gives you N, then a and b are factors of N.

Example:

N = 12
1 * 12 = 12
2 * 6  = 12
3 * 4  = 12
Enter fullscreen mode Exit fullscreen mode

Factors of 12: 1, 2, 3, 4, 6, 12

Now for:

N = 7
1 * 7 = 7
Enter fullscreen mode Exit fullscreen mode

Factors of 7: 1, 7


2. Prime

A prime number is a number that has only two factors: 1 and itself.

So from the example above, 7 is prime because its only factors are 1 and 7.

Other prime numbers: 2, 3, 5, 11, 13, 29, 37, 61, ...


3. Semi-Prime

A semi-prime number is a number whose factors are both prime numbers.

Example:

21 = 3 * 7
Enter fullscreen mode Exit fullscreen mode

Here 3 and 7 are both prime numbers.

So 21 is a semi-prime.

Note:
We exclude 1 and the number itself since they are common factors in every number.


4. Modulo

Modulo just means the remainder after division.

The symbol for modulo is % (or sometimes MOD).

Example:

7 % 3 = 1   → because 3*2=6 and 7-6=1
Enter fullscreen mode Exit fullscreen mode

So 1 is the remainder (modulo).

You can think of it as: what’s left over after dividing.


So that’s the 5 basic math concepts you need.

But as we go along, I’ll introduce another one that combines them.


RSA Algorithm

There are 2 main steps in RSA:

  1. Generating keys

  2. Using them for encryption and decryption


1. Generating Keys

Step a: Select 2 prime numbers (p, q)

Let p = 7 and q = 19.

Step b: Calculate their product

Let this be N.

N = p * q
N = 7 * 19 = 133
Enter fullscreen mode Exit fullscreen mode

Step c: Calculate the Totient (T), also called Euler’s Totient Function.

Euler’s Totient function tells us how many numbers less than N share no common factors with N.

For RSA, since p and q are prime, φ(N) = (p − 1)(q − 1).

This counts how many numbers less than N are coprime (relatively prime) with N — meaning their GCD is 1.

Example:
Let N be 13.

How many numbers less than 13 will produce a GCD of 1 with 13.
Answer is 12

For RSA, you can simply compute:

T = (p - 1) * (q - 1)
T = (7 - 1) * (19 - 1)
T = 6 * 18 = 108
Enter fullscreen mode Exit fullscreen mode

Step d: Choose a Public Key (E)

This value must satisfy:

  1. E must be coprime with T (i.e., gcd(E, T) = 1)

  2. E must be less than T
    Let E = 29.

  • 29 is less than 108
  • gcd(29, 108) = 1 So:
E = 29
Enter fullscreen mode Exit fullscreen mode

Step e: Find the Private Key (D)

D must satisfy the condition:

(D * E) mod T = 1
Enter fullscreen mode Exit fullscreen mode

This means D is the modular multiplicative inverse of E modulo T.

Let D be 41

(41 * 29) mod 108 = 1

(41 * 29) = 1189

1189 % 108 = 11 remainder 1

108 * 11 = 1188

1189 - 1188 = 1
Enter fullscreen mode Exit fullscreen mode

So:

D = 41
Enter fullscreen mode Exit fullscreen mode

2. Encryption and Decryption

Now that we have:

N = 133
E = 29
D = 41
Enter fullscreen mode Exit fullscreen mode

We can encrypt and decrypt messages (which are represented as numbers).

Encryption (using Public Key)

Cipher = Message ^ E mod N
Enter fullscreen mode Exit fullscreen mode

Example:

Message = 60
Cipher  = 60^29 mod 133 = 72
Enter fullscreen mode Exit fullscreen mode

Decryption (using Private Key)

Message = Cipher ^ D mod N
Enter fullscreen mode Exit fullscreen mode

Example:

Message = 72^41 mod 133 = 60 (the original)
Enter fullscreen mode Exit fullscreen mode

So we got back our message — RSA works!


Note

RSA only works with numbers.

So if you have a message like "Hello, World!", it’s first converted to numbers using ASCII or UTF-8 before applying RSA.

After decryption, the number is converted back to text.


How Secure Is RSA?

RSA’s security relies on the difficulty of factoring semi-prime numbers.
RSA security depends on how hard it is to factor n = p × q, the larger n (in bits), the harder the math problem.

If you’re given 133, could you easily find 7 and 19? Sure.

But what about a 2048-bit number — something with hundreds of digits?

That’s practically impossible with current computers.

A 2048-bit RSA key means the digit can be as large as (2^2048)-1
Which is roughly a 617-digit decimal number

Here is a table of different key lengths and time it would take to crack them

RSA Key Size Approximate Decimal Digits Factoring Difficulty Approximate Time to Break (with modern computers)
256-bit ~77 digits Very easy Few seconds
512-bit ~155 digits Still easy Minutes to hours
768-bit ~232 digits Hard, but done in labs Months (if you use supercomputers)
1024-bit ~309 digits Very hard Thousands of years
2048-bit ~617 digits Practically impossible Longer than universe’s age
4096-bit ~1234 digits Even harder Astronomically impossible

As you can see it gets harder every time the number gets larger. If you read my blog on how software works-encryption I talked about the goal of modern encryption was to make encryption computationally expensive - so expensive that its useless to even try.


Summary

Public Key  = (E, N)
Private Key = (D, N)

Encrypt:   Cipher = Message ^ E mod N
Decrypt:   Message = Cipher ^ D mod N
Enter fullscreen mode Exit fullscreen mode

Top comments (0)