Applied Cryptography: Introduction
A code review finds this line in a payment service: cipher = AES.new(key, AES.MODE_ECB). Another finds hashlib.sha256(password).hexdigest() in a login handler, a third if mac == expected: in a webhook receiver, and a fourth a TLS client with verify=False. Each line uses a respected algorithm, and each one is a vulnerability. Real cryptographic failures are almost never "someone broke AES". They are the wrong mode, a repeated nonce, a fast hash where a slow one was needed, a missing check, or a key stored next to the data it protects.
This course teaches cryptography the way a security engineer meets it: what each primitive promises, how it fails when it is misused, how you detect the misuse, and what the fix is. The mathematics is there when it explains a failure (why a repeated ECDSA nonce reveals the private key, why the birthday bound halves a hash's strength), and the derivations live in Number Theory & Cryptography if you want them in full. The one-page overview is Cryptography Basics in the on-ramp course.
A first run
The example encrypts a message with AES-256-GCM, decrypts it, then flips one bit of the ciphertext "in transit". The key and the nonce are fixed so the output is the same on every run; real code draws them from the operating system's random generator (AESGCM.generate_key, os.urandom(12)) and never reuses a nonce with the same key.
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.exceptions import InvalidTag
key = bytes(range(32)) # fixed for the lesson; real code: AESGCM.generate_key(bit_length=256)
nonce = bytes(12) # fixed for the lesson; real code: os.urandom(12), never reused with this key
aes = AESGCM(key)
msg = b"meet at the north gate at 10:00"
ct = aes.encrypt(nonce, msg, None)
print("plaintext :", len(msg), "bytes")
print("ciphertext:", len(ct), "bytes =", len(msg), "+ 16-byte tag")
print("first bytes:", ct[:12].hex())
print("decrypted :", aes.decrypt(nonce, ct, None).decode())
tampered = bytearray(ct)
tampered[5] ^= 0x01 # flip one bit in transit
try:
aes.decrypt(nonce, bytes(tampered), None)
except InvalidTag:
print("tampered : InvalidTag, message rejected")plaintext : 31 bytes
ciphertext: 47 bytes = 31 + 16-byte tag
first bytes: 63d9d0aa954df79d7cc0cc15
decrypted : meet at the north gate at 10:00
tampered : InvalidTag, message rejected
Two properties show up in five lines. The ciphertext hides the message (confidentiality), and the 16-byte tag makes any change detectable (integrity and authenticity). Older designs gave you only the first and left the second to you; much of this course is about what goes wrong in that gap.
What the course covers
| Section | What you learn | What you run |
|---|---|---|
| Symmetric encryption | the one-time pad, AES, ECB/CBC/CTR, padding oracles, AES-GCM and ChaCha20-Poly1305, nonce reuse, why DES and 3DES are gone | the block-modes lab, OpenSSL enc, a padding oracle and a nonce-reuse break on toys the lesson builds |
| Hashes and MACs | preimage and collision resistance, the birthday bound, SHA-2, SHA-3, BLAKE2, the MD5 and SHA-1 collisions, length extension, HMAC | the avalanche lab, birthday searches on truncated hashes, a pure-Python SHA-256 that forges a toy MAC |
| Public-key cryptography | RSA, OAEP and PSS, elliptic curves and ECDSA, Ed25519, Diffie–Hellman, X25519, forward secrecy | small-number RSA and DH, then the cryptography library on RFC test vectors |
| PKI and TLS | X.509, chains, revocation, Certificate Transparency, ACME, the TLS 1.3 handshake and key schedule, TLS's limits | certificates built in Python, openssl verify, s_server/s_client on 127.0.0.1 |
| Randomness and passwords | CSPRNGs, password entropy, PBKDF2, scrypt, bcrypt, Argon2id, peppers, HKDF | the password-cost lab, every password hash with current OWASP parameters |
| Using crypto safely | key management, envelope encryption, common mistakes, post-quantum migration | a KMS-style envelope, a crypto-misuse linter, a toy lattice cipher |
| Practice | exercises; mini project | an encrypted, authenticated file vault with a versioned header and tamper tests |
How the examples run
```python runexamples run in your browser in Pyodide, withhashlib,hmac,secrets,cryptography,argon2-cffi,bcrypt,pycryptodomeandpynacl. Pyodide'shashlibhas nopbkdf2_hmacorscrypt, so the course usescryptography'sPBKDF2HMACandScrypt.```bash runblocks are recorded sessions from a sandbox with OpenSSL 3.0, no network and no root. The lab PKI lives in$SAMPLES/pki: a lab root CA, a certificate forlab.test, an expired one and a chain file. Their RSA keys are derived from a fixed seed: they are for the lab only and must never protect anything real. TLS servers are started on127.0.0.1inside the block that uses them.- Fixed keys, nonces and salts appear wherever output must be stable, and each example says so. In real code they come from
secretsoros.urandom. Where a value is random by nature (RSA key generation, ECDSA signatures, bcrypt and Argon2 hashes), the examples print lengths, prefixes or verification results instead of the value.
The rules this course keeps
This is a Cybersecurity-track course and keeps its safety line (Security Foundations):
- Mechanism → detection → fix. Every attack lesson ends with how the weakness is found and how it is removed.
- Only toy targets. Padding oracles, nonce reuse, length extension and brute force are run against toys the example itself creates, with tiny key spaces where a search is involved. Nothing here cracks a real password hash or attacks a real service.
- No real secrets. Published test vectors (FIPS, RFC) and lab-only keys are labelled as such.
Before you start
You should be comfortable with Python bytes, hex and int.from_bytes (Python), and with the security vocabulary of Security Foundations. For TLS on the wire, TLS Records and the ClientHello and OpenSSL s_client and s_server are good companions.
Test yourself
In the first example, why does decryption of the tampered ciphertext fail?
- AES cannot decrypt modified data
- The GCM tag no longer matches, so the library rejects the message
- The nonce changed
- The key is too short
Which of these is the most common cause of real cryptographic failures?
- Breaking AES mathematically
- Misuse: wrong mode, repeated nonce, fast password hash, skipped verification
- Quantum computers
- Short ciphertexts
Why do the examples use fixed keys and nonces?
- Fixed values are more secure
- So the printed output is the same on every run; real code uses secrets or os.urandom
- The browser has no random generator
- AES requires them