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:
Receiver:
Before adding encryption, it’s best to make sure the original app is working.
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.
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()
# 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()
Links
[1] https://learn.parallax.com/tutorials/robot/cyberbot/cybersecurity-radio-data/send-and-receive-packets