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

ASCII and Other Simple Ciphers

The simple ciphers we examine in this tutorial are called monoalphabetic substitution ciphers, where one character always maps to some other letter in the adjusted alphabet. 

In addition to the Caesar cipher, you will see names like Atbash, Affine, Beaufort, Hill, and others. Here, you can experiment with Atbash, mixed alphabet, and a very useful variation on the Caesar shift cipher that uses ASCII characters. 

ASCII Shift Cipher

The Caesar cipher works well as an introduction to ciphers, but it’s not overly practical. With only 25 keys and every word separated by a space, it’s definitely one of the easiest ciphers to crack. The Caesar cipher is also not a very good fit for encrypting radio data since CAPS LOCK letters with no other characters or spaces would make a project to send an encrypted version of this dictionary really difficult:

{‘start’ : 3, ‘after’ : ‘Liftoff! ‘}

The ASCII Shift Cipher works on all printable characters, including spaces, so that dictionary string would be no problem to encrypt and decrypt with ASCII Shift. Although it’s still considered very weak in the encryption world, 93 different keys is still more secure than 25.

Example project: ascii_shift_cipher

  • Enter ascii_shift_cipher, then flash it into the micro:bit.

  • Open the terminal, make sure local echo is checked, and follow the prompts. Valid keys are from -93 to 93.
  • Try encrypting Cases, spaces, and punctuation! with the key set to 5.
  • Try decrypting Hfxjx1%xufhjx1%uzshyzfynts& with the key set to -5.
  • Try encrypting and decrypting a string like: {‘start’ : 3, ‘after’ : ‘Liftoff! ‘}

How It Works: ascii_shift_cipher

Assuming you are just making use of the function, all your project has to do to encrypt some text is this:

Like the caesar function, ascii_shift also has arguments for a key and text parameters that accept strings. Unlike the caesar function, the string you pass to text can contain all printable characters including spaces, digits, punctuation, and other keyboard symbols. The ascii_shift function also has a for letter in text loop that does the shift operation on each character and adds it to a result string, which was declared as an empty string variable before the loop.

Inside that function, the main difference is this statement:

Those four blocks do the same job as these seven:

Unlike the Caesar cipher, which started with characters indexed as 0…25, the printable ASCII characters are in a range of 32…126. The modulus operator trick and negative value trick won’t work right if your alphabet doesn’t start with an index of 0. So, it has to subtract 32 before both the set ascii to (remainder of (ascii/94)) block and the if (ascii < 0) then add 94 block, then add 32 afterwards. Here is a breakdown of each step:

  1. Get the letter’s ASCII value with set ascii to (char code (letter) at (0))
  2. Subtract 32 to shift the index from 32…126 to 0…93 with set ascii to (ascii – 32). 
  3. Add the key with set ascii to (ascii + key).
  4. Circular shift any index values above 93 around to continue from zero with set ascii to (ascii % 94).
  5.  Check if the ascii value is negative or positive with the if (ascii < 0) then.
  6. If it is negative, circular shift any index values above 0 around to continue from 93 with change ascii by 94
  7. Make sure the result goes back to the 32…126 range with set ascii to (ascii + 32). 

Printer-friendly version
Caesar Cipher in a Function
Prev
Substitution 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