Project: secure the robot
Authenticate with a password, decrypt an encrypted command, and act on it.
Do this lesson in the simulatorThe 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 learns nothing. 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:
- Send the password.
- Wait for the Base's reply.
- Decrypt it with the key.
- 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.
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 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
askhelper, as in module 9, does both); - decrypt the reply with key
-5; - if there is no reply, stop and turn the LED red;
- otherwise split it into
FORWARDand a number, andforward(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
- Try sending the wrong password first. Does your program correctly refuse to move?
- Someone else on the mat is listening. Explain what they learn from the Base's reply, and why it does not help them.
- Encrypt your own reply to the Base to confirm you have arrived, and send it.