Project: secure the robot

Authenticate with a password, decrypt an encrypted command, and act on it.

F11.9Cyber securityGCSE25 min

Covers OCR J277 1.4.2 Identifying and preventing vulnerabilities · AQA 8525 3.6.3 Methods to detect and prevent cyber security threats · Edexcel 1CP2 4.2.1 Network security

Do this lesson in the simulator

The radio is a broadcast: anyone on the mat can send the robot a command, and anyone can listen in. That is a security problem. In this project you make the robot authenticate before it takes orders, and encrypt the orders so a listener cannot read the command. It brings together passwords (F11.5), encryption (F11.6) and defensive habits (F11.8).

The protocol

The Base robot follows these rules:

  • It ignores everyone until it hears the right password, bugbot42.
  • Once a robot has given the password, the Base sends it an encrypted command, using a Caesar cipher with a key of 5.
  • The command is FORWARD <cm>: drive that far to reach the depot.

Your robot must:

  1. Send the password.
  2. Wait for the Base's reply.
  3. Decrypt it with the key.
  4. Read the distance and drive that far, into the green depot zone, without hitting anything.

If you send the wrong password, the Base stays silent, and you should not move.

A sequence of radio messages: your robot sends the password, the Base checks it and replies with an encrypted command, your robot decrypts it and drives; a robot with the wrong password gets no replyyour robotBaseanother robot,wrong password1. sends the passwordright password?authentication:no password,no reply2. yes: an encrypted commandencryption: Caesar cipher, key 53. decrypt: shift back 5FORWARD <cm>4. drive that far intothe green depota wrong passwordno replyno reply, so itdoes not move
Read down the page: time runs downwards. The password check is the authentication and the key is the encryption, the two defences this project combines. The Base's silence is the answer to a wrong password, so a robot that hears nothing must not move.

Try the pieces

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

def caesar(text, key):
    out = ""
    for ch in text:
        if ch.isalpha():
            base = ord("A")
            out = out + chr((ord(ch.upper()) - base + key) % 26 + base)
        else:
            out = out + ch
    return out

send("bugbot42")
for tick in range(15):
    wait(0.1)
    for sender, text in messages():
        print("encrypted reply:", text)
        print("decrypted:", caesar(text, -5))

Run this in the simulator

Run it and read the decrypted command. Then write the part that acts on it: split the command into the word and the number, and drive.

Plan it first

  • send the password and wait for the reply (an ask helper, as in lesson F10.6, does both);
  • decrypt the reply with key -5;
  • if there is no reply, stop and turn the LED red;
  • otherwise split it into FORWARD and a number, and forward(50, distance=that number);
  • turn the LED green when you arrive.

Task: secure the robot

Authenticate with the Base using the password bugbot42, decrypt its reply with a Caesar cipher (key 5), and carry out the FORWARD <cm> command to reach the green depot, without collisions. Do not send any command before the password, and do not type the distance: read it from the decrypted message. The caesar helper is provided.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

def caesar(text, key):
    out = ""
    for ch in text:
        if ch.isalpha():
            base = ord("A")
            out = out + chr((ord(ch.upper()) - base + key) % 26 + base)
        else:
            out = out + ch
    return out

def ask(question, seconds=1.5):
    send(question)
    for tick in range(int(seconds * 10)):
        wait(0.1)
        for sender, text in messages():
            return text
    return None

Challenges

  1. Try sending the wrong password first. Does your program correctly refuse to move?
  2. Someone else on the mat is listening. The Base's reply is KTWBFWI 25: what can they read without the key? (Look at what the Caesar cipher leaves alone.) And what did they learn when your robot sent the password?
  3. Encrypt your own reply to the Base to confirm you have arrived, and send it.