#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.
- One shared secret key for both sides
- Roughly 1000x faster; built for bulk data
- Key distribution requires a secure channel
- 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.
- Pick primesChoose two secret large primes p and q
- Compute modulusCalculate n = p * q and phi(n) = (p - 1) * (q - 1)
- Select exponentsPick e coprime to phi(n) and compute d = e^-1 mod phi(n)
- 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.
$ 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.
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
ecube-root attack: Ife = 3and unpaddedM^3 < n, the modulus never wraps andMis just the integer cube root ofC. - Hastad Broadcast Attack: If the same unpadded
Mis encrypted withe = 3under three distinct modulin1, n2, n3, combining(C1, C2, C3)with the Chinese Remainder Theorem yieldsM^3, and takingiroot(M3, 3)recoversM. - Common Modulus Attack: Encrypting the same
Munder one sharednwith two coprime exponentse1, e2leaksMvia Bezout's identity. - Wiener Attack: Choosing a small private exponent
d(smaller than(1/3) * n^(1/4), which pairs with a hugee) allows recoveringdvia continued fractions.
# 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 = 3and usee = 65537with 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, orphi(n)or reusing primes across keys. Generate primes with a cryptographically secure RNG (secrets/ OS CSPRNG); shared primes let attackers factor both moduli instantly usingmath.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, requiresphi(n) = (p - 1) * (q - 1)andd * 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.