RSA Algorithm

Introduction

The RSA cryptosystem, named after its inventors Rivest, Shamir, and Adleman, is one of the first practical public-key encryption systems. Its security rests on the computational difficulty of factoring large integers—a problem that has resisted efficient solution for centuries.

In RSA, the encryption key can be made public without compromising security, because decryption requires knowledge of the prime factorization of a large number. This asymmetry between easy multiplication and hard factorization is the mathematical foundation of the system.

Understanding RSA requires familiarity with modular arithmetic, Euler's theorem, and the Chinese Remainder Theorem. These number-theoretic tools combine elegantly to create a practical and secure encryption method.

Mathematical Prerequisites

Euler's Totient Function

Euler's totient function ϕ⁡(n) phi of n counts the integers from 1 1 to n n that are coprime to n n For a prime p p

ϕ⁡(p)=p−1 phi of p equals p minus 1

For the product of two distinct primes n=p⁢q n equals p q

ϕ⁢(p⁢q)=ϕ⁡(p)⁢ϕ⁡(q)=(p−1)⁢(q−1) phi times p q equals phi of p phi of q equals open paren p minus 1 close paren times open paren q minus 1 close paren

Euler's Theorem

Euler's theorem generalizes Fermat's Little Theorem. If gcd⁡(a,n)=1 the gcd of open paren a comma n close paren equals 1 then:

aϕ⁡(n)≡1⁢(mod⁡(n)) a raised to the phi of n power is identical to 1 times mod n

This theorem is the key to RSA: it allows us to compute inverses of exponents modulo ϕ⁡(n) phi of n

Key Generation

RSA key generation proceeds as follows:

Choose two large distinct prime numbers p p and q q In practice, these are typically 1024 1024 bits or larger each.

Compute the modulus n=p⁢q n equals p q This will be part of both public and private keys.

Compute Euler's totient: ϕ⁡(n)=(p−1)⁢(q−1) phi of n equals open paren p minus 1 close paren times open paren q minus 1 close paren

Choose a public exponent e e such that 1<e<ϕ⁡(n) 1 is less than e is less than phi of n and gcd⁡(e,ϕ⁡(n))=1. the gcd of open paren e comma phi of n close paren equals 1 period Common choices are e=65537=216+1 e equals 65537 equals 2 to the 16th power plus 1 or e=3 e equals 3.

Compute the private exponent d d as the modular inverse of e e modulo ϕ⁡(n) phi of n

e⁢d≡1⁢(mod⁡(ϕ⁡(n))) e d is identical to 1 times mod phi of n

The public key is (n,e) open paren n comma e close paren and the private key is (n,d) open paren n comma d close paren The primes p p and q q must be kept secret and can be discarded after computing d d.

Encryption and Decryption

Encryption

To encrypt a message m m (represented as an integer with 0≤m<n 0 is less than or equal to m is less than n ), compute the ciphertext:

c=me⁢mod⁡(n) c equals m to the e-th power mod n

This operation uses only the public key. Anyone can encrypt messages to the key owner.

Decryption

To decrypt the ciphertext c c compute:

m=cd⁢mod⁡(n) m equals c to the d-th power mod n

This requires the private key. Only the key owner can decrypt.

Correctness Proof

We must verify that decryption recovers the original message: cd≡m⁢(mod⁡(n)) c to the d-th power is identical to m times mod n

Substituting c=me c equals m to the e-th power

cd=(me)d=me⁢d c to the d-th power equals m to the e-th power to the d-th power equals m raised to the e d power

Since e⁢d≡1⁢(mod⁡(ϕ⁡(n))) e d is identical to 1 times mod phi of n we can write e⁢d=1+k⁢ϕ⁡(n) e d equals 1 plus k phi of n for some integer k k Thus:

me⁢d=m1+k⁢ϕ⁡(n)=m⋅(mϕ⁡(n))k m raised to the e d power equals m raised to the 1 plus k phi of n power equals m times m raised to the phi of n power to the k-th power

If gcd⁡(m,n)=1 the gcd of open paren m comma n close paren equals 1 Euler's theorem gives mϕ⁡(n)≡1⁢(mod⁡(n)) m raised to the phi of n power is identical to 1 times mod n so:

me⁢d≡m⋅1k≡m⁢(mod⁡(n)) m raised to the e d power is identical to m times 1 to the k-th power is identical to m times mod n

The case when gcd⁡(m,n)≠1 the gcd of open paren m comma n close paren is not equal to 1 (i.e., m m is divisible by p p or q q can be handled separately using the Chinese Remainder Theorem, confirming that decryption works for all valid messages.

Security of RSA

The security of RSA relies on the difficulty of the integer factorization problem. Given n=p⁢q n equals p q finding p p and q q is believed to be computationally infeasible for sufficiently large primes.

If an attacker could factor n n they could compute ϕ⁡(n) phi of n and then find d d from e e The best known classical algorithms for factoring have sub-exponential but super-polynomial complexity.

The number field sieve, the fastest known classical algorithm, factors an n nbit integer in approximately:

O⁢(exp⁡(c⋅n1/3⁢(ln⁡n)2/3)) O times open paren the exp of open paren c times n raised to the 1 divided by 3 power times open paren l n n close paren raised to the 2 divided by 3 power close paren close paren

For 2048-bit keys, this is astronomically large. However, Shor's quantum algorithm can factor in polynomial time, making RSA vulnerable to future quantum computers.

Efficient Computation

Modular Exponentiation

Computing me⁢mod⁡(n) m to the e-th power mod n directly is infeasible for large exponents. Instead, we use repeated squaring (binary exponentiation). Express e e in binary and compute successive squares modulo n n.

This reduces the number of multiplications from e e to O⁢(log⁡e) O times log e making encryption and decryption practical.

Chinese Remainder Theorem Optimization

Decryption can be accelerated using the Chinese Remainder Theorem. Since n=p⁢q n equals p q we compute:

mp=cd⁢mod⁡(p−1)⁢mod⁡(p),mq=cd⁢mod⁡(q−1)⁢mod⁡(q) m sub p equals c raised to the d mod of open paren p minus 1 close paren power mod p comma m sub q equals c raised to the d mod of open paren q minus 1 close paren power mod q

Then combine using CRT. Since computations modulo p p and q q involve numbers half the size, this is about 4 4 times faster than direct computation modulo n n.

Digital Signatures

RSA can also create digital signatures. To sign a message m m the signer computes:

s=md⁢mod⁡(n) s equals m to the d-th power mod n

Anyone can verify the signature using the public key:

m=se⁢mod⁡(n) m equals s to the e-th power mod n

Only the private key holder can produce valid signatures, but anyone can verify them. In practice, we sign a hash of the message rather than the message itself.

Summary

RSA is a public-key cryptosystem based on the difficulty of factoring large integers. Key generation involves choosing primes p,q p comma q computing n=p⁢q n equals p q and ϕ⁡(n)=(p−1)⁢(q−1) phi of n equals open paren p minus 1 close paren times open paren q minus 1 close paren then finding e e and d d with e⁢d≡1⁢(mod⁡(ϕ⁡(n))) e d is identical to 1 times mod phi of n

Encryption computes c=me⁢mod⁡(n) c equals m to the e-th power mod n decryption computes m=cd⁢mod⁡(n) m equals c to the d-th power mod n Euler's theorem guarantees correctness. RSA also enables digital signatures by reversing the roles of e e and d d.