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

OFDM Symbols and Subcarriers Explained

This article explains how OFDM (Orthogonal Frequency Division Multiplexing) symbols and subcarriers work. It covers modulation, mapping symbols to subcarriers, subcarrier frequency spacing, IFFT synthesis, cyclic prefix, and transmission. Step 1: Modulation First, modulate the input bitstream. For example, with 16-QAM , each group of 4 bits maps to one QAM symbol. Suppose we generate a sequence of QAM symbols: s0, s1, s2, s3, s4, s5, …, s63 Step 2: Mapping Symbols to Subcarriers Assume N sub = 8 subcarriers. Each OFDM symbol in the frequency domain contains 8 QAM symbols (one per subcarrier): Mapping (example) OFDM symbol 1 → s0, s1, s2, s3, s4, s5, s6, s7 OFDM symbol 2 → s8, s9, s10, s11, s12, s13, s14, s15 … OFDM sym...

LDPC Encoding and Decoding Techniques

Low Density Parity Check (LDPC) Guide Comprehensive analysis of linear error-correcting block codes, Tanner graphs, and 5G-NR implementations. ๐Ÿ“˜ Overview ๐Ÿงฎ Encoding ๐Ÿงฉ Decoding ๐Ÿ“š Resources Theory Encoding Tech Tanner Graph 5G Encoding Decoding 'LDPC' is the abbreviation for 'low density parity check'. LDPC code H matrix contains very few amount of 1's and mostly zeroes. LDPC codes are error correcting code. Using LDPC codes, channel capacities that are close to the theoretical Shannon limit can be achieved. Low density parity check (LDPC) codes are linear error-correcting block code suitable for error correction in a large block sizes transmi...

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...

Design of CMOS Flip-Flops (SR, D, JK)

Design of CMOS Flip-Flops (SR, D, JK) A flip-flop or latch is a circuit with two stable states, used to store state information. It is the basic storage element in sequential logic and a fundamental building block in digital electronics systems, including computers and communication devices. Flip-flops and latches act as data storage elements for states, pulse counting, and synchronization of variably-timed input signals to a reference clock. Flip-flops can be transparent/opaque (latches) or clocked (synchronous, edge-triggered). Latches are level-sensitive, while flip-flops are edge-sensitive. In sequential logic, the output depends on current inputs and previous states. Fig.1 shows a sequential circuit combining a combinational block and a memory element. ...

Flat vs Frequency Selective Online Simulator

Flat vs Frequency Selective Online Simulator Channel Type Without Fading Flat Fading Multipaths Nakagami m SNR(dB) Run Simulation Input Signal Signal After Fading Constellation Diagram BER vs SNR Explore Advanced Flat vs Frequency-Selective Fading Simulator Want to see these equations in action? Visualize it. Launch Simulator Tool Interactive Rayleigh Fading Simulator Want to see Rayleigh fading in action? Visualize it. Launch Simulator Tool Return to DSP Simulations Main Page →

Online Simulator for Frequency Modulatiuon and Demodulation

FM Modulation Simulator Frequency Modulation (FM) In Frequency Modulation, the frequency of the carrier signal varies in accordance with the message signal's amplitude. s FM (t) = A c cos(ฯ‰ c t + k f ∫m(t)dt) where ฯ‰ = 2ฯ€f & k f = Frequency Sensitivity Modulation index, ฮฒ = (k f * A m ) / f m Change the parameter values to see the effect. Message Freq (Hz) 1 Carrier Freq (Hz) Message Amplitude (Am) Kf (sensitivity): 50 Perform FM Demodulation ๐Ÿงช Experiment for Students: ...

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...