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

Caesar Cipher in a Function

Why move the Caesar cipher routine to a function?  One advantage would be that you can swap it out with other, better encryption functions, or even function/method calls to a module.  As an example, in the next activity, you will replace the caesar function with another one called ascii_shift.  After the function swap, your script will only need one line changed!

Example script: caesar_cipher_function

  • Enter caesar_cipher_function into the micro:bit Python Editor. 
  • Set the project’s name to caesar_cipher_function.
  • Click Save.
  • Click the Send to micro:bit button.
# caesar_cipher_function

from microbit import *

''' Function converts plaintext to ciphertext using key '''

def caesar(key, word):
    alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    result = "

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

''' Script starts from here... '''

sleep(1000)

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

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

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

    result = caesar(key, letters)
    
    print("result:", result)
    print()

The functionality is identical to the previous example (caesar_terminal_words).

  • Open the serial monitor, and follow the prompts.
  • Verify that the functionality matches the previous script.
  • Take a look at the actual script you have to work with, it’s the part below the ’’’ Script starts from here… ’’’ comment. 

See how all that script has to do is use result = caesar(key, letters) to encrypt the word?

How It Works

Your script now gets the ciphertext from this one line:

    result = caesar(key, word)

The caesar function has two parameters, key and word.  Keep in mind that word could also be ciphertext that you are converting back with a negative key value.  Inside the function, it does all the same steps as caesar_terminal_words, and then returns the result.

def caesar(key, word):
    alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    result = "

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

Printer-friendly version
Apply Caesar Cipher to Words from Serial Monitor
Prev
ASCII and Other Simple Ciphers
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress