← Secret messages

Has the message changed?

A checksum is a small number worked out from a message and sent with it. If the number the receiver works out is different, the message was damaged or changed on the way.

A bar code, a bank card number and a downloaded file all carry a small check number. If one digit is wrong, the scanner or the browser notices and refuses to trust it.

Radio can garble a message (a bit flips in the air), and a message can be changed on purpose. Encryption keeps a message private; it does not tell you it is unchanged. A checksum is the tool for that.

The sender works out a small number from the message (here: add up each letter's place in the alphabet, keep the last digit) and sends it with the message. The receiver works it out again. If the two differ, something changed.

send value sends a name and a number together (Agreeing a key in the open). Here the name is the message itself and the number is its checksum, so each message can be 8 characters at most: a longer one is cut short on the way and fails its own checksum.

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
checksum.ts
let ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
function checksum(message: string): number {
    let total = 0
    for (let i = 0; i < message.length; i++) {
        total += ALPHABET.indexOf(message.charAt(i))
    }
    return total % 10
}
radio.setGroup(5)
input.onButtonPressed(Button.A, function () {
    radio.sendValue("HELLO", checksum("HELLO"))
})
input.onButtonPressed(Button.B, function () {
    radio.sendValue("HELLP", checksum("HELLO"))
})
radio.onReceivedValue(function (name, value) {
    if (checksum(name) == value) {
        basic.showString(name)
    } else {
        basic.showIcon(IconNames.No)
    }
})
The receiver gets A's message, then B's message (HELLP with HELLO's checksum). What does it show?

RunRun it and compare the result with your prediction.

Run

B pretends the message was damaged on the way: the text changed but the checksum is still the old one.

The program as blocks
Show as code
checksum.ts
let ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
function checksum(message: string): number {
    let total = 0
    for (let i = 0; i < message.length; i++) {
        total += ALPHABET.indexOf(message.charAt(i))
    }
    return total % 10
}
radio.setGroup(5)
input.onButtonPressed(Button.A, function () {
    radio.sendValue("HELLO", checksum("HELLO"))
})
input.onButtonPressed(Button.B, function () {
    radio.sendValue("HELLP", checksum("HELLO"))
})
radio.onReceivedValue(function (name, value) {
    if (checksum(name) == value) {
        basic.showString(name)
    } else {
        basic.showIcon(IconNames.No)
    }
})
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
checksum.ts
let ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
function checksum(message: string): number {
    let total = 0
    for (let i = 0; i < message.length; i++) {
        total += ALPHABET.indexOf(message.charAt(i))
    }
    return total % 10
}
radio.setGroup(5)
input.onButtonPressed(Button.A, function () {
    radio.sendValue("HELLO", checksum("HELLO"))
})
input.onButtonPressed(Button.B, function () {
    radio.sendValue("HELLP", checksum("HELLO"))
})
radio.onReceivedValue(function (name, value) {
    if (checksum(name) == value) {
        basic.showString(name)
    } else {
        basic.showIcon(IconNames.No)
    }
})

What is the checksum of KEY? (K is 10, E is 4, Y is 24. Add them and keep the last digit.)

A message HELLO changes to EHLLO (two letters swap). Does this checksum notice?

Someone changes the message in the air and also works out the new checksum with the same public method. What does the receiver think?

ModifyChange the code on purpose and check the result.

Modify

Mix a secret key into the checksum. Both micro:bits know it, and nobody else does, so only they can work out a matching number.

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?

Make a variable secretKey = 7, and start total from secretKey instead of 0.

The checksum starts from a secret key.

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

Make a message checker: the receiver shows a tick for a good message and a cross for a bad one, and A+B shows how many bad messages have arrived.

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?

A sends MEET with its checksum; B sends a damaged MEAT with MEET's checksum.

Another hint

On receiving, show a tick when the checksum matches; otherwise add one to bad and show a cross. A+B shows bad.

A sends a good message and B a damaged one; the receiver shows a tick or a cross, and counts the bad ones.

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

For teachers

Students send messages with a checksum, see the receiver reject a changed one, and learn why a keyed checksum is needed against deliberate changes. About 45 minutes.

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

Students will be able to

Before class

The idea: privacy (encryption) and integrity (nobody changed it) are different jobs. A checksum is a small number worked out from the message and sent with it; a mismatch means the message changed.

Everyday picture: counting the cards when you hand over a pack. Say there are 52; if the receiver counts 51, something is missing. Where it breaks: if someone swaps a card for another, the count is still 52.

Honest limits: this checksum is deliberately simple. It ignores the order of letters (Investigate task 2), only has ten values (a changed message matches by chance one time in ten), and when the method is public anyone can make a matching one (task 3). Real systems use hash functions and a keyed checksum called a MAC.

Code: radio.sendValue(name, value) sends text and a number together, so the message and its checksum travel in one packet. The name is limited to 8 characters.

Running the lesson

  1. Investigate task 2: have two students swap letters and check the sum by hand.
  2. Ask: how would you make the check number more sensitive to order? (Multiply each place by its position.)

The hard part (on purpose)

A plain checksum catches accidents but not deliberate changes. Ask what an attacker would need to make a changed message pass.

When students get stuck

Stretch and support

Stretch: Make the checksum depend on order: add the letter's place times its position in the message.

Support: Give the receiving checker complete and ask students to write only the sender.

Where it leads

Next: prove who is sending, not just that the message is whole.

What each step is for

Now you can
Next lesson8. Prove it with a challengeA password sent again and again is the same every time. A challenge that changes each time makes each answer good for one use only.