A scrambling method works only while nobody knows the method. A real code has a public method and a secret key.
A message written backwards looks secret for about ten seconds. The question is what makes a code truly hard to read, and the answer is not hiding the method.
A first idea for privacy is to scramble the message before sending it. The micro:bit below reverses the text with a function (a named job, from micro:bit Foundations). HELLO goes out as OLLEH.
Behind the code: in micro:bit Foundations a function was a named job. This one, reverse, also takes a value in and hands one back:
reverse("HELLO") hands "HELLO" to the function, which calls it text. A value handed in like this is a parameter.reverse("HELLO") can go anywhere a text can, such as inside send string.Inside, a for index loop (in Loops) visits each place in the text, from 0 up to its length − 1. text.charAt(i) is the letter at place i, counted from 0 like a list. result starts empty, and each time round the next letter is joined on at the front, so the text comes out backwards. Every cipher in this course is built this way: a function that walks through a message letter by letter and returns a new one.
Make your prediction first. Help opens once you've locked it in.

function reverse(text: string): string {
let result = ""
for (let i = 0; i < text.length; i++) {
result = text.charAt(i) + result
}
return result
}
radio.setGroup(5)
input.onButtonPressed(Button.A, function () {
radio.sendString(reverse("HELLO"))
})
radio.onReceivedString(function (receivedString) {
basic.showString(receivedString)
})

function reverse(text: string): string {
let result = ""
for (let i = 0; i < text.length; i++) {
result = text.charAt(i) + result
}
return result
}
radio.setGroup(5)
input.onButtonPressed(Button.A, function () {
radio.sendString(reverse("HELLO"))
})
radio.onReceivedString(function (receivedString) {
basic.showString(receivedString)
})

function reverse(text: string): string {
let result = ""
for (let i = 0; i < text.length; i++) {
result = text.charAt(i) + result
}
return result
}
radio.setGroup(5)
input.onButtonPressed(Button.A, function () {
radio.sendString(reverse("HELLO"))
})
radio.onReceivedString(function (receivedString) {
basic.showString(receivedString)
})
The loop runs once for each place in HELLO: i is 0, 1, 2, 3, then 4.
Match each part of a code with its job.
Make the receiver show HELLO again.
In on radio received string, show reverse(received string).
The receiver reverses the text back before showing it.
This ticks by itself when your blocks do what is asked.
Invent a scrambling method of your own as two functions, scramble and unscramble, and send a message through them. Then ask a partner to guess your method from the scrambled text alone. Would it still work once they know it?
scramble: start with an empty result, and loop through the places like reverse does. Join each letter and then an X onto the end (MEET becomes MXEXEXTX). Return the result.
unscramble: the real letters sit at places 0, 2, 4 and so on. Loop i from 0 up to half the length, and join on the letter at place i × 2.
Your own scrambler and unscrambler as two functions: A sends the scrambled text and the receiver shows the unscrambled text.
This ticks by itself when your blocks do what is asked.
Students send a reversed message, see that anyone who knows the method can undo it, and meet the idea of a public method with a secret key. About 40 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
The idea: a cipher is a method for scrambling a message. If the method is the only secret, everything is lost the moment someone learns it. Real ciphers have a public method and a secret key; Caesar's cipher, next, is the smallest example.
Vocabulary: plain text (before), cipher text (after), method (steps), key (the secret value). Use them every time from now on.
Code: this lesson teaches the code every later cipher is built from. Foundations functions were named jobs with nothing in or out; reverse(text) takes a parameter and returns a text. The for index loop visits each place 0 to length − 1, and text.charAt(i) is the letter at place i (counted from 0, like a list). The Investigate trace task (result after i = 0 and 1) checks students can follow it; spend time here, because Caesar, Checksum and the CTF reuse the same shape.
A method everyone knows hides nothing. Ask what a listener who knows the method would need to read the message.
Stretch: Make the scrambler swap each pair of letters (HELLO → EHLLO).
Support: Give scramble complete and ask only for unscramble.
Next: a code with a key, the Caesar cipher.