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

Encrypt and Decrypt with Serial Monitor

Modifying the key = and letter = statements to encrypt and re-flashing a script to get a ciphertext character isn’t very efficient.  It would be much easier for your script to pass a function, a key, and a word to a function and let it encrypt or decrypt the whole string.  Getting there can be broken into three steps:

  1. Get it to work with user-entered characters in a loop
  2. Add a loop to make it perform the cipher on all characters in a word
  3. Move the working code from the main loop into a function

Another reason for setting it up this way is that you can replace a Caesar cipher function with a different encryption function, and the main script might only need one line modified to get it to work.

Caesar Cipher on Terminal-Entered Characters

In this example, the terminal will prompt you for a key and a letter, and then display the Caesar cipher result.  Previously, the encryption key was hard-coded as 5.  With this script, you can enter 5, or 13 for ROT-13, or any other value you decide to use.  Next, enter the letter to encrypt, and the script displays the ciphertext result in the terminal.

Example script: caesar_terminal_letters

  • Enter caesar_terminal_letters into the micro:bit Python Editor. 
  • Set the project’s name to caesar_terminal_letters.
  • Click Save.
  • Click the Send to micro:bit button.
# caesar_terminal_letters
 
from microbit import *
 
sleep(1000)
 
print("Set your keyboard to CAPS LOCK.")
print()
 
while True:
    text = input("Enter key: ")
    key = int(text)
 
    letter = input("Enter a letter: ")
    
    letter = letter.upper()
 
    alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
    index = alpha.find(letter)
    index = index + key
    index = index % 26
 
    result = alpha[index]
 
    print("result =", result)
    print()
  • Make sure your keyboard is set to CAPS LOCK.  (Or you will have to hold down the SHIFT key as you type letters.)
  • If the serial monitor is not already open, click Show serial, then click in the serial monitor.
  • Follow the prompts, typing in a key number and a letter to encrypt.  Valid key numbers are from -25 to 25.
  • Try encrypting a character, and then decrypting with the negative value of the key you encrypted with. 
    Example: Encrypt A with 13, then decrypt N with -13 to get back to A.

How caesar_terminal_letters Works

The techniques for building this script make use of:

  • Terminal with input statements: Input Messages
  • Looping with while True: Count and Repeat
  • Caesar cipher: Encrypt Letters with a Caesar Cipher Script

Instead of hard coding key = 5 and letter = “M”, like in caesar_cipher_letter, this script makes it work during runtime with:

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

    letter = input("Enter a letter: ")

Before doing the Caesar cipher, it uses letter = letter.upper() which changes any lower-case letter to upper-case.  …just in case you forgot to set your keyboard’s CAPS LOCK.


Did You Know?

These steps from the previous example sketch can be performed in one line:

    index = alpha.find(letter)
    index = index + key
    index = index % 26

Here’s how it would look:

    index = ( alpha.find(letter) + key ) % 26

This more compact format will be used in the next example script.


 


Printer-friendly version
How Caesar Letter Encryption Works
Prev
Apply Caesar Cipher to Words from Serial Monitor
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress