← Secret messages

One key to lock and unlock

XOR mixes a number with a key. Doing it again with the same key brings the number back, so one shared key works in both directions.

Most of the secret messages on the internet are locked with a key that both ends already share. The locking step is much like this lesson, only with far bigger numbers.

A computer stores numbers as patterns of bits (0s and 1s). XOR (written ^) compares two numbers bit by bit: where the bits differ the answer has a 1, where they match it has a 0.

The program as blocks
Show as code
  7 = 111
  5 = 101
7 ^ 5 = 010 = 2

Now the useful part: do it again with the same key and you get the original back. 2 ^ 5 = 7. So the sender computes message ^ key, and the receiver computes received ^ key. One shared key, one operation, both directions.

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
xor.ts
let key = 5
radio.setGroup(5)
input.onButtonPressed(Button.A, function () {
    radio.sendNumber(7 ^ key)
})
radio.onReceivedNumber(function (receivedNumber) {
    basic.showNumber(receivedNumber ^ key)
})
A is pressed. The radio carries a number. What does the receiving micro:bit show?

RunRun it and compare the result with your prediction.

Run

Open Show data in the simulator and find the number the radio carried.

The program as blocks
Show as code
xor.ts
let key = 5
radio.setGroup(5)
input.onButtonPressed(Button.A, function () {
    radio.sendNumber(7 ^ key)
})
radio.onReceivedNumber(function (receivedNumber) {
    basic.showNumber(receivedNumber ^ key)
})
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
xor.ts
let key = 5
radio.setGroup(5)
input.onButtonPressed(Button.A, function () {
    radio.sendNumber(7 ^ key)
})
radio.onReceivedNumber(function (receivedNumber) {
    basic.showNumber(receivedNumber ^ key)
})

What number does the radio carry when the sender encrypts 6 with key 3? (6 is 110 in bits and 3 is 011.)

The sender encrypted 6 with key 3, so the radio carries 5. A receiver wrongly uses key 4. What does it show?

Why does the receiver use the very same operation the sender used?

ModifyChange the code on purpose and check the result.

Modify

Let B change the key and show it. Set the key the same on both micro:bits, then press A. Then make the keys different. What happens?

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?

B adds one to key (back to 1 after 15) and shows it. Make both micro:bits agree before pressing A.

B changes the key and shows 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

Need a hint?

A variable pin holds 4271; A sends pin ^ key by radio.

Another hint

On radio received number, show received number ^ key.

Send a four-digit PIN, locked with a shared key, to a second micro:bit that unlocks and shows it.

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

A door keypad sends a PIN locked with the key; the receiver unlocks it and shows it.

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

Press A three times. The radio carries the same number each time. Why might that be a problem for a door keypad?

For teachers

Students lock and unlock numbers with XOR and a shared key, and meet the weakness of sending the same locked message every time. About 45 minutes.

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

Students will be able to

Before class

The idea: XOR gives a 1 where two bits differ and a 0 where they match. Doing it twice with the same key returns the original, so one key and one operation work in both directions. This is a shared-key (symmetric) cipher.

Bits: the lesson text shows 7 ^ 5 in bits. If students have not met binary, treat the working as a table to read, not something to learn: the check answers can be found by following the pattern.

Two honest limits: (1) XOR with a short key repeated is breakable; real systems use long keys and better methods. (2) The same message gives the same locked number every time, which the last Make task shows. Both are why the next lessons matter.

Running the lesson

  1. Investigate: let students do the bit working on paper first.
  2. Modify: wrong keys. Ask: does the receiver know something is wrong?

The hard part (on purpose)

XOR undoes itself with the same key, and a wrong key gives a wrong answer, not an error. Ask them to compare the keys on both micro:bits first.

When students get stuck

Stretch and support

Stretch: Lock each digit of the PIN separately, using a different key digit for each.

Support: Give the sending half complete.

Where it leads

Next: both ends need the same key. How do they agree it without sending it?

What each step is for

Now you can
Next lesson6. Agreeing a key in the openBoth ends need the same key, but anything sent by radio is public. Each end mixes a private number with public numbers, swaps the mixes, and both arrive at the same key without it ever being sent.