A postcard or a locked box
An analyst sends a customer file to a partner over the internet. A postcard can be read by anyone who handles it. A locked box opens 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.
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.
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.
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.
Hashing makes a fingerprint
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 that 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 % 997
print(simple_hash("survey"))
print(simple_hash("survey"))
print(simple_hash("surveys"))
# Same input, same hash. A change gives a new one.
# This toy is NOT safe. Real hashes are far stronger.
Symmetric encryption uses one key
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 of survey files.
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("SALES", 3)
print(secret)
print(shift_text(secret, 23))
# Locking with 3 and unlocking with 23 uses the same idea.
Asymmetric encryption uses two keys
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 file 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, and anyone can check with the public key.