Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Cybersecurity: Sniffing Attacks and Defenses

Cybersecurity: Sniffing Attacks and Defenses

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()

 


Printer-friendly version
Encrypt Your App Data
Prev
Try This: Monitor Encrypted Text
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress