When you have a working radio data app, it only takes three steps to update the script so that it transmits encrypted data. Likewise, it only takes three steps to update a receiver script to decrypt the data.
Transmitter:
- Step 1: Add the cipher() function below the transmitter script’s import statements.
- Step 2: Set an encryption key with a statement like key = 5.
- Step 3: Call the cipher() function to encrypt the data before sending the packet.
Receiver:
- Step 1: Add the cipher function below the transmitter script’s import statements.
- Step 2: Set a key to decrypt with a statement like key = -5.
- Step 3: Call the cipher() function to decrypt the data after receiving/before using the packet.
Before adding encryption, it’s best to make sure the original app is working.
- Open the countdown_sender and countdown_receiver scripts from Send and Receive Packets.
- Follow the instructions in Send and Receive Packets to the point where you have the countdown app running.
- Test to make sure you can enter an integer and message into the terminal connected to the sender micro:bit’s terminal and see the countdown results in the receiver micro:bit’s terminal.
Now that you’ve got the original app working, follow the steps to update the scripts to send and receive encrypted data. Again, it’s important to follow the steps because you’re on your own when it comes to encrypting the wireless keyboard cyber:bot control.
- Step 1: Copy the ascii_shift function from the ASCII Shift Cipher<link> activity’s ascii_shift_cipher script and paste it into the transmitter and receiver scripts, just below the import statements.
- Step 2: Add key = 5 to the transmitter script and key = -5 to the receiver script.
- Step 3:
- In the transmitter script, add packet = ascii_shift(key, packet) immediately above radio.send(packet).
- In the receiver script, add packet = ascii_shift(key, packet) immediately below and indented from if packet is not None: MAKE SURE TO INDENT PROPERLY. The packet = ascii_shift(key, packet) statement has to be indented further than if packet is not none:
- Flash and test your modified scripts.
- If you have any challenges troubleshooting the scripts, the solutions are below.
Transmitter script: countdown_sender_encrypted# countdown_sender_encrypted.py
from microbit import * import radio # Step 1: Add the cipher function below the transmitter # script’s import statements. (6 statements) def ascii_shift(key, text): result = "" for letter in text: ascii = ( ord(letter) + key - 32 ) % 94 + 32 result = result + chr(ascii) return result # Step 2: Set an encryption key with a statement like key = 5. key = 5 radio.on() radio.config(channel=7,length=50) sleep(1000) print("Countdown App") print("micro:bit sender") while True: text = input("Enter countdown start: ") value = int(text) message = input("Enter message after countdown: ") dictionary = { } dictionary['start'] = value dictionary['after'] = message packet = str(dictionary) print("Send: ", packet) # Step 3: Call the cipher function to encrypt the data # before sending the packet. packet = ascii_shift(key, packet) radio.send(packet) print()
Receiver Script: countdown_receiver_encrypted
# countdown_receiver_encrypted.py from microbit import * import radio # Step 1: Add the cipher function below the transmitter # script’s import statements. (6 statements) def ascii_shift(key, text): result = "" for letter in text: ascii = ( ord(letter) + key - 32 ) % 94 + 32 result = result + chr(ascii) return result # Step 2: Set a key to decrypt with a statement like key = -5. key = -5 radio.on() radio.config(channel=7,length=50) sleep(1000) print("Countdown App") print("micro:bit receiver\n") while True: packet = radio.receive() if packet is not None: # Step 3: Call the cipher function to decrypt the data # after receiving/before using the packet. packet = ascii_shift(key, packet) print("Receive: ", packet) print() print("Parse: ") dictionary = eval(packet) value = dictionary['start'] message = dictionary['after'] print("value = ", value) print("message = ", message, "\n") while value >= 0: print(value) sleep(1000) value = value - 1 print(message) print()