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: Encryption Intro

Cybersecurity: Encryption Intro

Apply Caesar Cipher to Words from Serial Monitor

This script places the Caesar cipher in a loop.  This will allow you to type entire words for encrypting/decrypting!  It still also works with single characters if that’s all you want to encrypt.  

Example script: caesar_terminal_words

  • Change project’s name from  caesar_terminal_letters to caesar_terminal_words.
  • Update it to match the script below
  • Save the modified script.
  • Click the Send to micro:bit button.
# caesar_terminal_words
 
from microbit import *
 
sleep(1000)
 
print("Set your keyboard to CAPS LOCK.")
print()
 
while True:
    text = input("Enter key: ")
    key = int(text)
 
    word = input("Enter character(s) in A...Z range: ")
 
    alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    result = "
 
    for letter in word:
        
        letter = letter.upper()
        index = ( alpha.find(letter) + key ) % 26
        result = result + alpha[index]
 
    print("result:", result)
    print()
  • Follow the prompts in the serial monitor.  Valid keys are from -25 to 25.
  • Try encrypting HELLO with the key set to 5.
  • Try decrypting MJQQT with the key set to -5.
  • Write a short message for your friend or lab partner and give them the key to decrypt it.

How caesar_terminal_words Works

Inside the while True loop, the script stores a number you enter into an int variable named key.  Then, it stores a word you type in a string variable named word.

    text = input("Enter key: ")
    key = int(text)

    word = input("Enter character(s) in A...Z range: ")

In addition to the alpha (alphabet) string, a second empty string named result is created to store the result.

    alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    result = "

After that, a for… loop goes through each letter in the plaintext string.  After making sure the letter is upper-case, it applies the Caesar cipher with two lines.  The first finds the index of the new character with index = ( alpha.find(letter) + key ) % 26.  The second adds the character to whatever is already in the ciphertext string with result = result + alpha[index].

    for letter in word:
        
        letter = letter.upper()
        index = ( alpha.find(letter) + key ) % 26
        result = result + alpha[index]

Before repeating the loop, the ciphertext is printed to the terminal.

    print("result: ", result)

 


Printer-friendly version
Encrypt and Decrypt with Serial Monitor
Prev
Caesar Cipher in a Function
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress