Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

8.3 (1 of 5): Encryption and hashing

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

50 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it meansExample
EncryptionScrambling data so only someone with the right key can read it.HELLO becomes IFMMP
DecryptionTurning scrambled data back into readable data.IFMMP becomes HELLO
AlgorithmA set of steps a computer follows.the steps that scramble a message
KeyA secret value used to scramble or unscramble data.shift by 1
PlaintextThe message before it is scrambled.HELLO
CiphertextThe scrambled message.IFMMP
SymmetricEncryption with one key to lock and unlock.one house key
AsymmetricEncryption with two keys, one public and one private.a postbox
HashingTurning 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Extra: Worked example: shift by 3

Plain letterCount 3 steps on, naming each letterCipher letter
AA, B, C, DD
BB, C, D, EE
XX, Y, Z, A (wrap round)A
H E L L OShift each letter by 3K H O O R

The cells students fill in are left blank in this preview.

Extra: Try it: shift the letters

Code: python
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?

SymmetricAsymmetric
KeysOne shared secret keyA public key that anyone can have (like a postbox slot). A private key that only you keep (like the postbox key)
SpeedFastSlower
BenefitGood for large amounts of dataNo secret key has to be shared first
DrawbackHow do you share the key safely?Slow for big files
UseEncrypting a laptop diskStarting 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

Code: python
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

QuestionAnswer
PurposeKeep data private, even if someone steals it
ProcessAn algorithm and a key scramble the data. The right key unscrambles it
BenefitA stolen laptop or a copied message is useless without the key
DrawbackIf you lose the key, you lose the data. It takes some computer power. Keys must be managed
Hashing benefitA stolen list of hashes does not show the passwords directly
Hashing drawbackWeak 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.

Code: python
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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Key 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Remember

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.

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.