Cryptography Basics and Security Goals
Before building RSA and Diffie-Hellman, it helps to know what "secure" is supposed to mean. Modern cryptography replaced clever secret designs with precise goals, public algorithms and keys sized against the best known attacks. This lesson sets up that vocabulary; the practical side (TLS, certificates, password storage) is covered in Cybersecurity.
Kerckhoffs's principle
A system should stay secure even if everything except the key is public. Algorithms like AES, RSA and SHA-256 are published and studied by thousands of researchers; only the key is secret. "Security through obscurity" fails as soon as the design leaks, and it always leaks.
Symmetric and public-key cryptography
| Symmetric | Public-key (asymmetric) | |
|---|---|---|
| Keys | one shared secret key | a public key and a private key |
| Examples | AES, ChaCha20, HMAC | RSA, Diffie-Hellman, ECDSA, ML-KEM |
| Speed | gigabytes per second | thousands of operations per second |
| Main problem | how do both sides get the key? | how do you know a public key is genuine? |
Real systems combine them: a public-key exchange agrees on a fresh symmetric key, and the symmetric cipher protects the data (TLS, SSH, Signal).
The one-time pad: perfect but impractical
XOR the message with a truly random key of the same length, used once. Shannon proved that the ciphertext reveals nothing about the message (perfect secrecy). The price is a key as long as all the data you will ever send. Reusing the key is fatal: , and the key cancels.
m1, m2 = b"ATTACK AT DAWN", b"RETREAT AT TEN"
key = bytes([0x3A, 0x91, 0x5C, 0x07, 0xE2, 0x48, 0x1F, 0xB6, 0x70, 0x2D, 0xC9, 0x84, 0x13, 0x6E]) # pretend this is a fresh secret key
c1 = bytes(a ^ k for a, k in zip(m1, key))
c2 = bytes(a ^ k for a, k in zip(m2, key))
print(c1.hex())
leak = bytes(a ^ b for a, b in zip(c1, c2))
print(leak == bytes(a ^ b for a, b in zip(m1, m2)))
# knowing m1, an attacker recovers m2 without the key:
print(bytes(a ^ b for a, b in zip(leak, m1)))7bc50846a1033ff7240d8dc54420
True
b'RETREAT AT TEN'
Computational security
Practical schemes give up perfect secrecy and aim for: no attacker with a realistic budget (say operations) can succeed with more than negligible probability. Security is then stated in bits: -bit security means the best attack costs about operations.
ops_per_second = 10**18 # an absurdly strong attacker: a billion billion per second
seconds_per_year = 365.25 * 24 * 3600
for bits in (56, 64, 80, 112, 128, 256):
years = 2**bits / ops_per_second / seconds_per_year
print(f"{bits:3}-bit security: {years:.2e} years") 56-bit security: 2.28e-09 years
64-bit security: 5.85e-07 years
80-bit security: 3.83e-02 years
112-bit security: 1.65e+08 years
128-bit security: 1.08e+13 years
256-bit security: 3.67e+51 years
56-bit DES falls in a fraction of a second at that rate; 128 bits takes about years, longer than the age of the universe.
What a cipher must hide: semantic security
A good public-key encryption scheme must be randomized. If encryption were deterministic, anyone could encrypt guesses ("yes", "no", a salary from a short list) with the public key and compare. The formal goal, IND-CPA (indistinguishability under chosen-plaintext attack), says an attacker who picks two messages and gets the encryption of one of them cannot tell which, better than guessing. The stronger IND-CCA also allows the attacker to decrypt other ciphertexts. Textbook RSA meets neither (Why Textbook RSA Is Insecure); padded RSA-OAEP and ElGamal-style schemes are designed to.
Matching key sizes
| Security | Symmetric key | RSA / finite-field DH modulus | Elliptic curve | Hash output |
|---|---|---|---|---|
| 80 bits (legacy) | 80 | 1024 | 160 | 160 |
| 112 bits | 112 | 2048 | 224 | 224 |
| 128 bits | 128 | 3072 | 256 | 256 |
| 256 bits | 256 | 15360 | 512 | 512 |
The column differences come from the attacks: generic key search for symmetric keys, the number field sieve for RSA and DH (Factoring Algorithms), attacks for elliptic curves, and the birthday bound for hash collisions.
In software
- Use high-level, misuse-resistant APIs: libsodium's
crypto_box, Python'scryptographyFernet or AES-GCM, TLS libraries. Never compose primitives yourself in production. - NIST SP 800-57 and the ECRYPT/BSI reports publish the key-size tables above; 2048-bit RSA is acceptable until 2030 under NIST guidance, 3072 bits after that.
Exercises
Exercise 1: Kerckhoffs
A vendor says its product is secure "because the algorithm is secret". What is wrong with this argument?
Answer
Security must rest only on the key; secret algorithms are reverse-engineered and unreviewed.
Solution
Software and hardware can be disassembled, so the algorithm will eventually be known; when that happens, every deployed device is broken at once and cannot be fixed by changing a key. Public algorithms have been attacked by experts for years, so their security is far better understood.
Exercise 2: Key reuse
Two messages were encrypted with the same one-time-pad key. You know the first message is "HELLO" and see both ciphertexts. What can you learn?
Answer
The whole second message (as far as the first one reaches).
Solution
, so . Even without knowing , statistics of language ("crib dragging") usually recover both, as happened to Soviet messages in the VENONA project.
Exercise 3: Brute-force budget (code)
How many bits of key can an attacker trying keys per second search in one year?
Answer
About 64 bits.
Solution
import math
print(math.log2(10**12 * 365.25 * 24 * 3600))64.77462129339004
A year holds about trials. A 56-bit DES key ( keys) falls in about 20 hours, while an 80-bit key would take about 38 million years.
Test yourself
Kerckhoffs's principle says a cryptosystem should be secure even if…
- the key is public
- everything except the key is public
- the ciphertext is secret
- the attacker has no computer
Why must public-key encryption be randomized?
- to be faster
- otherwise anyone can encrypt guesses with the public key and compare ciphertexts
- to save memory
- because primes are random
Which RSA modulus size gives about the same security as a 256-bit elliptic curve?
- 256 bits
- 1024 bits
- 3072 bits
- 512 bits