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 the Scrambled Alphabet Cipher Works

The scrambled alphabet cipher has two arguments, text and encrypt.  In this case, the cipher accepts upper-case characters or words.  The encrypt argument accepts True or False.  True encrypts, and False decrypts.  Inside the function, there is an alphabet, and a cryptabet.  The cryptabet is a scrambled version of the alphabet.  It also has an empty result string that will be used to build the encryption result.

# Scrambled alphabet cipher.
def scramble(text, encrypt):
    alpha  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    crypta = "PTQGMKCRSVEXADZBJFOWYNIHLU"
    result = "

 

This step is only performed if the function is called with the encrypt argument set to False.  When the function encrypts, it uses alpha and crypta as declared above.  When it decrypts, alpha has to store “PTQGMKCRSVEXADZBJFOWYNIHLU” and crypta has to store “ABCDEFGHIJKLMNOPQRSTUVWXYZ”.  So if encrypt is False, the statements below and indented shuffle the two strings.  It does so by copying the alpha string into temp, then copying the crypta into alpha.  Lastly, temp — which contains the original alpha — gets copied to crypta.

    if encrypt is False:
        temp = alpha
        alpha = crypta
        crypta = temp

 

The encryption loop starts by making sure the letter is upper case with letter = letter.upper().  Then, it finds the index of the letter in alpha with index = alpha.find(letter).  Finally, it looks up the letter with that index in the cryptabet and appends the string result with result = result + crypta[index].

    for letter in text:
        letter = letter.upper()
        index = alpha.find(letter)
        result = result + crypta[index]

    return result

# The script starts executing statements from here.

 

The script starts executing statements here, with the usual sleep(1000) and a message to set your keyboard to CAPS LOCK.

sleep(1000)

print("Set your keyboard to CAPS LOCK.")
print()

 

Inside the main loop, an input statement prompts you to type a CAPS LOCK word and stores the result in a variable named text.  Then, result = scramble(text, True) encrypts the text you typed and stores it in a variable named result, which gets printed.  

while True:
    text = input("Enter a CAPS LOCK string: ")
    
    result = scramble(text, True)

    print("scrambled result =", result)

 

Next, result = scramble(result, False) decrypts the string, and the last print statement displays the original text you entered.

    result =  scramble(result, False)
    
    print("unscrambled result =", result)

 


Printer-friendly version
Strengthen Your Cipher with Substitution
Prev
Try This: Generate a Random Cryptabet
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress