Just as a PIN is stronger with an increased number of combinations, so is the cipher. The ascii_shift cipher has more combinations.
To crack the cipher, your script will have to examine more combinations.
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.
# 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)
# 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
Links
[1] https://learn.parallax.com/tutorials/robot/cyberbot/cybersecurity-encryption-intro/ascii-and-other-simple-ciphers