Encrypt and Decrypt your Radio Data

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 sender micro:bit’s serial monitor and see the countdown results in the receiver micro:bit’s serial monitor.

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: Go to ASCII and Other Simple Ciphers, and copy the ascii_shift function from the ascii_shift_cipher script and paste it into the countdown transmitter and receiver scripts, just below their 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:
  • Set the project names to countdown_sender_encrypted and countdown_receiver_encrypted.  
  • Save the modified scripts.
  • Click Send to micro:bit both micro:bit Python Editors.
  • 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()