Try This: More Combinations

Just as a PIN is stronger with an increased number of combinations, so is the cipher.  The ascii_shift cipher has more combinations.  

  • Change the radio_send_images_caesar_key_unknown script's project name to radio_send_images_caesar_key_unknown_try_this.
  • Change the radio_receive_images_caesar_brute_force script's project name to radio_receive_images_caesar_brute_force_try_this.
  • Replace the caesar() functions in both scripts with the ascii_shift() function from the Cybersecurity: Encryption Intro tutorial’s ASCII Shift Cipher activity.
  • Replace the caesar(key, packet) calls from both programs with ascii_shift(key, packet) calls.  
  • In radio_send_images_caesar_key_unknown_try_this, change  key = random.randint(1, 25) to key = random.randint(1, 93).

To crack the cipher, your script will have to examine more combinations.

  • In radio_receive_images_caesar_brute_force_try_this, change for key in range(-1, -26, -1) to for key in range(-1, -94, -1).
  • You could also comment the while True: statement and outdent everything below and indented from it.  You can then press/release Reset and wait to grab another packet for analysis.
  • Make sure your scripts match the ones below, then click Save in both micro:bit Python Editors.

You might notice an upper-case HAPPY as well as a lower-case happy.  The key that generates the upper-case HAPPY is the correct answer, though either might actually work for a sniffing attacker’s purposes.  This is an additional weakness of the ASCII Shift cipher.  Make sure not to choose a key that will simply encrypt your upper case characters as lower-case or vice-versa.

Try This Transmitter Script :radio_send_images_caesar_key_unknown_try_this.py

# radio_send_images_caesar_key_unknown_try_this.py

from microbit import *
import radio
import random                               # <- add

''' Function converts plaintext to ciphertext using key '''

def ascii_shift(key, text):                 # <- change (try this)
    result = ""                             # <- chante (try this)
    for letter in text:                     # <- change (try this)
        ascii = ( ord(letter) + key - 32 ) % 94 + 32  # <- change (try this)
        result = result + chr(ascii)        # <- change (try this)
    return result                           # <- change (try this)

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

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

sleep(1000)

string_list = ["HAPPY", "SAD", "ANGRY"]

# key = random.randint(1, 25)               # <- add
key = random.randint(1, 93)                 # <- change (try this)

while True:
    
    for packet in string_list:
        print("packet:", packet)
        display.show(getattr(Image, packet))
        
        # packet = caesar(3, packet)             # <- change (before)
        # packet = caesar(key, packet)           # <- change (after)
        packet = ascii_shift(key, packet)        # <- change (try this)
        
        
        print("Send encrypted:", packet)
        radio.send(packet)
        # sleep(2500)                       # <- change (before)
        sleep(6000)                         # <- change (after)

 

Try This Receiver Script: radio_receive_images_caesar_brute_force_try_this.py

# radio_receive_images_caesar_brute_force_try_this.py

from microbit import *
import radio

''' Function converts plaintext to ciphertext using key '''

def ascii_shift(key, text):                 # <- change (try this)
    result = ""                             # <- chante (try this)
    for letter in text:                     # <- change (try this)
        ascii = ( ord(letter) + key - 32 ) % 94 + 32  # <- change (try this)
        result = result + chr(ascii)        # <- change (try this)
    return result                           # <- change (try this)

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

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

sleep(1000)

while True:
    
    packet = radio.receive()

    if packet:
        print("Receive encrypted:", packet)
        # packet = caesar(-3, packet)             # <- comment
        # print("packet:", packet)                # <- comment
        # display.show(getattr(Image, packet))    # <- comment
        # for key in range(-1, -26, -1):          # <- add
        for key in range(-1, -94, -1):            # <- change (try this)
            # result = caesar(key, packet)        # <- add
            result = ascii_shift(key, packet)     # <- change (try this)
            print("key:", key, "result:", result) # <- add
            sleep(200)                            # <- add
        print()                                   # <- add