← Teacher guide

Has the message changed?

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

Secret messages · Trust · about 45 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 8 min · Investigate 10 min · Modify 7 min · Make 17 min

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

What you will seeA question to askThe fix
Every message is rejected.Do the sender and receiver use the same secretKey?Use one secretKey value on both micro:bits.
A name longer than 8 letters is cut short.How long is the message?radio.sendValue names are limited to 8 characters. Use shorter messages.

Answers to the checks

  1. Predict: The receiver gets A's message, then B's message (HELLP with HELLO's checksum). What does it show?
    HELLO, then a cross. The first matches its checksum (HELLO adds up to 7). HELLP adds up to 8, so it doesn't match the 7 that arrived.
  2. Working it out: What is the checksum of KEY? (K is 10, E is 4, Y is 24. Add them and keep the last digit.)
    8
    10 + 4 + 24 = 38, and the last digit is 8.
  3. Swapped letters: A message HELLO changes to EHLLO (two letters swap). Does this checksum notice?
    No: the letters add to the same total
    Addition ignores order, so swapped letters give the same checksum. That is a weakness of this simple checksum. Real checksums are sensitive to order.
  4. A deliberate change: Someone changes the message in the air and also works out the new checksum with the same public method. What does the receiver think?
    Nothing wrong: the checksum matches the changed message
    A public checksum protects against accidents, not against a person who deliberately changes the message. If the checksum also needs a secret key, nobody without the key can produce a matching one.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify

The program as blocks
Show as code
let ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let secretKey = 7
function checksum(message: string): number {
    let total = secretKey
    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)
    }
})

Make

The program as blocks
Show as code
let ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let secretKey = 7
let bad = 0
function checksum(message: string): number {
    let total = secretKey
    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("MEET", checksum("MEET"))
})
input.onButtonPressed(Button.B, function () {
    radio.sendValue("MEAT", checksum("MEET"))
})
input.onButtonPressed(Button.AB, function () {
    basic.showNumber(bad)
})
radio.onReceivedValue(function (name, value) {
    if (checksum(name) == value) {
        basic.showIcon(IconNames.Yes)
    } else {
        bad += 1
        basic.showIcon(IconNames.No)
    }
})

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.

Exit ticket

What does a checksum let a receiver notice?

That the message was damaged or changed after the checksum was worked out. A keyed checksum also stops someone making a matching one without the key.

What each step is for

Where it leads

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