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: Brute Force Attacks & Defenses

Cybersecurity: Brute Force Attacks & Defenses

How Using the Random Key Works

How It Works: radio_send_images_caesar_key_unknown

This section only explains modifications that make it use a random key and send strings at a slower rate.  For info on the program before modifications, check Share Something Personal – Encrypted.

Adding import random makes the random methods available so that the script can use random.randint().

Adding key = random.randint(1, 25) sets the key equal to a randomly generated integer in the 1 through 25 range.  This statement was placed above the while True: loop so that it gets executed before the main loop starts sending repeated encrypted messages.  That way, the script sets the key value once, and then uses it with every repetition of the while True: loop.  

Each time you restart the micro:bit, there is a 1 in 25 chance you will get the same key.  Conversely, there is a 24 out of 25 chance that the key will be different.

In the main loop, changing packet = caesar(3, packet) to packet = caesar(key, packet) makes it so that the main loop repeatedly uses the one key value that was randomly determined before the main loop started repeating.

The sleep(2500) was changed to sleep(6000).  Intentionally slowing down the broadcast rate gives the online editor enough time to take a 0.2 s break between each line printed.  This will reduce the WebUSB tendency to make printing errors.

How It Works: radio_receive_images_caesar_brute_force

The brute force receiver removed the statements that assumed key was 3 and displayed it.

        # packet = caesar(-3, packet)             # <- comment
        # print("packet:", packet)                # <- comment
        # display.show(getattr(Image, packet))    # <- comment

In place of those three statements, it uses a loop that decrypts and prints the decrypted packet’s result to the terminal with each of the 25 possible keys.  The for key in range(-1, -26, -1)  loop repeats the indented-below statements with key set to -1, then -2, -3, and so on up through -25.  Each time through the loop, result = caesar(key, packet) decrypts the packet for that repetition’s key value.  Then, print(“key:”, key, “result:”, result) displays the key and decrypted string for each iteration of the loop.  

        for key in range(-1, -26, -1):            # <- add
            result = caesar(key, packet)          # <- add
            print("key:", key, "result:", result) # <- add
            sleep(200)                            # <- add
        print()                                   # <- add

Lastly, sleep(200) delays before repeating the loop to give the online editor’s WebUSB connection extra time to transfer information to the terminal (since it seems to need it at the time of this writing).  After all 25 loop repetitions, print() displays an empty line before the while True: loop repeats and another 25 keys and decrypted strings are displayed.

 


Printer-friendly version
Crack a Cipher with Brute Force
Prev
Try This: More Combinations
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress