← Secret messages

Scrambling is not a secret

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:

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.

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
scramble.ts
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)
})
A is pressed. What does the other micro:bit show?

RunRun it and compare the result with your prediction.

Run

The program as blocks
Show as code
scramble.ts
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)
})
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
scramble.ts
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.

In reverse("HELLO"), what is result after the loop has run for i = 0 and then i = 1?

Sam knows the sender reverses every message. A message arrives: OLLEH. What does Sam do to read it?

Match each part of a code with its job.

The method
The key
The plain text
The cipher text

Which is the better rule for a code?

ModifyChange the code on purpose and check the result.

Modify

Make the receiver show HELLO again.

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?

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.

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

Make

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?

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?

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.

Another hint

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.

For teachers

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.

Students will be able to

Before class

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.

Running the lesson

  1. Predict: students expect HELLO. Ask: where in the program is the reversing undone?
  2. Make: let pairs swap and guess. The point is that a guess at the method is cheap.

The hard part (on purpose)

A method everyone knows hides nothing. Ask what a listener who knows the method would need to read the message.

When students get stuck

Stretch and support

Stretch: Make the scrambler swap each pair of letters (HELLO → EHLLO).

Support: Give scramble complete and ask only for unscramble.

Where it leads

Next: a code with a key, the Caesar cipher.

What each step is for

Now you can
Next lesson3. The Caesar cipherShift every letter along the alphabet by a secret number. The method is public; the number is the key.