Skip to main content

RSA Algorithm Explained

 

The Mathematics Behind the RSA Algorithm

RSA is one of the classic public-key cryptographic algorithms. Its mathematics comes primarily from number theory, especially prime numbers, modular arithmetic, Euler's theorem, and modular inverses.

1. The Big Picture

RSA has two keys:

  • Public key — anyone can know it.
  • Private key — must be kept secret.
Message   →   Encryption using public key   →   Ciphertext

Ciphertext   →   Decryption using private key   →   Original Message

The fascinating part is that encryption and decryption are based on exponentiation modulo a large number.

Encryption: c = me mod n

Decryption: m = cd mod n

2. First: What Does "mod" Mean?

The word mod means "remainder after division."

Example 1

17 divided by 5 gives:

17 = 5 × 3 + 2

Therefore:

17 mod 5 = 2

Another example

25 mod 7 = 4

because:

25 = 7 × 3 + 4

Modular arithmetic is extremely important in RSA.

3. Why Prime Numbers?

RSA begins by selecting two large prime numbers:

p = prime number
q = prime number

For learning, we will use small numbers:

p = 61
q = 53

In real RSA, these numbers are enormously larger.

4. Calculate n

Multiply the two primes:

n = p × q

With our example:

n = 61 × 53
n = 3233

This number n becomes part of both the public and private keys.

Important: The security of RSA relies heavily on the difficulty of taking a huge number such as n and discovering the two prime factors p and q.

5. Euler's Totient Function

The next mathematical concept is Euler's totient function, written as:

φ(n)

It counts how many positive integers less than or equal to n are relatively prime to n.

A small example

Consider:

n = 8

Numbers from 1 through 8 that are relatively prime to 8 are:

1, 3, 5, 7

Therefore:

φ(8) = 4

For RSA

Because p and q are prime:

n = p × q

We can calculate:

φ(n) = (p - 1)(q - 1)

For our example:

φ(3233)
= (61 - 1)(53 - 1)
= 60 × 52
= 3120

6. Choosing the Public Exponent e

We now choose a number e.

It must satisfy:

1 < e < φ(n)

and:

gcd(e, φ(n)) = 1

In other words, e and φ(n) must have no common factor other than 1.

Our example

φ(n) = 3120
e = 17

Since:

gcd(17, 3120) = 1

17 is a valid choice.

Our public key is now:

Public Key = (e, n)
= (17, 3233)

7. What Does gcd Mean?

gcd means greatest common divisor.

Example

Consider:

gcd(12, 18)

Factors of 12:

1, 2, 3, 4, 6, 12

Factors of 18:

1, 2, 3, 6, 9, 18

The largest common factor is 6:

gcd(12, 18) = 6

But:

gcd(17, 3120) = 1

So 17 and 3120 are relatively prime.

8. Finding the Private Key d

Now we need a number d satisfying:

e × d ≡ 1 mod φ(n)

With our values:

17d ≡ 1 mod 3120

The solution is:

d = 2753

Let's verify it

17 × 2753 = 46801

Divide 46801 by 3120:

46801 = 3120 × 15 + 1

Therefore:

46801 mod 3120 = 1

So:

17 × 2753 ≡ 1 mod 3120

Therefore:

Private Key = (d, n)
= (2753, 3233)

9. How Do We Find d?

We use the Extended Euclidean Algorithm.

We want:

17d + 3120k = 1

The Extended Euclidean Algorithm finds integers d and k that satisfy this equation.

In real cryptographic software, this calculation is performed automatically.

Key idea: Finding d from e and φ(n) is easy when φ(n) is known. The challenge for an attacker is obtaining φ(n), because that requires factoring n.

10. RSA Encryption

Suppose our message is represented by:

m = 65

Encryption is:

c = me mod n

Substitute our values:

c = 6517 mod 3233

The result is:

c = 2790

So 65 has been transformed into 2790.

65
↓
Encryption using (17, 3233)
↓
2790

11. RSA Decryption

The recipient has the private key:

(2753, 3233)

Decryption is:

m = cd mod n

Therefore:

m = 27902753 mod 3233

The result is:

m = 65

The original message has been recovered.

12. Why Does RSA Actually Work?

This is the most important mathematical part.

We choose e and d such that:

ed ≡ 1 mod φ(n)

This means there is some integer k such that:

ed = 1 + kφ(n)

Now encryption followed by decryption gives:

(me)d = med

Substitute:

med = m1 + kφ(n)

Therefore:

m1 + kφ(n) = m × (mφ(n))k

Euler's theorem tells us:

mφ(n) ≡ 1 mod n

Therefore:

m × (1)k ≡ m mod n

Therefore: med ≡ m mod n

That's why decryption gives us the original message.

13. A Tiny RSA Example From Scratch

Let's use extremely small numbers so that we can see the entire process manually.

Step 1: Choose primes

p = 3
q = 11

Step 2: Calculate n

n = 3 × 11 = 33

Step 3: Calculate φ(n)

φ(n) = (3 - 1)(11 - 1)
= 2 × 10
= 20

Step 4: Choose e

Choose:

e = 3

Check:

gcd(3, 20) = 1

Good.

Step 5: Find d

We need:

3d ≡ 1 mod 20

Try d = 7:

3 × 7 = 21
21 mod 20 = 1

Therefore:

d = 7

Keys

Key Value
Public key (3, 33)
Private key (7, 33)

Step 6: Encrypt

Suppose:

m = 4

Encryption:

c = 43 mod 33
= 64 mod 33
= 31

So:

4 → 31

Step 7: Decrypt

m = 317 mod 33

The result is:

m = 4

So:

Original 4 → Encrypt → 31 → Decrypt → 4

14. Why Can't an Attacker Simply Calculate d?

This is the central security idea.

The attacker knows:

n
e

For our example:

n = 3233
e = 17

But to calculate d, we need:

φ(n)

And to calculate φ(n), we need:

p and q

In other words:

p, q
↓
n = p × q
↓
φ(n) = (p-1)(q-1)
↓
d

Going forward is easy.

Going backward requires factoring n.

15. A Simple Factoring Example

Suppose an attacker sees:

n = 3233

They could try:

3233 ÷ 2
3233 ÷ 3
3233 ÷ 5
...

Eventually they discover:

3233 = 61 × 53

Then they can calculate:

φ(n) = (61 - 1)(53 - 1)
= 3120

Then they can calculate d.

This is why our tiny example is NOT secure.

3233 is trivial to factor. Real RSA uses enormous primes, typically producing RSA moduli such as 2048 bits or larger.

16. The Role of Modular Exponentiation

You may wonder how a computer can calculate something like:

27902753 mod 3233

without generating a gigantic number containing thousands of digits.

It uses an efficient technique called modular exponentiation, commonly implemented using repeated squaring.

Example

Suppose we want:

313 mod 7

Since:

13 = 8 + 4 + 1

We calculate powers by repeatedly squaring:

31 mod 7 = 3
32 mod 7 = 2
34 mod 7 = 4
38 mod 7 = 2

Then:

313 = 38 × 34 × 3

Therefore:

2 × 4 × 3 mod 7
= 24 mod 7
= 3

So:

313 mod 7 = 3

17. Chinese Remainder Theorem

Real RSA implementations often use another important number theory concept: the Chinese Remainder Theorem (CRT).

Instead of doing one huge modular exponentiation modulo:

n = pq

computations can be performed separately modulo p and q and then recombined.

mod p      +      mod q

↓

CRT

↓

mod pq

This can make RSA decryption significantly faster.

18. What Mathematics Is Really Doing the Work?

Mathematical concept Purpose in RSA
Prime numbers Used to construct n
Multiplication n = p × q
Euler's totient Calculates φ(n)
GCD Checks whether e is valid
Extended Euclidean Algorithm Finds d
Modular exponentiation Encryption and decryption
Euler's theorem Explains why decryption reverses encryption
Integer factorization Provides the computational security assumption
Chinese Remainder Theorem Can speed up private-key operations

19. The Entire RSA Algorithm in One Place

Key Generation

  1. Choose two large primes p and q.
  2. Calculate:
    n = pq
  3. Calculate:
    φ(n) = (p-1)(q-1)
  4. Choose e such that:
    gcd(e, φ(n)) = 1
  5. Calculate d such that:
    ed ≡ 1 mod φ(n)

Public Key

(e, n)

Private Key

(d, n)

Encryption

c = me mod n

Decryption

m = cd mod n

RSA turns number theory into a one-way computational problem.



Contact Us

Name

Email *

Message *

Popular Posts

UGC NET Electronic Science Previous Year Question Papers with Solutions

Download Papers and Solutions Exam Pattern Preparation Tips FAQs More Home / Engineering & Other Exams / UGC NET 2026 PYQ 📊 Exam Highlights: Electronic Science (88) Feature Details Junior Research Fellowship (JRF) ₹37,000 + HRA per month Eligibility M.Sc/M.Tech in Electronics (55%) Validity of Certificate JRF (3 Years) | Lectureship (Lifetime) 📥 Download UGC NET Electronics PDFs Complete collection of previous year question papers, answer keys and explanations for Subject Code 88. Start Downloading 📂 View All Question Papers June 2026 - Question Paper Download PDF June 202...

Online Simulator for ASK, FSK, and PSK Signal Generation

Interactive Digital Signal Processing (DSP) Tutorial and Simulator for ASK, FSK, and BPSK modulation techniques. Try our new Digital Signal Processing Simulator!   •   Interactive ASK, FSK, and BPSK tools updated for 2025. Start Now Digital Modulation Visualizer: ASK, FSK, & BPSK Simulator Learn and visualize binary modulation techniques (ASK, FSK, BPSK) in real-time with adjustable carrier and sampling parameters. Perfect for DSP students and engineers. 📡 ASK Simulator 📶 FSK Simulator 🎚️ BPSK Simulator 📚 More Topics ASK Modulator FSK Modulator BPSK Modulator Demodulation More Topics 1. ASK (Ampli...

UGC NET Electronic Science June 2026 Question Paper with Answer Key & Detailed Solutions

Home / UGC NET PYQ / June 2026 Solved UGC NET Electronic Science June 2026 Question Paper with Answer Key and Full Explanations 📥 Download Question Paper (PDF) 2025 2024 2023 2022 2021 2020 Explanations 1. Answer: Option (3) Total Students = 900 Urban Students = 7/10*400 + 4/5*500 = 680 680/900 * 100 = 75.56% 2. Answer: Option (1) Urban Students = 7/10*400 + 4/5*500 + 4/7*700 + 1/2 * 600 = 1380 3. Answer: Option (3) Male = 700*4/7 = 400 Female = 700*3/7=300 400-300=100 100/400*100 = 25% 4. Answer: Option (2) 400*3/10*40/100 + 500*1/5*60/100+700*3/7*50/100+600*1/3*30/100 = 318 5. Answer: Option (2) Total Female Students in B,C, and D colleges = 600 Female Post-graduates  = 100*60/100 + 300*50/100 + 200*30/100 = 270 270/600 *100 = 45% 6. Answer: Option (3) 7. Answer: Option (2) 8. Answer: Option (4) 9. Answer: Option (4) 10. Answer: Option (2) 11. Answer: Option (2) 12. Answer...

Constellation Diagrams of ASK, PSK, and FSK (with MATLAB Code + Simulator)

Constellation Diagrams: ASK, FSK, and PSK Comprehensive guide to signal space representation, including interactive simulators and MATLAB implementations. 📘 Overview 🧮 Simulator ⚖️ Theory 📈 Q-function 📚 Resources BASK Modulation Transmits one of two signals: 0 or $\sqrt{E_b}$, representing binary 0 and 1. Simple but sensitive to noise. BFSK Modulation Transmits one of two signals: $\sqrt{E_b}$ on the Y-axis or $\sqrt{E_b}$ on the X-axis. These are orthogonal signals. BPSK Modulation Transmits $+\sqrt{E_b}$ or $-\sqrt{E_b}$ (antipodal signaling). Most efficient binary scheme. ...

Vestigial Sideband Modulation (VSB)

  Whenever you pass a signal through a low-pass filter, it does not cut the signal exactly at the transition edge. A real filter does not have an ideal stopband, so some unwanted frequency components always remain. The same principle applies to VSB (Vestigial Sideband) modulation. VSB is generated from a Double-Sideband Suppressed-Carrier (DSB-SC) signal. In DSB-SC, both the upper sideband (USB) and lower sideband (LSB) are present. If we pass a DSB-SC signal through a low-pass filter in an attempt to extract only one sideband (like SSB), the filter cannot perfectly remove the unwanted sideband because practical filters have gradual roll-off. Therefore, instead of extracting exactly one complete sideband, we keep one full sideband and allow a small portion of the other sideband to pass. This remaining portion is called the vestigial sideband . Because of this added “vestige,” the bandwidth of VSB is slightly larger than the bandwidth of SSB-SC.     Vestigia...

Design of CMOS XOR/XNOR Gates

Design of CMOS XOR/XNOR Gates The semiconductor industry has experienced rapid integration of multimedia applications into mobile electronics, leading to very high integration density in CMOS VLSI. As operating frequencies increase, power consumption, speed, silicon area, and reliability become critical considerations. The XOR-XNOR circuits are fundamental building blocks in arithmetic circuits (Full Adders, Multipliers), compressors, comparators, parity checkers, code converters, error-detecting/correcting codes, and phase detectors. Their performance directly impacts the complex circuits they are used in. Design goals include full output voltage swing, low power consumption, reduced transistor count, minimal delay, and simultaneous non-skewed outputs. Static Logic (Static CMOS) Stat...

MUSIC Algorithm Explained (with MATLAB + Simulator)

Practical Implementation of the MUSIC Algorithm The focus is on how the algorithm works computationally , not just theory, and it explains the denominator (a H E n E n H a) mathematically and intuitively. 1. Introduction The MUSIC (Multiple Signal Classification) algorithm is a high-resolution method used in signal processing and array processing to estimate the Direction of Arrival (DOA) of signals received by a sensor array. Unlike classical beamforming methods, MUSIC uses eigenvector decomposition of the covariance matrix to separate the signal subspace and noise subspace , allowing it to achieve much higher angular resolution. In practical implementations, MUSIC works by: Simulating or collecting array signals Computing the covariance matrix Performing eigenvalue decomposition Separating signal and noise subspaces Scanning possible angles using a steering vector Constructing a pseudo-spectrum where peaks indicate signal directions 2. Signal Mo...