Introduction to Cryptography
What cryptography provides — confidentiality, integrity, authenticity and non-repudiation — Kerckhoffs's principle, a brief history from Caesar to AES, symmetric vs asymmetric cryptography, the building blocks, threat models, and the golden rule — don't roll your own.
Cryptography is the science of protecting information from adversaries. It secures every HTTPS page you load, your messaging apps, passwords, payments, software updates and cryptocurrencies. You'll rarely implement primitives yourself, but every engineer needs to choose and use them correctly — misuse is far more common than broken algorithms.
What cryptography provides
| Goal | Question | Tools |
|---|---|---|
| Confidentiality | Can only intended parties read it? | Encryption (Symmetric, Public-key) |
| Integrity | Has it been modified? | Hashes, MACs |
| Authenticity | Who sent it? | MACs, digital signatures |
| Non-repudiation | Can the sender deny sending it? | Digital signatures (only the private-key holder could have signed) |
Encryption alone gives confidentiality, not integrity: an attacker may be able to flip bits in a ciphertext and predictably change the plaintext. That's why modern systems use authenticated encryption.
Kerckhoffs's principle
A cryptosystem should be secure even if everything about it, except the key, is public.
Security must rest on the secrecy of the key, not of the algorithm. Secret ("proprietary") algorithms get reverse-engineered, and only public scrutiny over years builds confidence. AES, ChaCha20, RSA and elliptic curves are all public; your keys are not. "Security through obscurity" is at best an extra layer, never the foundation.
A short history
| Era | Example | Lesson |
|---|---|---|
| Antiquity | Caesar cipher: shift each letter by 3 | Only 25 keys — trivially brute-forced |
| 9th century | Frequency analysis (al-Kindi) breaks substitution ciphers | Patterns in plaintext leak through |
| 16th century | Vigenère cipher: shifts vary with a keyword | Repeating keys still leak patterns (broken by Kasiski) |
| 1917 | One-time pad: XOR with a truly random key as long as the message, used once | Provably unbreakable, but impractical key distribution; reusing the key is catastrophic |
| WWII | Enigma rotor machine, broken at Bletchley Park | Operational mistakes and known plaintext matter as much as maths |
| 1976–77 | Diffie–Hellman and RSA — public-key cryptography | Strangers can agree on keys over a public channel |
| 1977 / 2001 | DES, then AES standardised via open competition | Open, peer-reviewed design wins |
| 2010s+ | Authenticated encryption, TLS 1.3, Signal protocol, post-quantum standards (2024) | Usability and misuse-resistance are design goals |
def caesar(text, shift):
return "".join(chr((ord(c) - 65 + shift) % 26 + 65) if c.isupper() else c for c in text)
print(caesar("ATTACK AT DAWN", 3)) # DWWDFN DW GDZQ — and every one of 25 shifts is easy to tryTwo families
- One shared key encrypts and decrypts.
- Very fast (AES runs at GB/s with hardware support).
- Problem: how do both sides get the key securely?
- Examples: AES-GCM, ChaCha20-Poly1305, HMAC.
- A key pair: public key (share freely) and private key (keep secret).
- Encrypt to the public key, or sign with the private key.
- ~1,000× slower; used for small data — key exchange and signatures.
- Examples: RSA, ECDH/X25519, Ed25519, ML-KEM.
Real systems use both (hybrid encryption): public-key cryptography to agree on or transport a fresh symmetric key, then symmetric encryption for the bulk data. That's exactly what TLS does (HTTPS and TLS).
The building blocks
Randomness
Keys, nonces and salts must come from a cryptographically secure pseudo-random number generator (CSPRNG) seeded by the operating system's entropy — never Math.random() or random.random(), which are predictable.
import secrets
key = secrets.token_bytes(32) # 256-bit key
token = secrets.token_urlsafe(32) # session token, password-reset linkconst key = crypto.getRandomValues(new Uint8Array(32)); // browser and NodeSecurity levels and threat models
- Computational security: breaking the scheme is possible in principle but requires infeasible work. A 128-bit security level means ~2¹²⁸ operations — far beyond all computers on Earth for the lifetime of the universe.
- Threat model: who is the adversary and what can they do? Eavesdrop passively? Modify traffic (man-in-the-middle)? Choose plaintexts or ciphertexts? Steal a database? Run code on the same machine (side channels)? Protocols are proven secure only against specific models.
- Quantum computers would break RSA and elliptic-curve cryptography (Shor's algorithm) and halve the effective strength of symmetric keys (Grover's algorithm). Hence post-quantum cryptography: NIST standardised ML-KEM (Kyber) for key exchange and ML-DSA (Dilithium) for signatures in 2024; browsers already use hybrid X25519 + ML-KEM key exchange.
The golden rule: don't roll your own
Cryptography fails in subtle ways that tests don't catch: a reused nonce, a timing leak in a comparison, a missing integrity check, a predictable random number. Instead:
- Use well-reviewed, high-level libraries: libsodium, Google Tink, the Python
cryptographypackage, Go'scrypto, the Web Crypto API, Java's JCA with modern algorithms. - Prefer misuse-resistant APIs that choose algorithms for you ("seal this message with this key").
- Use established protocols — TLS, SSH, Signal, WireGuard, standard OAuth/OIDC — rather than inventing new ones.
- Store passwords with a password hash, not encryption (Password storage).
- Manage keys properly: generate securely, store in a KMS or HSM, rotate, restrict access. Most real-world breaks attack keys and implementations, not the maths (Common attacks).
- Cryptography provides confidentiality, integrity, authenticity and non-repudiation; encryption alone doesn't guarantee integrity.
- Kerckhoffs's principle: security must depend only on the secrecy of the key.
- Symmetric crypto is fast but needs a shared key; asymmetric crypto solves key distribution; real systems combine them.
- Use a CSPRNG for all keys, nonces and tokens.
- Modern targets are 128-bit security; post-quantum algorithms are being deployed.
- Never roll your own: use vetted libraries, misuse-resistant APIs and standard protocols.