← Secret messages

The Caesar cipher

Shift every letter along the alphabet by a secret number. The method is public; the number is the key.

Julius Caesar is said to have shifted each letter of his orders three places along the alphabet. Anyone who did not know the shift saw nonsense.

The Caesar cipher shifts every letter along the alphabet by the key. With key 3, A becomes D, B becomes E, and Z wraps round to C. The function shift moves one letter; encrypt shifts every letter of a message. Spaces and numbers pass through unchanged.

Two things are new: ALPHABET.indexOf(letter) finds a letter's place in the alphabet (A is 0), and % 26 ("remainder after dividing by 26") wraps the end of the alphabet back to the start.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

The program as blocks
Show as code
caesar.ts
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
}
input.onButtonPressed(Button.A, function () {
    basic.showString(encrypt("HELLO", 3))
})
A is pressed. What does the micro:bit show?

RunRun it and compare the result with your prediction.

Run

The program as blocks
Show as code
caesar.ts
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
}
input.onButtonPressed(Button.A, function () {
    basic.showString(encrypt("HELLO", 3))
})
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.

InvestigateTrace how each line of code connects to what you see.

Investigate

The program as blocks
Show as code
caesar.ts
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
}
input.onButtonPressed(Button.A, function () {
    basic.showString(encrypt("HELLO", 3))
})

With key 3, which place does Z (place 25) move to? (A is 0.)

What does encrypt do with the key 0?

Ana encrypted a message with key 5. To decrypt it, which key should she use with the same encrypt function?

ModifyChange the code on purpose and check the result.

Modify

Add a decrypt function and test it by encrypting and decrypting HELLO.

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
Need a hint?

Make a function decrypt(message, key) that returns encrypt(message, 26 − key). Then show decrypt(encrypt("HELLO", 3), 3).

A decrypt function that undoes encrypt, so the micro:bit shows HELLO again.

This ticks by itself when your blocks do what is asked.

MakeBuild something new using what you have learned. Expect a few tries; that is normal.

Make

Make a secret messenger. A sends an encrypted message by radio and the receiver decrypts it with the same key; B adds one to the key on this micro:bit. Try it with both micro:bits using the same key, then with different keys. What does the receiver show?

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
Need a hint?

A variable key starts at 3. B adds one to key and shows it.

Another hint

A sends encrypt(MEET AT 3, key) by radio; the receiver shows decrypt(received string, key).

A sends an encrypted message by radio; the receiver decrypts it with the same key; B changes the key.

This ticks by itself when your blocks do what is asked.

For teachers

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

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

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

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.

Where it leads

Next: how long does it take to break this?

What each step is for

Now you can
Next lesson4. Breaking a Caesar cipherA Caesar cipher has only 25 useful keys, so trying them all breaks it in moments. Seeing why is how you learn what a strong key needs.