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

How Encrypted Texting Works

Now that the scripts are sending different case letters, spaces and punctuation, it’s time to choose a different cipher.  Reason being the caesar() function cannot accommodate those extra characters without modifications, but the ascii_shift() function can.  It was introduced in the Cybersecurity: Encryption Intro tutorials’ ASCII Shift Cipher activity.

After the import statements, the ascii_shift() function is added.  With def ascii_shift(key, text), it accepts key and text parameters and returns the encrypted result.  Most importantly, the text can contain any printable ASCII character.  

from microbit import *
import radio

''' Function converts plaintext to ciphertext using key '''
 
def ascii_shift(key, text):
    result = "
    for letter in text:
        ascii = ( ord(letter) + key - 32 ) % 94 + 32
        result = result + chr(ascii)
    return result

 

After the ascii_shift() function, the script starts with the usual radio.on, radio.config, and sleep calls along with a couple print statements to help you identify which micro:bit is running which version of the script.

''' Script starts from here... '''

radio.on()
radio.config(channel=7)

sleep(1000)

print("micro:bit transceiver A")
print()

 

Before the main loop, the script prompts you for an encryption key.  So long as both transceivers are using the same key, the messages sent will be faithfully reproduced by the receiver and vice-versa.

text = input("Enter key: ")
key = int(text)

 

These are messages similar to the original unencrypted radio texting app.  Without them, an empty terminal might be confusing, so the prompts are intended to encourage users to start typing messages.

print()
print("Type messages, press enter to send.")
print("Received messages will also be displayed.")
print()

 

Inside the main loop, the script checks if you have typed any characters in the terminal.  If you have, an input statement captures what you typed.  Before transmitting it with radio.send, it does the extra step of tx = ascii_shift(key, tx) to encrypt the string you typed.

while True:

    if uart.any():
        tx = input("Send: ")
        tx = ascii_shift(key, tx)
        radio.send(tx)

 

The main loop also checks for messages from the radio.  When one is received, it is stored in the message variable.  At this point, message is ciphertext, which has to be decrypted.  That’s what message = ascii_shift(-key, message) does, returning the plaintext message.  …and after print(“Receive: “, message), you’ll be able to see and read it in the terminal.

    message = radio.receive()
    if message:
        message = ascii_shift(-key, message)
        print("Receive: ", message)

 


Printer-friendly version
Texting with Terminals: Unencrypted vs. Encrypted
Prev
Encrypt Your App Data
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress