Number TheoryTopic #34 of 40

RSA Cryptography

Public-key cryptography, RSA algorithm, key generation, encryption, and decryption.

Public-Key Cryptography

Symmetric vs. Asymmetric

Symmetric (Private Key):

  • Same key for encryption and decryption
  • Key must be shared secretly
  • Fast but key distribution is hard

Asymmetric (Public Key):

  • Different keys: public (encrypt) and private (decrypt)
  • Public key can be shared openly
  • RSA is the most famous example

RSA Algorithm

Key Generation

  1. Choose two large distinct primes pp and qq
  2. Compute n=pqn = pq (the modulus)
  3. Compute ϕ(n)=(p1)(q1)\phi(n) = (p-1)(q-1)
  4. Choose ee with 1<e<ϕ(n)1 < e < \phi(n) and gcd(e,ϕ(n))=1\gcd(e, \phi(n)) = 1
  5. Compute d=e1(modϕ(n))d = e^{-1} \pmod{\phi(n)}

Public key: (n,e)(n, e) Private key: (n,d)(n, d)

Encryption

To encrypt message MM (as integer, 0M<n0 \leq M < n): C=Me(modn)C = M^e \pmod{n}

Decryption

To decrypt ciphertext CC: M=Cd(modn)M = C^d \pmod{n}

Why It Works

Cd=(Me)d=Med(modn)C^d = (M^e)^d = M^{ed} \pmod{n}

Since ed1(modϕ(n))ed \equiv 1 \pmod{\phi(n)}: ed=1+kϕ(n)ed = 1 + k\phi(n)

By Euler's theorem (if gcd(M,n)=1\gcd(M, n) = 1): Med=MMkϕ(n)=M(Mϕ(n))kM1k=M(modn)M^{ed} = M \cdot M^{k\phi(n)} = M \cdot (M^{\phi(n)})^k \equiv M \cdot 1^k = M \pmod{n}

Example

Key Generation

  • p=61p = 61, q=53q = 53
  • n=61×53=3233n = 61 \times 53 = 3233
  • ϕ(n)=60×52=3120\phi(n) = 60 \times 52 = 3120
  • Choose e=17e = 17 (coprime to 3120)
  • d=171(mod3120)=2753d = 17^{-1} \pmod{3120} = 2753

Public key: (3233,17)(3233, 17) Private key: (3233,2753)(3233, 2753)

Encryption

Message: M=65M = 65 C=6517(mod3233)=2790C = 65^{17} \pmod{3233} = 2790

Decryption

M=27902753(mod3233)=65M = 2790^{2753} \pmod{3233} = 65

Security of RSA

Based on Factoring

Security relies on the difficulty of factoring n=pqn = pq.

If attacker can factor nn, they can compute ϕ(n)\phi(n) and find dd.

Key Size

  • 512-bit: Insecure (can be factored)
  • 1024-bit: Weak, not recommended
  • 2048-bit: Currently standard
  • 4096-bit: High security

Attacks

Factoring attacks: Number Field Sieve, Quadratic Sieve

Side-channel attacks: Timing, power analysis

Small exponent attacks: If ee is too small and same message sent to multiple recipients

Digital Signatures

RSA can also create digital signatures.

Signing

Sign message MM with private key: S=Md(modn)S = M^d \pmod{n}

Verification

Verify signature with public key: M=Se(modn)M' = S^e \pmod{n}

If M=MM' = M, signature is valid.

Why It Works

Only the private key holder can create SS such that Se=MS^e = M.

Practical Considerations

Message Padding

Never encrypt raw messages. Use padding schemes like:

  • OAEP (Optimal Asymmetric Encryption Padding)
  • PKCS#1

Hybrid Encryption

RSA is slow for large data. In practice:

  1. Generate random symmetric key KK
  2. Encrypt data with KK (using AES, etc.)
  3. Encrypt KK with RSA

Fast Exponentiation

Use square-and-multiply algorithm for Me(modn)M^e \pmod{n}.

Chinese Remainder Theorem Speedup

For decryption, compute: Mp=Cdmod(p1)(modp)M_p = C^{d \mod (p-1)} \pmod{p} Mq=Cdmod(q1)(modq)M_q = C^{d \mod (q-1)} \pmod{q}

Then combine using CRT. About 4× faster.

Mathematical Prerequisites

Euler's Theorem

aϕ(n)1(modn)a^{\phi(n)} \equiv 1 \pmod{n} when gcd(a,n)=1\gcd(a, n) = 1

Extended Euclidean Algorithm

To find d=e1(modϕ(n))d = e^{-1} \pmod{\phi(n)}

Fast Modular Exponentiation

To compute Me(modn)M^e \pmod{n} efficiently

Comparison with Other Systems

SystemSecurity BasisKey Size (equivalent to 128-bit symmetric)
RSAFactoring3072 bits
Diffie-HellmanDiscrete log3072 bits
ECCElliptic curve discrete log256 bits

ECC provides same security with smaller keys.