A secret note in class
You want to pass a secret note, but the teacher may catch it. So you change each letter to the next one in the alphabet.
HELLO becomes IFMMP. Your friend knows the trick, so she can read it. The teacher cannot.
Computers do the same, with much better tricks.
Key words
| Word | What it means | Example |
|---|---|---|
| Encryption | Scrambling data so only someone with the right key can read it. | HELLO becomes IFMMP |
| Decryption | Turning scrambled data back into readable data. | IFMMP becomes HELLO |
| Algorithm | A set of steps a computer follows. | the steps that scramble a message |
| Key | A secret value used to scramble or unscramble data. | shift by 1 |
| Plaintext | The message before it is scrambled. | HELLO |
| Ciphertext | The scrambled message. | IFMMP |
| Symmetric | Encryption with one key to lock and unlock. | one house key |
| Asymmetric | Encryption with two keys, one public and one private. | a postbox |
| Hashing | Turning data into a short fixed code that cannot be turned back. | a fingerprint of a file |
The cells students fill in are left blank in this preview.
Quick check: which word?
Have a go
Quick check: which word?
3 questionsExtra: Worked example: shift by 3
| Plain letter | Count 3 steps on, naming each letter | Cipher letter |
|---|---|---|
| A | A, B, C, D | D |
| B | B, C, D, E | E |
| X | X, Y, Z, A (wrap round) | A |
| H E L L O | Shift each letter by 3 | K H O O R |
The cells students fill in are left blank in this preview.
Extra: Try it: shift the letters
message = "HELLO"
key = 3
secret = ""
for letter in message:
place = ord(letter) - ord("A")
place = (place + key) % 26
secret = secret + chr(place + ord("A"))
print(secret)
# Change the key to 1. You should see IFMMP.
Make a free account to save your work and track your progress.
This shift is only a toy
Warning
Many people think any scrambling keeps data safe. A shift cipher has only 25 keys, so anyone can try them all in a few minutes. Real systems use much stronger methods, with very long keys.
Symmetric: one key
flowchart LR
plain[Plaintext] -->|lock with the shared key| cipher[Ciphertext]
cipher -->|unlock with the SAME key| plain2[Plaintext]
Asymmetric: two keys
flowchart LR
plain[Plaintext] -->|lock with the PUBLIC key| cipher[Ciphertext]
cipher -->|unlock with the PRIVATE key| plain2[Plaintext]
Match the term to its meaning
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Symmetric or asymmetric?
| Symmetric | Asymmetric | |
|---|---|---|
| Keys | One shared secret key | A public key that anyone can have (like a postbox slot). A private key that only you keep (like the postbox key) |
| Speed | Fast | Slower |
| Benefit | Good for large amounts of data | No secret key has to be shared first |
| Drawback | How do you share the key safely? | Slow for big files |
| Use | Encrypting a laptop disk | Starting a safe connection to a website |
The cells students fill in are left blank in this preview.
Extra: Real names, and how HTTPS uses both
The most used symmetric method is called AES. A well known asymmetric method is called RSA.
A safe website (HTTPS) uses both. First, asymmetric encryption lets your browser and the site agree a new shared key. Then symmetric encryption uses that key for the rest of the visit, because it is fast.
Hashing is not encryption
Encryption can be reversed with the key. Hashing cannot be reversed.
A hash is like a fingerprint. The same text always gives the same hash. A tiny change gives a very different hash.
Websites store the hash of your password, not the password. When you log in, the site hashes what you typed and compares.
Extra: Try it: a hash
import hashlib
for text in ["hello", "Hello", "hello!"]:
code = hashlib.sha256(text.encode("utf-8")).hexdigest()
print(text, "->", code[:16], "...")
# Look: one small change gives a completely different code.
Make a free account to save your work and track your progress.
Encryption: purpose, benefit, drawback
| Question | Answer |
|---|---|
| Purpose | Keep data private, even if someone steals it |
| Process | An algorithm and a key scramble the data. The right key unscrambles it |
| Benefit | A stolen laptop or a copied message is useless without the key |
| Drawback | If you lose the key, you lose the data. It takes some computer power. Keys must be managed |
| Hashing benefit | A stolen list of hashes does not show the passwords directly |
| Hashing drawback | Weak passwords can still be guessed by hashing many guesses |
The cells students fill in are left blank in this preview.
Extra: Your turn: shift and hash
Fill in the functions. Shift capital letters, encrypt and decrypt a message, and check a password by comparing hashes. The hash function is done for you. Press Run Tests.
import hashlib
def shift_letter(letter, key):
# Move a capital letter along the alphabet by key places. Z wraps round to A.
# shift_letter("A", 3) gives "D". Anything that is not a capital letter stays the same.
return letter
def encrypt(message, key):
# Use shift_letter on every letter of the message.
return message
def decrypt(message, key):
# The reverse. Hint: shifting back is the same as shifting forward by 26 minus key.
return message
def hash_of(text):
# This one is done for you. It gives back the hash of the text.
return hashlib.sha256(text.encode("utf-8")).hexdigest()
def password_matches(typed, stored_hash):
# True if the hash of what was typed equals the stored hash.
return False
Make a free account to save your work, or press Start lab to work through the whole lesson.
Scenario: what went wrong?
Have a go
Scenario: what went wrong?
4 questionsKey words crossword
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Final check
Have a go
Final check
5 questionsRemember
Important
Encryption hides data and can be reversed with a key. Symmetric uses one key. Asymmetric uses two. Hashing is one way and is used for passwords.