← Teacher guide

The Caesar cipher

Students build the Caesar cipher, decrypt with it, and send encrypted messages by radio with a shared key.

Secret messages · Secret codes · about 50 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 8 min · Investigate 10 min · Modify 10 min · Make 18 min

Before class

The idea: every letter moves along the alphabet by the key. The method (shift) is public; the key (the number) is the secret. Both people need the same key.

New code: ALPHABET.indexOf(letter) is the letter's place in the alphabet (A is 0, and −1 if it is not found, which is why spaces pass through). % 26 wraps: 28 % 26 = 2. Treat these as recipes; the Investigate step makes the wrap visible.

A pattern worth naming: decrypting is encrypting with the rest of the circle, 26 − key. Students often expect a separate "undo" method.

Honesty about strength: this code is easy to break, as the next lesson shows. Say so now; the lesson after turns it into the point.

Running the lesson

  1. Investigate: the Z → C question is the whole idea. Give time to do it by hand.
  2. Make: when keys differ, the receiver shows plausible-looking nonsense. Ask: does the receiver know it is wrong?

The hard part (on purpose)

Wrapping round the alphabet with % 26, and undoing a shift. Ask them to shift one letter by hand, including one near Z, before testing.

When students get stuck

What you will seeA question to askThe fix
Spaces or numbers come out as letters.What does indexOf return for a space?indexOf gives −1 for a character that is not a letter; shift returns it unchanged.
decrypt does not give back the message.How far round the alphabet is encrypt plus decrypt?The two keys must add to 26: decrypt uses 26 − key.

Answers to the checks

  1. Predict: A is pressed. What does the micro:bit show?
    KHOOR. H→K, E→H, L→O, L→O, O→R: each letter moves three places along.
  2. Z wraps round: With key 3, which place does Z (place 25) move to? (A is 0.)
    2
    25 + 3 = 28, and 28 % 26 = 2: place 2 is C. Without % 26 it would run off the end of the alphabet.
  3. Key zero: What does encrypt do with the key 0?
    Nothing is changed
    A shift of 0 leaves each letter where it is. Key 26 does the same, because 26 % 26 is 0. A key of 0 or 26 would be no secret at all.
  4. Going back: Ana encrypted a message with key 5. To decrypt it, which key should she use with the same encrypt function?
    −5, or 21 (26 − 5)
    Shifting by 5 and then by 21 is 26 places in all: the full circle back to the start. So decrypt(message, key) is encrypt(message, 26 − key).

Solutions

One possible solution for each task. Other correct programs are fine.

Modify

The program as blocks
Show as code
let ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
function shift(letter: string, key: number): string {
    let place = ALPHABET.indexOf(letter)
    if (place < 0) {
        return letter
    }
    return ALPHABET.charAt((place + key) % 26)
}
function encrypt(message: string, key: number): string {
    let result = ""
    for (let i = 0; i < message.length; i++) {
        result = result + shift(message.charAt(i), key)
    }
    return result
}
function decrypt(message: string, key: number): string {
    return encrypt(message, 26 - key)
}
input.onButtonPressed(Button.A, function () {
    basic.showString(decrypt(encrypt("HELLO", 3), 3))
})

Make

The program as blocks
Show as code
let ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let key = 3
function shift(letter: string, key: number): string {
    let place = ALPHABET.indexOf(letter)
    if (place < 0) {
        return letter
    }
    return ALPHABET.charAt((place + key) % 26)
}
function encrypt(message: string, key: number): string {
    let result = ""
    for (let i = 0; i < message.length; i++) {
        result = result + shift(message.charAt(i), key)
    }
    return result
}
function decrypt(message: string, key: number): string {
    return encrypt(message, 26 - key)
}
radio.setGroup(5)
input.onButtonPressed(Button.A, function () {
    radio.sendString(encrypt("MEET AT 3", key))
})
input.onButtonPressed(Button.B, function () {
    key += 1
    basic.showNumber(key)
})
radio.onReceivedString(function (receivedString) {
    basic.showString(decrypt(receivedString, key))
})

Stretch and support

Stretch: Add a numeric key menu: B counts up and A sends, and the key resets to 1 after 25.

Support: Give encrypt and decrypt complete; students wire up the buttons and radio.

Exit ticket

What is the key in a Caesar cipher?

The number of places each letter shifts. Sender and receiver must use the same number.

What each step is for

Where it leads

Next: how long does it take to break this?