2 years ago 2 years ago Cryptographic Share

Learn Encryption Concepts with the Historic Caesar Cipher

This is the first type of "secret spy code" I learned as a child, from a book called "The Spy's Guidebook". Years later, I learned that it's called a Caesar Cipher — named after Julius Caesar.

It's also sometimes known as a "shift cipher", since it basically just shifts each letter of the alphabet by a given number of letters. If a letter gets shifted past the end of the alphabet, it just "wraps around" to the start of the alphabet. So (for example) the letter Z shifted by one position would become A, and the letter Y shifted by 3 positions would end up as the letter B.

Decryption is the reverse process.

The version on this page includes the space character as one of the characters to shift. This means that spaces will appear in different places to the original sentence (that is, all the words in the ciphertext won't have the exact same number of letters as in the original plaintext).

ROT13

One of the most often-used key numbers for the Caesar cipher is 13, so each letter is shifted by 13 positions. This is convenient since there are 26 letters in the English alphabet, which means that the same shift to encrypt will also decrypt, no matter which direction you shift the letters in (up or down). This algorithm is often referred to as "ROT13" where the ROT stands for "rotation".

Python Code for the Caesar Cipher (Including Spaces)

This version of the cipher includes the space character as if it's a letter, which makes the ciphertext a bit harder to crack. Have a think about why this would be the case.

"""
This program implements a Caesar cipher algorithm.
It includes spaces as a valid alphabetic character, so that 
everything is modulo 27 (rather than the usual 26), which
allows for spaces in the text and handles them better than 
just leaving the spaces in the original positions in the words

A disadvantage of this is that spaces might end up as the first
and/or last characters of the ciphertext. For this reason, quote
marks are displayed around the text. A better treatment could be 
done with further development work to handle this better.
"""

# Encryption Stage 

plaintext = input("Enter the plaintext string here: ").lower()
key = input("Enter your key: ") 
alphabet = "abcdefghijklmnopqrstuvwxyz "
ciphertext = ""

#The key (i.e. the shift value) must be an integer
while not key.isnumeric():
    # Re-enter the key
    key = input("Enter your key (must be a whole number): ")

# Although a Caesar cipher is obviously not the most difficult to
# type to break, here allowance is made for any integer value of 
# the key, so that a long key like 21357 may be used which may help
# to obfuscate something, as compared to restricting to keys of 
# only from 1 to 25 which would make the type of cipher
# more clearly advertised to anyone who sees the key

key = (int(key))

for i in plaintext:   
    new_ind = (alphabet.index(i) + key) % 27 
    ciphertext += alphabet[new_ind] 
           
print("The ciphertext is: '" + ciphertext + "'")

# Decryption Stage 

ciphertext = input("\nEnter the ciphertext string here: ").lower()
key = input("Enter your key: ")
plaintext = ""

#The key (i.e. the shift value) must be an integer
while not key.isnumeric():
    # Re-enter the key
    key = input("Enter your key (must be a whole number): ")
key = int(key)

for i in ciphertext:
    new_ind = (alphabet.index(i) - key) % 27 
    plaintext += alphabet[new_ind]  
    
print("Original plaintext string is: '" + plaintext + "'")


Code Colouring

The PHP code colouring for the display on this page was done using highlight.js. The colour theme used here is a slightly modified version of the theme called 'a11y-dark.min.css', which is one of the files that comes with the highlight.js package when you download it from their website. The modified version adds rounded corners to the dark grey background. I called it 'a11y-dark_rounded.min.css', you can see it on this site if you click here. The only change I've made (so far) to the original is the border radius of 10px added to the end of the very first line of .css code.

Live Version (Beta)

I'll add a better live working version of this to the page later, and explain more about how the cipher works, and some of its historical uses, and also some of its uses in learning about how cryptography works.

Run the Cipher On This Page: Encrypting

The version below doesn't handle Python "Input" statements well, but you can still try it. Just enter your plain text string in the first popup box, and then a numeric (whole number) key in the second popup box, and then click "cancel" on the 3rd popup box:


Run the Cipher On This Page: Decrypting

The version below doesn't handle Python "Input" statements well, but you can still try it. Just enter your cipher text string in the first popup box, and then a numeric (whole number) key in the second popup box, and then click "cancel" on the 3rd popup box. (NOTE that the plaintext might sometimes appear under the code example in the box above, not below):


Alternative Method to Run the Python Script

If you want to run this Python code, and you don't have Python installed, you can run it on a website called futurecoder. Highlight, then copy, the whole source code, and then go to the futurecoder.io IDE website and paste (Ctrl-V) the code in. It runs well enough in there (except the lines keep scrolling to the right, which is annoying). Futurecoder is the only web implementation of Python I've found so far that handles the input() function correctly. Though I think it may not work in Safari on a Mac.

I'll do a better version of the cipher online soon. And also look into more ways you can run the code for yourself (other than using futurecoder.io).

How to Crack the Caesar Cipher

You can try to make up some messages with this, or get a friend to make some up, and then practice trying to decrypt them.

Compared to modern computer-based cryptographic algorithms, the Caesar cipher is extremely simple and weak. Several factors can be identified within it which make it easy to crack. Yet, back in ancient times, it was regarded as quite secure. Julius Caesar was known to use the cipher with a shift of 3, so A would encode to D, B would become E, C would become F, etc. One of the reasons it was easier back then to get away with using such a weak (by modern standards) cipher is that so few people could even read or write at all. And there were many local languages used, so it would be assumed that a long-distance message containing unreadable characters was just written in a different language.

Some of the specific reasons which make the Caesar cipher weak are (and you can try some of these out in various ways to see if they help you do decrypt messages that have been encrypted with it):

  • Every letter of the plaintext (i.e. original text) always encodes to the same encrypted letter.
  • This also means that entire words always encode to the same word, i.e. the word "the" will always be the same three letters for any particular key (shift value).
  • Some letters in English are much more commonly used than others (e.g. "e" is the most popular), so analysing a large block of encrypted text just by counting the number of letters can give a very good (if not complete) clue as to what many of the letters are.
  • Since there is only one shift value for any given message, it's only needed to know what just one character of original text encodes to — and from that you can work out the shift value, and from that you can decrypt the entire message.
  • There are other weaknesses, I'll write more later. These are just the first I could think of immediately.

Using the Caesar Cipher to Help Learn Computer Programming

You can modify the Python code above to try out different improvements and functions to the encryption algorithm.

Also, you could re-write the cipher using a different language (especially if you don't know Python). A JavaScript version which ran in a web page would be a good one to try to make. I might do one of these sometime and put it on this page.

More examples of this to follow soon...

Cover image by Shutterstock The Enigma Cipher Coding Machine from World War II.

Categories Cryptographic,Security
Byte.Yoga Homepage - Australian Cyber Security Web Magazine

Share This Page

If you liked this page, please share it with others! You can use the links to share on Facebook, Twitter, LinkedIn, Pinterest, and Email. Ther is also an RSS feed to get updates for the website.