Students build the Caesar cipher, decrypt with it, and send encrypted messages by radio with a shared key.
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.
Predict and Run 8 min · Investigate 10 min · Modify 10 min · Make 18 min
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.
Wrapping round the alphabet with % 26, and undoing a shift. Ask them to shift one letter by hand, including one near Z, before testing.
| What you will see | A question to ask | The 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. |
One possible solution for each task. Other correct programs are fine.

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))
})

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: 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.
What is the key in a Caesar cipher?
The number of places each letter shifts. Sender and receiver must use the same number.
Next: how long does it take to break this?