Lesson 2 · Unit 01
Shared keys and key pairs
Encrypt, sign, HMAC, and why base64 is none of them
What you practise
- Symmetric encryption, the key-distribution problem, public/private pairs, encrypting vs signing, HMAC, encoding (base64) is not encryption
Try a question from this lesson
Pick one
A config file holds cGFzc3dvcmQ= and a teammate says the value is safe. This line needs no key. What was done to the password?
import base64
s = "cGFzc3dvcmQ="
print(base64.b64decode(s).decode())
# passwordCorrect answer
It was encoded: a change of format, not a secret
Why
If anyone can undo it without a key, it is not encryption. Base64 only rewrites bytes as safe text characters. Hashing cannot be undone at all, and signing does not hide anything.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Symmetric: one shared key
Symmetric encryption uses the same key to lock and to unlock. Real ciphers such as AES are fast enough for whole disks and video streams.
The catch: both sides must already hold the key, and sending it over the network would expose it.
The toy below shows the shape only. Never use XOR like this for real.
key = b"K3y!"
def xor(data): # toy cipher
return bytes(d ^ k for d, k
in zip(data, key))
secret = xor(b"hi!!")
print(xor(secret)) # b'hi!!'Concept
HMAC: a hash with a key
Anyone can compute a plain hash, so a hash attached to a message proves nothing about its sender.
An HMAC mixes a secret key into the hash. Only someone holding the key can produce the tag or check it.
Webhooks, signed cookies and API requests all use it: "this came from someone who knows our secret, and it was not changed".
import hmac
key = b"shared-secret"
tag = hmac.new(key, b"amount=100",
"sha256").hexdigest()10 more exercises in the app
- 2× type the answer
- 4× multiple choice
- 1× match the pairs
- 1× fill the blanks
- 1× true or false
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.