Notes
Encoding
Encoding changes the format of data so it can travel safely through systems that only handle text. Base sixty four and hexadecimal are the common ones. Here the word password one two three becomes a string of letters ending in an equals sign. It looks scrambled. It is not protected at all. No key was used, and one function call turns it straight back. Encoding is for compatibility. It gives zero confidentiality, and anyone who claims otherwise has confused it with encryption.
>>> base64.b64encode(b"password123") b'cGFzc3dvcmQxMjM=' >>> b"password123".hex() '70617373776f7264313233' >>> base64.b64decode( ... 'cGFzc3dvcmQxMjM=') b'password123' # no key was used at any point
Spot it
Learn to recognise encoding on sight, because it appears everywhere. Base sixty four uses letters, digits, plus and slash, and often ends with one or two equals signs. Hex is digits and the letters a to f. Two cases catch people out. A JSON web token is three Base sixty four parts joined by dots, and its middle part, which holds the user's details, can be read by anyone who has the token. And HTTP basic authentication is just the user name and password in Base sixty four.
- Base64: A-Z a-z 0-9 + / often ends in = or == hex: only 0-9 and a-f, even length
- URL encoding: %20 %3D
- a JWT: three Base64 parts joined by dots; the middle part is readable by anyone
- HTTP Basic auth: Base64 of user:password, not encrypted
Hashing
Hashing is different in kind. A hash function takes any input, of any size, and produces a fixed-size fingerprint. SHA two fifty six always gives two hundred and fifty six bits, shown here as sixty four hex characters. The same input always gives the same output. There is no key, and there is no way back: you cannot compute the input from the fingerprint. You can only guess inputs and compare, which matters a great deal when we get to passwords.
>>> hashlib.sha256(b"hello").hexdigest() '2cf24dba5fb0a30e26e83b2ac5b9e29e 1b161e5c1fa7425e73043362938b9824' # any input, always 256 bits out # same input, same output # no key, and no way back
Avalanche effect
A good hash has the avalanche effect. We changed one letter, hello to hell p, and the fingerprint is unrecognisably different: one hundred and thirty one of the two hundred and fifty six bits flipped, about half, which is what randomness looks like. That property is why hashes prove integrity. If a downloaded file's hash matches the published one, not a single bit has changed. If it differs at all, something did, and you should not trust the file.
sha256("hello") = 2cf24dba5fb0a30e...
sha256("hellp") = fdd7585e08c4e2af...
# one letter changed
# 131 of the 256 output bits
# flipped: about half
# so a hash proves that data
# has not changed at all Encryption
Encryption is the one that keeps secrets. It transforms data using a key, and only someone with the right key can transform it back. Here we encrypt a short string with AES, the standard cipher. Twenty one bytes go in and thirty seven come out: the data plus a sixteen byte tag that detects tampering. With the key, decryption returns the original. With the wrong key, or if one bit of the ciphertext changed, it raises an error and returns nothing at all.
key = AESGCM.generate_key(256)
nonce = os.urandom(12)
ct = AESGCM(key).encrypt(
nonce, b"card=4111...", None)
# 21 bytes in, 37 bytes out
# (data + a 16-byte check tag)
AESGCM(key).decrypt(nonce, ct, None)
# b'card=4111...'
# wrong key or changed data:
# InvalidTag, nothing returned Side by side
Three questions tell them apart. Is there a key? Can it be reversed? And what is it for? Encoding has no key, anyone can reverse it, and it exists for format. Hashing has no key, nobody can reverse it, and it is for integrity and for storing passwords. Encryption has a key, only the key holder can reverse it, and it is for secrecy. One rule covers a lot of confusion: if there is no key, there is no secret.
key? reverse? used for
encoding no anyone format
hashing no nobody integrity,
passwords
encryption yes key holder secrecy
if there is no key,
there is no secret Which one when
So which one when? To send binary data through a system that expects text, encode. To check that a file was not changed, hash. To store a user's login password, hash it, with a deliberately slow password hash that gets its own episode. To store something you must read back later, such as a card number or an address, encrypt. And for data in transit, use TLS, which combines all three inside one protocol you do not have to assemble yourself.
- send bytes through a text system: encode
- check a file was not changed: hash
- store a login password: hash, with a slow password hash (episode 5)
- store a card number you must read back: encrypt
- protect data in transit: TLS, which uses all three
The real secret
One principle sits under everything in this part. A cryptographic system must stay secure even if everything about it is public, except the key. AES and SHA two fifty six are published standards that thousands of experts have attacked for decades, and that scrutiny is why we trust them. So the secret is never the algorithm. It is the key. And that makes the hard problems practical ones: creating keys, storing them, sharing them and rotating them. Most of Part four is about exactly that.
- a system must stay secure even if everything about it is public, except the key
- AES, SHA-256, RSA: all published and studied for decades
- so the hard problems are: making keys, storing them, sharing them, rotating them
- that is most of this part
Never invent your own
A rule every security engineer repeats: never invent your own cryptography. Anyone can design a scheme that they personally cannot break, and it means nothing. Home-made schemes fail in ways their authors cannot see. Even combining good pieces in a new way is risky. Use high-level, reviewed libraries, and your cloud's key management service, and choose the boring, standard options. In cryptography, boring is the highest compliment.
- do not design algorithms
- do not combine primitives yourself unless you must
- use high-level, reviewed libraries: libsodium, Tink, the cryptography package, your platform KMS
- pick "boring" defaults: AES-GCM, SHA-256, Argon2, TLS 1.3
Common mistakes
Mistakes. Calling something encrypted when it is only Base sixty four encoded. Storing passwords encrypted, or worse in plain text, instead of hashed. Using a fast hash like MD five or plain SHA two fifty six for passwords. Hashing data you later need to read back. Treating the contents of a JSON web token as secret. Storing the encryption key right beside the data it protects. And inventing a cipher. Each of these has caused real breaches.
- "encrypted" with Base64
- passwords stored encrypted (or plain) instead of hashed
- MD5 or plain SHA-256 for passwords
- hashing data you need back
- trusting a JWT payload as secret
- a key stored beside the data it protects
- a home-made cipher
Say it like this
In the interview: encoding changes the format of data with no key, and anyone can reverse it; Base sixty four is the example, and it provides no security. Hashing is a one-way function producing a fixed-size fingerprint, used for integrity and, with a slow algorithm such as Argon two, for storing passwords. Encryption is reversible only with a key and provides confidentiality; AES in GCM mode is the standard choice. Then give one example of each being misused.
- "Encoding changes format, no key, anyone reverses it: Base64. Hashing is a one-way
- fixed-size fingerprint for integrity and password
- storage: SHA-256, Argon2.
- Encryption is reversible only with a key, for confidentiality: AES-GCM."