Skip to main content

9.3 Asymmetric Key Cryptography: Diffie-Hellman & RSA Algorithm

๐Ÿ“šModule 09: Network Security & CryptographyTopic 9.3โฑ๏ธ16 min read
๐ŸŽฏHigh-Yield For:Semester Exams (All Universities) โ€ข Placement Technical Rounds โ€ข Engineering Foundations

๐Ÿ’ก Core Intuition: The Open Padlock and the Private Keyโ€‹

๐Ÿณ The Everyday Analogy: Padlocks vs Master Keysโ€‹

Imagine Alice wants anyone in the world to be able to send her confidential letters, but she has never met her correspondents:

  1. The Public Key (The Open Padlock): Alice purchases 10,000 identical open padlocks. She places them in an open crate on her front porch. Anyone can take a padlock, place a confidential message in a metal box, snap Alice's padlock shut, and ship the box to her.
  2. The Private Key (The Secret Physical Key): Snapping the padlock shut requires no key. But once clicked shut, only Alice holds the unique physical key that can unlock it. Even the person who snapped the lock shut cannot reopen the box!
  3. The Key Scaling Advantage: In a symmetric system of 1,000,0001,000,000 users, every pair needs a unique secret key, requiring nearly 500ย billion500\text{ billion} shared keys. In an asymmetric system, each user requires only one key pair, requiring just 2,000,0002,000,000 total keys.

๐Ÿ’ป Bridging to Computer Scienceโ€‹

In network security:

  • Asymmetric Key Cryptography (Public Key Cryptography) uses mathematically linked key pairs:
    • Public Key (PKPK): Distributed openly to the entire world. Used by senders to encrypt data or verify signatures.
    • Private Key (SKSK): Kept strictly secret by the owner. Used to decrypt ciphertext or generate digital signatures.
  • Asymmetric algorithms rely on one-way trapdoor functions: mathematical operations that are trivial to compute in the forward direction, but computationally impossible to reverse unless you possess a secret piece of information (the "trapdoor").


๐Ÿ›๏ธ Symmetric vs Asymmetric Cryptographyโ€‹

Symmetric Key vs Asymmetric Key Cryptography

The fundamental architectural trade-off between algorithmic throughput and key distribution scalability

High Throughput

Symmetric Cryptography (Shared Key)

โšก
Dominant Architecture / DomainAES-128/256, ChaCha20
  • โ€ขSingle shared secret key K used for both encryption and decryption.
  • โ€ขKey scaling problem: N users require N(N - 1) / 2 shared keys.
  • โ€ขBlazing fast: 1,000x to 10,000x faster than asymmetric ciphers.
  • โ€ขCannot provide non-repudiation because both parties share the exact same key.
"The heavy workhorse for encrypting bulk data payloads."
Scalable Trust

Asymmetric Cryptography (Key Pair)

๐Ÿ”
Dominant Architecture / DomainRSA, Diffie-Hellman, ECDSA, Ed25519
  • โ€ขPublic Key (PK) encrypts; Private Key (SK) decrypts.
  • โ€ขScales linearly: N users require exactly 2N keys.
  • โ€ขComputationally heavy: based on modular exponentiation and elliptic curves.
  • โ€ขProvides non-repudiation and digital identity verification.
"The cryptographic handshake mechanism used to exchange symmetric session keys."

๐Ÿค Diffie-Hellman Key Exchange (1976)โ€‹

Published by Whitfield Diffie and Martin Hellman in 1976, Diffie-Hellman (DH) solved the greatest paradox in security history: How can two parties establish a shared secret key over an insecure, publicly monitored channel without transmitting the secret itself?

The Mathematical Foundation: Discrete Logarithm Problem (DLP)โ€‹

Diffie-Hellman relies on modular arithmetic over a large prime pp with primitive root gg:

  • Computing Y=gXโ€Šmodโ€ŠpY = g^X \bmod p is computationally easy (via repeated squaring).
  • Given Y,g,Y, g, and pp, finding X=logโกg(Y)โ€Šmodโ€ŠpX = \log_g(Y) \bmod p is the Discrete Logarithm Problem, which is computationally intractable for large primes (pโ‰ฅ2048ย bitsp \ge 2048\text{ bits}).

The 4-Step Diffie-Hellman Protocolโ€‹

Space-Time Protocol Ladder
Step 1 of 4

The Diffie-Hellman Key Exchange Protocol

Space-time protocol exchange establishing a shared symmetric key over an insecure channel

๐Ÿ’กClick or hover any packet flight arrow, timer event, or step button to inspect protocol mechanics in real time
TIME (t)+โˆž
๐Ÿ‘ฉEndpoint A
Alice
Lifeline Rail (t โ†“)
๐Ÿ‘จEndpoint B
Bob
Lifeline Rail (t โ†“)
11. Public Domain Parameters(Agreed public prime p, primitive root g)
22. Alice Sends Public A(A = g^a mod p)
33. Bob Sends Public B(B = g^b mod p)
44. Shared Secret Established(K = (B)^a mod p = (A)^b mod p = g^(ab) mod p)
Step 1 of 4alice โž” bobโœ… Delivered Successfully1. Public Domain Parameters

Alice and Bob publicly agree on a large prime p and primitive root generator g.

Step-by-Step Mathematical Walkthroughโ€‹

  1. Public Agreement: Alice and Bob agree on public parameters: a prime pp and primitive root gg.
  2. Alice's Key Pair: Alice picks a secret random integer aa (1<a<p1 < a < p) and computes: A=gaโ€Šmodโ€ŠpA = g^a \bmod p Alice sends AA publicly to Bob.
  3. Bob's Key Pair: Bob picks a secret random integer bb (1<b<p1 < b < p) and computes: B=gbโ€Šmodโ€ŠpB = g^b \bmod p Bob sends BB publicly to Alice.
  4. Shared Secret Derivation:
    • Alice takes Bob's public value BB and raises it to her private key aa: K=Baโ€Šmodโ€Šp=(gb)aโ€Šmodโ€Šp=gabโ€Šmodโ€ŠpK = B^a \bmod p = (g^b)^a \bmod p = \mathbf{g^{ab} \bmod p}
    • Bob takes Alice's public value AA and raises it to his private key bb: K=Abโ€Šmodโ€Šp=(ga)bโ€Šmodโ€Šp=gabโ€Šmodโ€ŠpK = A^b \bmod p = (g^a)^b \bmod p = \mathbf{g^{ab} \bmod p}

An eavesdropper (Eve) watching the wire sees only p,g,A,p, g, A, and BB. To compute K=gabโ€Šmodโ€ŠpK = g^{ab} \bmod p, Eve must solve the Discrete Logarithm Problem to find aa or bb, which is mathematically intractable!


The Man-in-the-Middle (MitM) Flaw of Unauthenticated DHโ€‹

Diffie-Hellman provides forward secrecy and secrecy, but zero authentication:

  1. An active attacker (Eve) sits between Alice and Bob.
  2. When Alice sends AA, Eve intercepts it and sends her own public key E1E_1 to Bob.
  3. When Bob sends BB, Eve intercepts it and sends her own public key E2E_2 to Alice.
  4. Alice establishes key K1=gaโ‹…e2K_1 = g^{a \cdot e_2} with Eve. Bob establishes key K2=gbโ‹…e1K_2 = g^{b \cdot e_1} with Eve.
  5. Eve transparently decrypts, reads, alters, and re-encrypts every message between Alice and Bob!
  • Remedy: Authenticated Diffie-Hellman (e.g. Ephemeral Elliptic Curve Diffie-Hellman - ECDHE signed with an RSA or ECDSA digital certificate in TLS).

๐Ÿ” The RSA Algorithm (Rivest, Shamir, Adleman - 1977)โ€‹

RSA was the first practical public-key cryptosystem capable of both encryption and digital signatures.

The Mathematical Foundation: Euler's Totient Theoremโ€‹

Let n=pโ‹…qn = p \cdot q be the product of two large prime numbers. Euler's Totient Function ฯ•(n)\phi(n) counts integers up to nn relatively prime to nn:

ฯ•(n)=(pโˆ’1)(qโˆ’1)\mathbf{\phi(n) = (p - 1)(q - 1)}

Euler's Theorem states that for any integer MM relatively prime to nn:

Mฯ•(n)โ‰ก1(modn)M^{\phi(n)} \equiv 1 \pmod n

Therefore, for any integer kk:

Mkโ‹…ฯ•(n)+1โ‰กM(modn)\mathbf{M^{k \cdot \phi(n) + 1} \equiv M \pmod n}

If we choose exponents ee and dd such that eโ‹…dโ‰ก1(modฯ•(n))e \cdot d \equiv 1 \pmod{\phi(n)}, then:

(Me)dโ‰กMeโ‹…dโ‰กMkโ‹…ฯ•(n)+1โ‰กM(modn)\big(M^e\big)^d \equiv M^{e \cdot d} \equiv M^{k \cdot \phi(n) + 1} \equiv \mathbf{M \pmod n}


The 5-Step RSA Key Generation Algorithmโ€‹

Step NumberMathematical OperationRule / Formula
Step 1Choose two large distinct primespp and qq (typically 1024 or 2048 bits each).
Step 2Compute the modulusn=pโ‹…q\mathbf{n = p \cdot q}. (nn is made public).
Step 3Compute Euler's Totientฯ•(n)=(pโˆ’1)(qโˆ’1)\mathbf{\phi(n) = (p - 1)(q - 1)}. (Kept strictly secret).
Step 4Choose public exponent eeSelect ee such that 1<e<ฯ•(n)1 < e < \phi(n) and gcdโก(e,ฯ•(n))=1\mathbf{\gcd(e, \phi(n)) = 1}. (Industry standard: e=65537e = 65537).
Step 5Compute private exponent ddCompute modular multiplicative inverse using Extended Euclidean Algorithm: dโ‰กeโˆ’1(modฯ•(n))โ€…โ€ŠโŸนโ€…โ€Šeโ‹…dโ‰ก1(modฯ•(n))\mathbf{d \equiv e^{-1} \pmod{\phi(n)}} \implies \mathbf{e \cdot d \equiv 1 \pmod{\phi(n)}}.
  • Public Key: (e,โ€…โ€Šn)\mathbf{\big(e, \; n\big)} (Shared with everyone).
  • Private Key: (d,โ€…โ€Šn)\mathbf{\big(d, \; n\big)} (Kept secret; p,q,p, q, and ฯ•(n)\phi(n) are discarded or destroyed).

Encryption and Decryption Formulasโ€‹

Encryptionย (Sender):C=Meโ€Šmodโ€Šn(whereย 0โ‰คM<n)\text{Encryption (Sender)}: \quad \mathbf{C = M^e \bmod n} \quad (\text{where } 0 \le M < n)

Decryptionย (Receiver):M=Cdโ€Šmodโ€Šn\text{Decryption (Receiver)}: \quad \mathbf{M = C^d \bmod n}


๐Ÿงฎ Complete Numerical Walkthrough: RSA Step-by-Stepโ€‹

Let us execute the complete RSA algorithm using small prime numbers:

Step 1: Key Generationโ€‹

  1. Choose primes: p=7,q=11p = 7, \quad q = 11.
  2. Compute modulus: n=pโ‹…q=7ร—11=77n = p \cdot q = 7 \times 11 = \mathbf{77}
  3. Compute Totient: ฯ•(n)=(pโˆ’1)(qโˆ’1)=(7โˆ’1)(11โˆ’1)=6ร—10=60\phi(n) = (p - 1)(q - 1) = (7 - 1)(11 - 1) = 6 \times 10 = \mathbf{60}
  4. Choose public exponent ee:
    • Must satisfy gcdโก(e,60)=1\gcd(e, 60) = 1. Let us pick e=7e = \mathbf{7} (since gcdโก(7,60)=1\gcd(7, 60) = 1).
  5. Compute private exponent dd:
    • eโ‹…dโ‰ก1(modฯ•(n))โ€…โ€ŠโŸนโ€…โ€Š7dโ‰ก1(mod60)e \cdot d \equiv 1 \pmod{\phi(n)} \implies 7d \equiv 1 \pmod{60}.
    • We need an integer kk such that 7d=60k+17d = 60k + 1:
      • If k=1:61/7โ‰ˆ8.71k = 1: 61 / 7 \approx 8.71
      • If k=2:121/7โ‰ˆ17.28k = 2: 121 / 7 \approx 17.28
      • If k=3:181/7โ‰ˆ25.85k = 3: 181 / 7 \approx 25.85
      • If k=4:241/7โ‰ˆ34.42k = 4: 241 / 7 \approx 34.42
      • If k=5:301/7=43k = 5: 301 / 7 = \mathbf{43}!
    • Therefore, d=43d = \mathbf{43}.

Publicย Key:ย (e=7,โ€…โ€Šn=77)Privateย Key:ย (d=43,โ€…โ€Šn=77)\text{Public Key: } (e = 7, \; n = 77) \qquad \text{Private Key: } (d = 43, \; n = 77)


Step 2: Encryptionโ€‹

Alice wishes to send the plaintext message M=9M = \mathbf{9} to Bob using Bob's public key (e=7,n=77)(e=7, n=77):

C=Meโ€Šmodโ€Šn=97โ€Šmodโ€Š77C = M^e \bmod n = 9^7 \bmod 77

  • 92=81โ‰ก4(mod77)9^2 = 81 \equiv 4 \pmod{77}
  • 94=(92)2โ‰ก42=16(mod77)9^4 = (9^2)^2 \equiv 4^2 = 16 \pmod{77}
  • 97=94ร—92ร—91โ‰ก(16ร—4ร—9)=576(mod77)9^7 = 9^4 \times 9^2 \times 9^1 \equiv (16 \times 4 \times 9) = 576 \pmod{77}
  • 576=7ร—77+37โ€…โ€ŠโŸนโ€…โ€Š576โ‰ก37(mod77)576 = 7 \times 77 + 37 \implies 576 \equiv \mathbf{37} \pmod{77}

C=37\mathbf{C = 37}


Step 3: Decryptionโ€‹

Bob receives ciphertext C=37C = 37 and decrypts it using his private key (d=43,n=77)(d=43, n=77):

M=Cdโ€Šmodโ€Šn=3743โ€Šmodโ€Š77M = C^d \bmod n = 37^{43} \bmod 77

Using modular exponentiation:

  • 371โ‰ก37(mod77)37^1 \equiv 37 \pmod{77}
  • 372=1369=(17ร—77)+60โ‰กโˆ’17โ‰ก60(mod77)37^2 = 1369 = (17 \times 77) + 60 \equiv -17 \equiv 60 \pmod{77}
  • 374โ‰ก(โˆ’17)2=289=(3ร—77)+58โ‰กโˆ’19(mod77)37^4 \equiv (-17)^2 = 289 = (3 \times 77) + 58 \equiv -19 \pmod{77}
  • Repeating squaring across the binary decomposition of 43=32+8+2+143 = 32 + 8 + 2 + 1:
  • The final result evaluates to: M=9M = \mathbf{9}

The decrypted plaintext matches the original message identically!


Security Basis: The Integer Factorization Problemโ€‹

Why can an attacker not calculate dd from the public key (e,n)(e, n)?

  • To find dd, the attacker must compute dโ‰กeโˆ’1(modฯ•(n))d \equiv e^{-1} \pmod{\phi(n)}.
  • To compute ฯ•(n)\phi(n), the attacker must know pp and qq.
  • To find pp and qq, the attacker must factor nn.
  • For a 2048-bit2048\text{-bit} modulus nn, the best known classical factoring algorithm (the General Number Field Sieve) would require billions of years of supercomputer computation!

๐Ÿญ Real-World Production Context: Hybrid Cryptosystems, Forward Secrecy & Post-Quantum Cryptographyโ€‹

1. Hybrid Cryptography in Production (TLS & SSH)โ€‹

Why does HTTPS never encrypt full web pages directly using RSA?

  • Computational Overhead: Asymmetric modular exponentiation (Meโ€Šmodโ€ŠnM^e \bmod n) requires heavy multi-precision arithmetic. RSA is roughly 1,000ย timesย slower1,000\text{ times slower} than symmetric AES in software and 10,000ร—10,000\times slower than hardware AES-NI.
  • Message Size Limit: Standard RSA can only encrypt messages strictly smaller than its modulus size (M<nM < n). Encrypting a 50ย MB50\text{ MB} video would require thousands of individual RSA blocks.
  • The Production Solution (Hybrid Cryptography):
    1. The client and server execute an asymmetric handshake (ECDHE / RSA) to mutually authenticate and agree upon a single temporary, high-entropy 256-bit symmetric key.
    2. All bulk application data (HTML, video, images) is encrypted using high-speed symmetric AES-256-GCM or ChaCha20-Poly1305.

2. Perfect Forward Secrecy (PFS) via Ephemeral Diffie-Hellman (ECDHE)โ€‹

In legacy TLS 1.2, clients encrypted the symmetric pre-master secret using the server's static RSA public key.

  • The Retrospective Wiretap Threat: Intelligence agencies or attackers record and store millions of encrypted HTTPS sessions across fiber taps. If the server's private RSA key is compromised, leaked, or subpoenaed 5 years later, the adversary can decrypt every historical recording retroactively!
  • TLS 1.3 Mandate: TLS 1.3 completely banned static RSA key exchanges. It mandates Ephemeral Diffie-Hellman (ECDHE):
    • For every new connection, both endpoints generate fresh, temporary private keys (x,y)(x, y) that are erased from RAM immediately after the session key is derived.
    • Even if the server's long-term certificate private key is stolen later, historical recorded sessions remain mathematically unbreakable.

3. The Quantum Threat & NIST Post-Quantum Cryptography (PQC)โ€‹

In 1994, Peter Shor formulated Shor's Algorithm:

  • On a sufficiently large, fault-tolerant quantum computer, Shor's algorithm factors integers and calculates discrete logarithms in polynomial time O((logโกN)3)\mathcal{O}\big((\log N)^3\big), instantaneously breaking RSA, Diffie-Hellman, and Elliptic Curve Cryptography (ECDSA/ECDH)!
  • In 2024, NIST officially standardized the first Post-Quantum Cryptography (PQC) standards based on high-dimensional lattice mathematics:
    • ML-KEM (Kyber / FIPS 203): Key Encapsulation Mechanism replacing Diffie-Hellman/RSA key exchange.
    • ML-DSA (Dilithium / FIPS 204): Digital signature algorithm replacing RSA/ECDSA signatures.
  • Cloudflare, Google Chrome, and Apple iMessage already deploy hybrid Post-Quantum TLS (X25519 + Kyber768) to protect current communications against "Harvest Now, Decrypt Later" adversaries.

๐Ÿ“ Numerical Applications & Worked Examplesโ€‹

Example 1: Full RSA Cryptosystem Mathematical Traceโ€‹

Problem Statement: In an RSA cryptosystem, the prime numbers are chosen as p=3p = 3 and q=11q = 11. The public exponent is selected as e=7e = 7.

  1. Compute the modulus nn, totient ฯ•(n)\phi(n), and derive the private key exponent dd using the Extended Euclidean Algorithm.
  2. Encrypt the plaintext message M=5M = 5.
  3. Show the complete decryption calculation confirming recovery of the plaintext.

Step-by-Step Solution:

  1. Key Generation:
    • Modulus: n=pโ‹…q=3ร—11=33n = p \cdot q = 3 \times 11 = \mathbf{33}
    • Euler's Totient: ฯ•(n)=(pโˆ’1)(qโˆ’1)=(3โˆ’1)(11โˆ’1)=2ร—10=20\phi(n) = (p - 1)(q - 1) = (3 - 1)(11 - 1) = 2 \times 10 = \mathbf{20}
    • Public exponent e=7e = 7. Check gcdโก(7,20)=1\gcd(7, 20) = 1 (valid).
    • Derive private exponent dd: eโ‹…dโ‰ก1(modฯ•(n))โ€…โ€ŠโŸนโ€…โ€Š7dโ‰ก1(mod20)e \cdot d \equiv 1 \pmod{\phi(n)} \implies 7d \equiv 1 \pmod{20} 7d=20k+1โ€…โ€ŠโŸนโ€…โ€ŠForย k=1:7d=21โ€…โ€ŠโŸนโ€…โ€Šd=37d = 20k + 1 \implies \text{For } k = 1: 7d = 21 \implies d = \mathbf{3}
    • Public Key: (e=7,โ€…โ€Šn=33)(e = 7, \; n = 33)
    • Private Key: (d=3,โ€…โ€Šn=33)(d = 3, \; n = 33)
  2. Encryption (M=5M = 5): C=Meโ€Šmodโ€Šn=57โ€Šmodโ€Š33C = M^e \bmod n = 5^7 \bmod 33
    • Break down exponent via repeated squaring: 52=25โ‰กโˆ’8(mod33)5^2 = 25 \equiv -8 \pmod{33} 54=(โˆ’8)2=64=(2ร—33)โˆ’2โ‰กโˆ’2(mod33)5^4 = (-8)^2 = 64 = (2 \times 33) - 2 \equiv -2 \pmod{33} 57=54ร—52ร—51โ‰ก(โˆ’2)ร—(โˆ’8)ร—5=80(mod33)5^7 = 5^4 \times 5^2 \times 5^1 \equiv (-2) \times (-8) \times 5 = 80 \pmod{33} 80=(2ร—33)+14โ€…โ€ŠโŸนโ€…โ€ŠC=1480 = (2 \times 33) + 14 \implies \mathbf{C = 14}
  3. Decryption (C=14C = 14): M=Cdโ€Šmodโ€Šn=143โ€Šmodโ€Š33M = C^d \bmod n = 14^3 \bmod 33 142=196=(5ร—33)+31โ‰กโˆ’2(mod33)14^2 = 196 = (5 \times 33) + 31 \equiv -2 \pmod{33} 143=142ร—14โ‰ก(โˆ’2)ร—14=โˆ’28(mod33)14^3 = 14^2 \times 14 \equiv (-2) \times 14 = -28 \pmod{33} โˆ’28+33=5-28 + 33 = \mathbf{5} The decrypted plaintext M=5M = 5 matches the original message identically!

Example 2: Diffie-Hellman Key Exchange with Man-in-the-Middle Interceptionโ€‹

Problem Statement: Alice and Bob agree on public parameters p=23p = 23 (prime modulus) and g=5g = 5 (primitive root).

  • Alice picks private secret a=6a = 6.
  • Bob picks private secret b=15b = 15.
  1. Calculate Alice's public value AA and Bob's public value BB.
  2. Compute the shared secret key KK derived by Alice and Bob.
  3. Suppose an active adversary (Mallory) intercepts the transmission and substitutes her own private secret m=4m = 4. Trace how Mallory creates two independent shared keys.

Step-by-Step Solution:

  1. Public Values:
    • Alice computes: A=gaโ€Šmodโ€Šp=56โ€Šmodโ€Š23A = g^a \bmod p = 5^6 \bmod 23 52=25โ‰ก2(mod23)โ€…โ€ŠโŸนโ€…โ€Š56=(52)3โ‰ก23=8โ€…โ€ŠโŸนโ€…โ€ŠA=85^2 = 25 \equiv 2 \pmod{23} \implies 5^6 = (5^2)^3 \equiv 2^3 = \mathbf{8} \implies \mathbf{A = 8}
    • Bob computes: B=gbโ€Šmodโ€Šp=515โ€Šmodโ€Š23B = g^b \bmod p = 5^{15} \bmod 23 54โ‰ก22=4(mod23),58โ‰ก42=16โ‰กโˆ’7(mod23)5^4 \equiv 2^2 = 4 \pmod{23}, \quad 5^8 \equiv 4^2 = 16 \equiv -7 \pmod{23} 515=58ร—54ร—52ร—51โ‰ก(โˆ’7)ร—4ร—2ร—5=โˆ’280(mod23)5^{15} = 5^8 \times 5^4 \times 5^2 \times 5^1 \equiv (-7) \times 4 \times 2 \times 5 = -280 \pmod{23} โˆ’280=โˆ’13ร—23+19โ€…โ€ŠโŸนโ€…โ€ŠB=19-280 = -13 \times 23 + 19 \implies \mathbf{B = 19}
  2. Shared Secret Key Derivation:
    • Alice computes with Bob's public B=19B = 19: K=Baโ€Šmodโ€Šp=196โ€Šmodโ€Š23=(โˆ’4)6โ€Šmodโ€Š23=46โ‰ก(16)3โ‰ก(โˆ’7)3=โˆ’343(mod23)K = B^a \bmod p = 19^6 \bmod 23 = (-4)^6 \bmod 23 = 4^6 \equiv (16)^3 \equiv (-7)^3 = -343 \pmod{23} โˆ’343=โˆ’15ร—23+2โ€…โ€ŠโŸนโ€…โ€ŠK=2-343 = -15 \times 23 + 2 \implies \mathbf{K = 2}
    • Bob computes with Alice's public A=8A = 8: K=Abโ€Šmodโ€Šp=815โ€Šmodโ€Š23=(23)15=245โ€Šmodโ€Š23K = A^b \bmod p = 8^{15} \bmod 23 = (2^3)^{15} = 2^{45} \bmod 23 211=2048โ‰ก1(mod23)โ€…โ€ŠโŸนโ€…โ€Š245=(211)4ร—21โ‰ก1ร—2=2โ€…โ€ŠโŸนโ€…โ€ŠK=22^{11} = 2048 \equiv 1 \pmod{23} \implies 2^{45} = (2^{11})^4 \times 2^1 \equiv 1 \times 2 = \mathbf{2} \implies \mathbf{K = 2}
  3. Man-in-the-Middle Attack:
    • Mallory intercepts A=8A = 8 and B=19B = 19. She computes her public value: Mpub=54โ€Šmodโ€Š23=625โ‰ก4(mod23)M_{\text{pub}} = 5^4 \bmod 23 = 625 \equiv 4 \pmod{23}
    • Mallory sends Mpub=4M_{\text{pub}} = 4 to both Alice and Bob.
    • Key with Alice: KAlice-Mallory=(Mpub)aโ€Šmodโ€Š23=46โ€Šmodโ€Š23=2K_{\text{Alice-Mallory}} = (M_{\text{pub}})^a \bmod 23 = 4^6 \bmod 23 = \mathbf{2}.
    • Key with Bob: KBob-Mallory=(Mpub)bโ€Šmodโ€Š23=415โ€Šmodโ€Š23=9K_{\text{Bob-Mallory}} = (M_{\text{pub}})^b \bmod 23 = 4^{15} \bmod 23 = \mathbf{9}.
    • Mallory decrypts Alice's traffic with Key 2, reads the plaintext, re-encrypts it with Key 9, and forwards it to Bob undetected, proving why Diffie-Hellman requires digital signatures (PKI) for authentication!

Example 3: High-Speed Modular Exponentiation via Square-and-Multiplyโ€‹

Problem Statement: In RSA and Diffie-Hellman operations, computing modular exponentiations C=Meโ€Šmodโ€ŠnC = M^e \bmod n naively by multiplying MM by itself ee times requires astronomical CPU time and produces gigabyte-sized intermediate products that overflow hardware registers.

  1. Compute C=729โ€Šmodโ€Š100C = 7^{29} \bmod 100 using the formal Square-and-Multiply (Binary Exponentiation) algorithm.
  2. Formulate the binary representation of exponent e=29e = 29.
  3. Trace each intermediate squaring and multiplication step modulo 100.
  4. Calculate the total number of modular operations required versus naive multiplication, and analyze the asymptotic complexity for a 2048-bit RSA key.

Step-by-Step Solution:

  1. Binary Representation of Exponent e=29e = 29: 29=16+8+4+1=(11101)229 = 16 + 8 + 4 + 1 = (11101)_2 The binary string has length k=5k = 5 bits: b4=1,โ€…โ€Šb3=1,โ€…โ€Šb2=1,โ€…โ€Šb1=0,โ€…โ€Šb0=1b_4 = 1, \; b_3 = 1, \; b_2 = 1, \; b_1 = 0, \; b_0 = 1.
  2. Step-by-Step Algorithm Trace (Left-to-Right Scan): We initialize accumulator A=1A = 1 and process bits from most significant bit (b4b_4) to least significant (b0b_0):
    • Bit b4=1b_4 = 1 (MSB):
      • Square: A=12โ€Šmodโ€Š100=1A = 1^2 \bmod 100 = 1
      • Multiply: A=(1ร—7)โ€Šmodโ€Š100=7A = (1 \times 7) \bmod 100 = \mathbf{7}
    • Bit b3=1b_3 = 1:
      • Square: A=72โ€Šmodโ€Š100=49A = 7^2 \bmod 100 = \mathbf{49}
      • Multiply: A=(49ร—7)โ€Šmodโ€Š100=343โ€Šmodโ€Š100=43A = (49 \times 7) \bmod 100 = 343 \bmod 100 = \mathbf{43}
    • Bit b2=1b_2 = 1:
      • Square: A=432โ€Šmodโ€Š100=1849โ€Šmodโ€Š100=49A = 43^2 \bmod 100 = 1849 \bmod 100 = \mathbf{49}
      • Multiply: A=(49ร—7)โ€Šmodโ€Š100=343โ€Šmodโ€Š100=43A = (49 \times 7) \bmod 100 = 343 \bmod 100 = \mathbf{43}
    • Bit b1=0b_1 = 0:
      • Square: A=432โ€Šmodโ€Š100=1849โ€Šmodโ€Š100=49A = 43^2 \bmod 100 = 1849 \bmod 100 = \mathbf{49}
      • Multiply: Bit is 0 โ†’\to Skip multiplication (A=49A = \mathbf{49})
    • Bit b0=1b_0 = 1 (LSB):
      • Square: A=492โ€Šmodโ€Š100=2401โ€Šmodโ€Š100=1A = 49^2 \bmod 100 = 2401 \bmod 100 = \mathbf{1}
      • Multiply: A=(1ร—7)โ€Šmodโ€Š100=7A = (1 \times 7) \bmod 100 = \mathbf{7}
    • Final Output: 729โ€Šmodโ€Š100=7\mathbf{7^{29} \bmod 100 = 7}
  3. Complexity & Efficiency Analysis:
    • Naive Multiplication: Requires eโˆ’1=29โˆ’1=28ย multiplicationse - 1 = 29 - 1 = \mathbf{28\text{ multiplications}}.
    • Square-and-Multiply: Requires 4ย squarings+3ย multiplications=7ย modularย operations4\text{ squarings} + 3\text{ multiplications} = \mathbf{7\text{ modular operations}}.
    • Scaling to 2048-Bit RSA:
      • For a 2048-bit exponent, naive multiplication requires 22048โ‰ˆ3.23ร—106162^{2048} \approx 3.23 \times 10^{616} operations (impossible in the lifetime of the universe).
      • Square-and-multiply requires at most 2048ย squarings+2048ย multiplications=4096ย operations2048\text{ squarings} + 2048\text{ multiplications} = \mathbf{4096\text{ operations}}, completing on modern CPUs in under 1 millisecond!

๐ŸŽฏ Exam & Interview Pitfall Checkโ€‹

Question 1: Explain Euler's Totient function ฯ•(n)\phi(n) and why the security of the RSA cryptosystem relies fundamentally on the computational hardness of integer prime factorization.

Answer:

  • Euler's Totient Function ฯ•(n)\phi(n):
    • ฯ•(n)\phi(n) counts the number of positive integers less than nn that are coprime to nn.
    • When n=pโ‹…qn = p \cdot q is the product of two distinct prime numbers, any multiple of pp (there are qq such multiples) or qq (there are pp such multiples) shares a factor with nn.
    • By the inclusion-exclusion principle: ฯ•(n)=pโ‹…qโˆ’pโˆ’q+1=(pโˆ’1)(qโˆ’1)\phi(n) = p \cdot q - p - q + 1 = (p - 1)(q - 1)
  • Why Security Relies on Integer Factorization:
    • To compute the private key exponent dd, an adversary must solve eโ‹…dโ‰ก1(modฯ•(n))e \cdot d \equiv 1 \pmod{\phi(n)}, which requires knowing ฯ•(n)\phi(n).
    • However, calculating ฯ•(n)=(pโˆ’1)(qโˆ’1)\phi(n) = (p - 1)(q - 1) requires knowledge of the individual prime factors pp and qq.
    • The public key announces only the product n=pโ‹…qn = p \cdot q.
    • While multiplying two 1024-bit primes pp and qq takes microseconds, factoring a 2048-bit composite number nn back into pp and qq has no known polynomial-time classical algorithm (the General Number Field Sieve takes sub-exponential time O(expโก(cโ‹…(lnโกn)1/3(lnโกlnโกn)2/3))\mathcal{O}(\exp(c \cdot (\ln n)^{1/3} (\ln \ln n)^{2/3})), requiring billions of CPU-years).
    • Consequently, the private key dd remains computationally impossible to derive from public information.

Question 2: Explain the Man-in-the-Middle (MitM) attack on the classic Diffie-Hellman Key Exchange protocol. How is this vulnerability mitigated in modern protocols like TLS 1.3?

Answer:

  • Diffie-Hellman MitM Attack:
    1. Classic Diffie-Hellman establishes a shared key based on the mathematical properties of gabโ€Šmodโ€Špg^{ab} \bmod p, but it does not authenticate the identities of the participants.
    2. An active adversary (Eve) intercepts Alice's public value A=gaโ€Šmodโ€ŠpA = g^a \bmod p and generates her own private exponent e1e_1, sending E1=ge1โ€Šmodโ€ŠpE_1 = g^{e_1} \bmod p to Bob pretending to be Alice.
    3. When Bob replies with B=gbโ€Šmodโ€ŠpB = g^b \bmod p, Eve intercepts it and generates another private exponent e2e_2, sending E2=ge2โ€Šmodโ€ŠpE_2 = g^{e_2} \bmod p to Alice pretending to be Bob.
    4. Alice computes shared key KA=(E2)aโ€Šmodโ€ŠpK_A = (E_2)^a \bmod p. Eve computes the identical key KA=(A)e2โ€Šmodโ€ŠpK_A = (A)^{e_2} \bmod p.
    5. Bob computes shared key KB=(E1)bโ€Šmodโ€ŠpK_B = (E_1)^b \bmod p. Eve computes the identical key KB=(B)e1โ€Šmodโ€ŠpK_B = (B)^{e_1} \bmod p.
    6. Alice and Bob believe they share a secure connection, but they are actually communicating through Eve, who decrypts Alice's messages with KAK_A, inspects or alters them, and re-encrypts them with KBK_B for Bob.
  • Mitigation:
    • Diffie-Hellman must be paired with Digital Signatures and Public Key Certificates.
    • In TLS 1.3, the server sends an ephemeral Diffie-Hellman key share (ECDHE) which it cryptographically signs using its certified private signing key (RSA or ECDSA). The client verifies the signature against the server's trusted X.509 certificate, guaranteeing that the public DH parameter genuinely originated from the authentic server.

Core Conceptual Questions
  • Why can't we use RSA to encrypt a 1 GB movie file directly?:
    1. Size Constraint: Plaintext MM must be strictly smaller than the modulus nn (M<nM < n). For a 2048-bit key, the maximum plaintext block is 256 bytes.
    2. Performance: Modular exponentiation on multi-thousand-bit numbers requires massive CPU cycles. Encrypting a gigabyte file via RSA would take minutes and burn battery life.
    3. Hybrid Cryptography: Real-world protocols (TLS, SSH, PGP) use Hybrid Encryption: asymmetric cryptography (RSA/ECDHE) negotiates a lightweight 256-bit symmetric key, and fast symmetric cryptography (AES-GCM) encrypts the bulk payload.
  • Why is textbook RSA insecure without padding?: In textbook RSA (C=Meโ€Šmodโ€ŠnC = M^e \bmod n), encrypting the same message twice produces the exact same ciphertext. Furthermore, if e=3e = 3 and MM is small (M<n1/3M < n^{1/3}), M3<nM^3 < n, so C=M3C = M^3 and an attacker can recover MM simply by taking the integer cube root! Production RSA strictly mandates OAEP (Optimal Asymmetric Encryption Padding) to inject randomness.
Common Interview Traps
  • Trap: Confusing Private Exponent dd with Totient ฯ•(n)\phi(n): dd is the modular inverse of ee modulo ฯ•(n)\phi(n). It is calculated via eโ‹…dโ‰ก1(modฯ•(n))e \cdot d \equiv 1 \pmod{\phi(n)}, NOT eโ‹…dโ‰ก1(modn)e \cdot d \equiv 1 \pmod n.
  • Trap: Believing Diffie-Hellman Encrypts Data: Diffie-Hellman is strictly a Key Exchange / Key Agreement protocol. It cannot encrypt or decrypt messages directly; it merely establishes a shared symmetric secret key.

๐Ÿ’ฌ

Discussion & Doubts