Skip to main content
Asymmetric Cryptography: RSA, Diffie-Hellman & ECC
Lesson 6 of 19

Asymmetric Cryptography: RSA, Diffie-Hellman & ECC

Explain public-key encryption, RSA key generation with OAEP/PSS, Diffie-Hellman and X25519 key exchange, digital signatures, and common RSA implementation attacks.

Intermediate 32 min Cryptography

Learning objectives

  • Compare asymmetric and symmetric cryptography and explain their roles in hybrid encryption
  • Derive RSA keys using Euler's totient and explain why production RSA requires OAEP and PSS padding
  • Trace Diffie-Hellman and X25519 key exchange and explain why authentication prevents MitM attacks
  • Compare RSA and ECC key sizes and diagnose classic textbook RSA attacks such as Hastad and Wiener

Recommended first: Math Foundations: Modular Arithmetic, GCD & CongruencesSymmetric Encryption: AES, Block Modes & AEAD

#Why this matters

Symmetric ciphers like AES encrypt gigabytes per second, yet they assume both sides already hold the same secret key. Asymmetric (public-key) cryptography removes that bottleneck by splitting the key into a public key that anyone may see and a private key that only the owner keeps.

Symmetric vs Asymmetric Cryptography
Symmetric (AES-GCM, ChaCha20)
  • One shared secret key for both sides
  • Roughly 1000x faster; built for bulk data
  • Key distribution requires a secure channel
Asymmetric (RSA, X25519, Ed25519)
  • Linked public key and private key pair
  • Slower math; built for key exchange and signatures
  • Public key can travel over an open channel

In practice, TLS 1.3, SSH, and Signal use asymmetric algorithms to authenticate peers and negotiate a fresh session key, then hand bulk traffic to symmetric AEAD.

#RSA mathematics and secure padding

RSA security relies on the hardness of factoring a large composite modulus n = p * q into its two secret prime factors. Best known classical factoring algorithms (the General Number Field Sieve) run in sub-exponential time, so keys must be at least 2048 bits, with 3072 bits recommended for 128-bit equivalent security.

  1. Pick primesChoose two secret large primes p and q
  2. Compute modulusCalculate n = p * q and phi(n) = (p - 1) * (q - 1)
  3. Select exponentsPick e coprime to phi(n) and compute d = e^-1 mod phi(n)
  4. Protect messageApply OAEP padding to encrypt or PSS padding to sign

In textbook math, encryption computes C = M^e mod n and decryption computes M = C^d mod n. Decryption recovers M because e * d = 1 (mod phi(n)), so by Euler's theorem M^(e*d) = M (mod n). Keeping p, q, and phi(n) secret is critical: anyone who learns them can immediately compute d = pow(e, -1, phi).

Textbook RSA without padding is completely insecure in production. It is deterministic (the same message produces the same ciphertext) and malleable ((2^e * C)^d mod n = 2 * M). Real systems must use RSA-OAEP (RFC 8017) for encryption and RSA-PSS for digital signatures, and should use RSA primarily for signatures or legacy key transport rather than fresh key exchange.

Verifying small RSA arithmetic and generating modern keys
$ python3 -c "p,q=61,53; n=p*q; phi=(p-1)*(q-1); e=17; d=pow(e,-1,phi); c=pow(65,e,n); print(f'n={n} d={d} C={c} M={pow(c,d,n)}')"
n=3233 d=2753 C=2790 M=65
$ openssl genpkey -algorithm X25519 | head -n 2
-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VuBCIEICiP8PuyfM0b/wAGaqmWY79BShQQri+GrqTNvw/k8VtY

#Diffie-Hellman key exchange and ECC

Diffie-Hellman lets Alice and Bob derive a shared secret over an open network without ever transmitting the secret itself. Its security relies on the Discrete Logarithm Problem: given public prime p, generator g, and A = g^a mod p, an eavesdropper cannot feasibly recover a.

Diffie-Hellman key exchange (toy parameters p=23, g=5)Diffie-Hellman key exchange (toy parameters p=23, g=5)AliceBobA = 5^6 mod 23 = 8B = 5^15 mod 23 = 19Alice computes 19^6 mod 23 = 2; Bob computes 8^15 mod 23 = 2

An eavesdropper sees p, g, A, and B, but multiplying A * B mod p only gives g^(a+b) mod p, not the shared secret K = g^(a*b) mod p.

Modern protocols replace finite-field Diffie-Hellman with Elliptic Curve Cryptography (ECC), especially X25519 (Curve25519 ECDHE) and P-256 (secp256r1). Because no sub-exponential attack is known for well-chosen curves, ECC achieves 128-bit security with a 256-bit key -- 12 times smaller than a 3072-bit RSA key.

Algorithm Key size Equivalent security Typical 2026 use
RSA-2048 2048 bits 112-bit Legacy certificates and signatures
RSA-3072 3072 bits 128-bit High-assurance RSA signatures (PSS)
X25519 / P-256 256 bits 128-bit TLS 1.3 ECDHE key exchange, WireGuard
Ed25519 (EdDSA) 256 bits 128-bit SSH keys (ssh-keygen -t ed25519), code signing

Note that Ed25519 is an EdDSA signature scheme on Curve25519, not ECDSA. Also note that TLS 1.3 and modern libraries are now adding hybrid post-quantum key encapsulation (ML-KEM, NIST FIPS 203) alongside X25519 to defend against future quantum computers.

#Digital signatures and classic RSA attacks

A digital signature proves authenticity and integrity. To sign message M, Alice hashes it (H = SHA256(M)) and signs the digest with her private key (using RSA-PSS or Ed25519). Anyone can verify (M, S) using Alice's public key. Verification fails if M was altered in transit or if S was produced by a different private key.

In CTF challenges and flawed legacy code, textbook RSA fails in predictable ways:

  • Small e cube-root attack: If e = 3 and unpadded M^3 < n, the modulus never wraps and M is just the integer cube root of C.
  • Hastad Broadcast Attack: If the same unpadded M is encrypted with e = 3 under three distinct moduli n1, n2, n3, combining (C1, C2, C3) with the Chinese Remainder Theorem yields M^3, and taking iroot(M3, 3) recovers M.
  • Common Modulus Attack: Encrypting the same M under one shared n with two coprime exponents e1, e2 leaks M via Bezout's identity.
  • Wiener Attack: Choosing a small private exponent d (smaller than (1/3) * n^(1/4), which pairs with a huge e) allows recovering d via continued fractions.
python
# Toy demonstration of Hastad Broadcast Attack (e = 3, no OAEP padding)
# Given ciphertexts C1, C2, C3 under distinct moduli n1, n2, n3:
# 1. Recover M^3 mod (n1*n2*n3) using the Chinese Remainder Theorem (crt)
# 2. Take the integer cube root to recover M
from math import isqrt

def integer_cbrt(x: int) -> int:
    lo, hi = 0, 1 << ((x.bit_length() + 2) // 3)
    while lo < hi:
        mid = (lo + hi + 1) // 2
        if mid ** 3 <= x:
            lo = mid
        else:
            hi = mid - 1
    return lo

#Defending / Common mistakes

  • Using textbook RSA or PKCS#1 v1.5 encryption. Always use RSA-OAEP (SHA-256) for encryption and RSA-PSS for signatures; avoid e = 3 and use e = 65537 with at least 2048-bit (ideally 3072-bit) keys.
  • Unauthenticated Diffie-Hellman. Plain DH stops passive eavesdropping, not active Man-in-the-Middle attacks. Always authenticate key-exchange messages with certificates or signed handshakes (as in TLS 1.3 and SSH).
  • Static RSA key exchange. Static RSA lacks forward secrecy. Prefer ephemeral X25519/ECDHE (and hybrid ML-KEM where supported) so past recordings stay safe if a server key leaks.
  • Exposing p, q, or phi(n) or reusing primes across keys. Generate primes with a cryptographically secure RNG (secrets / OS CSPRNG); shared primes let attackers factor both moduli instantly using math.gcd(n1, n2).

#Summary

  • Asymmetric cryptography solves key distribution and enables digital signatures by separating a public key from a private key, while pairing with symmetric AEAD for bulk speed.
  • RSA relies on factoring n = p * q, requires phi(n) = (p - 1) * (q - 1) and d * e = 1 (mod phi(n)), and must always use OAEP/PSS padding with >= 2048-3072 bit keys.
  • Diffie-Hellman relies on the Discrete Logarithm Problem; modern protocols prefer ephemeral X25519/ECDHE (256-bit keys matching 3072-bit RSA) with authentication for forward secrecy.
  • Digital signatures are created with the signer's private key and verified with the signer's public key; never deploy unpadded textbook RSA.

Lecture presentations

Slides on this topic: view them in the browser or download the PDF.

Idea of Asymmetric Cryptography and Diffie-Hellman Key Exchange

Is it possible to obtain a shared secret over an open channel without handing the key to anyone?

Slides: 24 PDF · 1.1 MB
RSA Algorithm: Key Generation, Encryption/Decryption and Correctness Proof

The first practical public-key cryptosystem: two keys, one mathematical foundation — multiplying large numbers is easy, factoring them is hard.

Slides: 26 PDF · 832 KB
RSA Cryptanalysis: Factoring, Small Exponent and Timing Attacks

Three ways to break RSA — attacking the mathematics, the protocol and the device. Most real-world breaks happen in the last group.

Slides: 23 PDF · 972 KB
Elliptic-Curve Cryptography: Basics, ECDH and ECDSA

A key 12 times shorter for the same security. The price is a new kind of arithmetic: adding points on a curve instead of multiplying numbers.

Slides: 26 PDF · 1.1 MB

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge