DEV Community

Shreehari Menon
Shreehari Menon

Posted on • Originally published at meetcyber.net on

RSA Explained: The Simple Math Behind Internet Security

From prime numbers to public keys - how RSA enables secure logins, encrypted communication, and digital signatures.

1. Introduction: The Billion-Dollar Math Problem

Imagine you are a modern software developer. You sit down at your laptop, open your terminal, and type a simple command: sshuser@server.com. Instantly, you are securely logged into a server located halfway across the world. No one can spy on your connection, and the server knows exactly who you are without you ever having to type a password over the internet.

How did this happen? It happened because of a mathematical breakthrough that occurred over forty years ago.

Previously, we had learned about Asymmetric Encryption - the magical “Public Mailbox” system where you use a Public Key to lock a message and a completely different Private Key to unlock it. It solved the ultimate problem of the internet: allowing two strangers to share a secret without ever meeting in person.

But how do you actually build a Public and Private key? How can two digital keys be mathematically linked so that one locks and the other unlocks, without the Public Key accidentally revealing the Private Key?

In 1977, three brilliant researchers — Ron R ivest, Adi S hamir, and Leonard A dleman — published the answer. They named it RSA.

Today, RSA remains one of the most widely used cryptographic algorithms on the planet.

In this article, we will ignore the complex academic equations and just look at the simple, beautiful intuition behind how RSA works.

2. The Core Concept: The Trapdoor Function

To build a Public/Private key system, cryptographers needed to invent a very specific type of mathematical algorithm called a Trapdoor Function.

A Trapdoor Function is a math problem that is incredibly easy to calculate in one direction, but virtually impossible to reverse - unless you possess a secret piece of information (the trapdoor).

Imagine I hand you a bucket of yellow paint and a bucket of blue paint. I ask you to mix them together to create a specific shade of green.

Forward Direction (Easy): You pour some amount of yellow paint and some amount of blue paint into a bucket and stir them together. In a few seconds, you have a particular shade of green.

Reverse Direction (Hard): Now imagine I hand you only the final bucket of green paint and ask you to determine exactly how much yellow paint and how much blue paint were used to create that precise shade.

Was it 50% yellow and 50% blue? Was it 60% yellow and 40% blue?

Or some other exact combination?

Determining the precise original proportions just by looking at the final color becomes extremely difficult.

But what if you had a special chemical (the trapdoor) that could instantly reveal the exact proportions of yellow and blue used to create the shade? That secret chemical would make the impossible task suddenly easy.

That is the essence of a Trapdoor Function. In the digital world, RSA uses numbers instead of paint. Specifically, it uses Prime Numbers.


A Trapdoor Function is easy to compute forward, but impossible to reverse without a secret key.

3. The Secret Ingredients: Prime Numbers and Multiplication

A prime number is a number that can only be divided cleanly by 1 and itself (like 2, 3, 5, 7, 11, 13…). Prime numbers are the fundamental “atoms” of mathematics.

The entire security of RSA relies on one incredibly simple mathematical fact: Multiplying two prime numbers together is very easy, but factoring the result back into the original primes is brutally difficult.

Let’s look at an example:

  • The Easy Way (Multiplication): If I ask your computer to multiply 7 x 13 , it instantly tells you the answer is 91.
  • The Hard Way (Factoring): If I give you the number 3127 and ask, “Which two prime numbers did I multiply together to get this?” you will have to guess and check for a while. (The answer is 53 x 59 ).

Now, imagine we don’t use small numbers like 53. Imagine we use prime numbers that are 300 digits long.

If you multiply two 300-digit prime numbers together, a standard laptop can calculate the 600-digit answer in a fraction of a millisecond.

But if you give that 600-digit answer to a room full of supercomputers and ask them to find the original two prime numbers, it would take them millions of years of continuous guessing.

4. How RSA Generates Keys (Step-by-Step)

Here is how a developer’s computer uses this prime number math to generate a Public and Private Key. (We will use small variables to keep it simple).

Step 1: Pick the Secret Ingredients

Your computer secretly chooses two massive prime numbers. We will call them P and Q. (These are the yellow and blue paint - these must be kept absolutely secret. They are the trapdoor!)

Step 2: Create the Public Canvas

Your computer multiplies them together: N = P x Q. The resulting massive number N , is called the Modulo. (This is the green paint, you publish N to the world. It becomes the core part of your Public Key.)

Step 3: Derive the Locking and Unlocking Keys

Using some clever math (specifically, Euler’s Totient function, which we will skip for simplicity), your computer uses the secret primes ( P and Q ) to generate two more numbers:

  1. e (The Public Exponent): This is used for encrypting.
  2. d (The Private Exponent): This is used for decrypting.

The Final Result:

Your Public Key is made of the numbers (N and e). You send this to anyone who wants to talk to you.

Your Private Key is the number (d). You keep this hidden on your computer.

Because a hacker only knows N (the 600-digit number), they cannot figure out P and Q. Because they don’t know P and Q , they cannot calculate d (your Private Key). The math is locked!


The massive public number ’N’ is derived from ‘P’ and ‘Q’. But without knowing ‘P’ and ‘Q’, a hacker cannot build the Private Key.

5. Clock Math: How the Locking Actually Works

So, Bob has Alice’s Public Key ( N and e ). He wants to send her the secret number “ 7 ”. How does he scramble it?

RSA uses something called Modular Arithmetic , which is essentially “Clock Math”. If it is 10:00 AM, and you add 4 hours, it is not 14:00 AM. The clock “wraps around” at 12, so the answer is 2:00 PM. In clock math, we say the “modulo” is 12.

In RSA, the “clock size” is that massive public number N.

To Encrypt (Bob): Bob takes his message, raises it to the power of the public key ( e ), and wraps it around the massive clock ( N ). The message spins around the clock millions of times and lands on a random-looking number. That random number is the Ciphertext.

To Decrypt (Alice): Because the message was spun around the clock using the Public Key, it is trapped. The only mathematical way to unwind it is to spin it forward a very specific, massive number of times until it perfectly resets to the original message. That exact number of spins is the Private Key ( d ).

Alice applies her Private Key, the math unwinds, and the original message pops out!

6. Real-World Applications of RSA

Where will you, as an engineer, actually encounter RSA today?

  1. SSH (Secure Shell): When developers connect to remote servers (like AWS or GitHub), they often generate an “RSA Keypair.” Your computer holds the Private Key, and you upload the Public Key to GitHub.
  2. Digital Signatures: RSA can be used in reverse. If you encrypt a file’s hash with your Private Key, anyone can use your Public Key to verify that you signed it.
  3. PGP (Pretty Good Privacy): Used for encrypting highly sensitive emails, often utilized by journalists and whistleblowers.

7. The Problem with RSA Today (Why it is Aging)

RSA is beautiful, brilliant, and has secured the internet for four decades. However, it has a growing problem: Speed and Size.

Remember, the entire security of RSA relies on the fact that standard computers cannot factor the giant number N back into P and Q. But computers are getting faster every single year.

In the 1990s, a 512-bit N was considered secure. By the 2000s, computers got faster, so we had to increase the key size to 1024 bits. Today, the standard is 2048 bits or even 4096 bits.

These keys are absolutely massive. Doing complex “clock math” with 4000-bit numbers requires a lot of CPU power. If every mobile phone had to do 4096-bit RSA math for every website it visited, battery life would drop. RSA is becoming too heavy for the modern, fast-paced mobile internet.

Furthermore, as we will discuss in upcoming articles, a Quantum Computer doesn’t need to guess. Through an algorithm called Shor’s Algorithm, a quantum computer can find the prime numbers P and Q almost instantly, completely destroying RSA.

Summary

  • RSA is an Asymmetric Encryption algorithm created in 1977.
  • It relies on a Trapdoor Function : easy to do one way, practically impossible to reverse without a secret.
  • The “Trapdoor” in RSA is Prime Number Factorization. Multiplying two giant primes ( P and Q ) is easy; figuring out what they were from the result ( N ) is nearly impossible for classical computers.
  • The Public Key ( N , e ) is used to spin a message around a mathematical clock.
  • The Private Key ( d ) is the exact mathematical step required to unwind the clock and reveal the message.
  • The Catch: As computers get faster, RSA keys must become larger, making the algorithm slower and heavier for modern devices.

What’s Next?

Engineers realized over a decade ago that RSA was getting too bulky. We needed an algorithm that provided the exact same Asymmetric “Public/Private Key” magic, but with much smaller, faster keys.

We found the answer by looking away from prime numbers and looking toward geometry.

In the next article we will explore the algorithm that currently secures your smartphone, modern web browsing, and cryptocurrencies like Bitcoin. We will see how drawing lines through curved graphs created a faster, leaner successor to RSA.


Top comments (0)