A postcard or a locked box
A postcard can be read by anyone who handles it. A locked box can be opened only with the key.
Data crossing the internet is like a postcard unless it is scrambled.
Today you meet three ways to scramble or fingerprint data, and learn when to use each.
By the end of this lesson
Note
I can tell apart three ways to scramble or fingerprint data and say when each one is used.
Key words
| Word | What it means |
|---|---|
| Encryption | Scrambling data so only someone with the right key can read it. |
| Plaintext | Data before it is scrambled. |
| Ciphertext | Data after it is scrambled. |
| Key | A secret value used to scramble or unscramble data. |
| Hashing | Turning data into a short fixed fingerprint. It cannot be turned back. |
| Symmetric encryption | Encryption where the same key locks and unlocks. |
| Asymmetric encryption | Encryption with a pair of keys: a public key locks and a private key unlocks. |
| Public key | The key you share with everyone. |
| Private key | The key you keep secret. |
| Digital signature | A mark made with a private key that proves who sent a message. |
The cells students fill in are left blank in this preview.
Starter: what you already know
Have a go
Starter: what you already know
3 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.
Cycle 1: hashing
Hashing turns any data into a short fingerprint of fixed size. The same data always gives the same hash. A tiny change gives a very different hash.
You cannot turn a hash back into the data.
Purpose: store passwords and check data has not changed. A site keeps the hash of your password, not the password. At login it hashes what you type and compares.
Try it: a toy hash
def simple_hash(text):
total = 0
for letter in text:
total = total + ord(letter)
return total % 1000
print(simple_hash("cat"))
print(simple_hash("cat"))
print(simple_hash("cot"))
# Same input, same hash. A change gives a new one.
# This toy is NOT safe. Real hashes are far stronger.
Cycle 2: symmetric encryption
Symmetric encryption uses one key to lock and the same key to unlock.
It is fast, so it suits large amounts of data such as a whole disk or a video call.
The problem: both sides must have the key. How do you send the key safely? If a thief sees the key, all is lost.
Try it: a toy symmetric cipher
def shift_text(text, key):
result = ""
for letter in text:
if letter >= "A" and letter <= "Z":
result = result + chr((ord(letter) - 65 + key) % 26 + 65)
else:
result = result + letter
return result
secret = shift_text("HELLO", 3)
print(secret)
print(shift_text(secret, 23))
# Locking with 3 and unlocking with 23 uses the same idea.
Cycle 3: asymmetric encryption
Asymmetric encryption uses a pair of keys. The public key locks. Only the matching private key unlocks.
Sam shares a public key with the world. Anyone can lock a message for Sam. Only Sam, with the private key, can read it.
It is slower, but it solves the sharing problem. It also gives digital signatures: sign with the private key, anyone can check with the public key.