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.
Builds on: Text as numbers (Digital systems › Data representation)
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.
Make your prediction first. Help opens once you've locked it in.

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

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

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.)
Add a decrypt function and test it by encrypting and decrypting HELLO.
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.
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?
A variable key starts at 3. B adds one to key and shows it.
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.
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.
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.
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.
Next: how long does it take to break this?