Caesar Cipher | Python

TRY IT HERE

A Caesar Cipher is what’s on the menu! However, you may be asking yourself… ‘What is a Caesar Cipher?’ Well, it definitely doesn’t have anything to do with salad! For the uninitiated, a Caesar Cipher (also known as a ‘shift cipher’) is one of the most basic types of encryption techniques and gets its name from Julius Caesar, who was known to use this method in his own correspondence. The cipher takes an input of plain text and a defined number of spaces to shift each letter.

For example, if the plain text message is ‘ABC’ with a shift right of 3, the text would be ‘encrypted’ into cipher text ‘DEF’. In contrast, you can then ‘decrypt’ the cipher text by using the same shift (but to the left), to convert the cipher text back to plain text ‘ABC’.

When two parties agree upon a secret number to use, they can encrypt and decrypt messages to each other using this program. Let’s take a look at how I accomplished this…

First, I imported a module called ‘art’ which holds the fun Caesar Cipher graphic that I am using as the header for this program. I also declared a list called ‘alphabet’ which holds each character A through Z.

Next, I defined a function called caesar() which takes the three arguments of ‘text’, ‘shift’, and ‘direction’. Within this function, I declared a variable ‘end_text’ which holds an empty string.

The first thing that occurs is a check to see if ‘direction’ is set to ‘decode’. If so, the function will take the value of ‘shift’ and multiply it by -1 converting the shift number to the same value, but negative. Since a negative valued index shifts to the left and a positive to the right, this is an easy way for the same function to both decode and encode the message.

If the direction is set to ‘encode’ or after the ‘decode’ shift number is converted to negative, the program advances to the ‘for’ loop. Here is where the encoding and decoding process actually occurs. For each character in the entered text, the loop first checks whether the character is held in the list ‘alphabet’. If so, it gets the current index of the character and assigns it to the ‘position’ variable. A new variable called ‘new_position’ is then assigned with the sum of the values from ‘position’ and ‘shift’. The variable ‘end_text’ is then appended with the character in the ‘alphabet’ list at the index held in ‘new_position’.

If the character is not listed in the ‘alphabet’ list, such as spaces, punctuation, numbers, and special characters, the ‘else’ statement will run which will append the value to the ‘end_text’ variable. This enables the program to keep these characters so it can handle full bodies of text and not singular words.

Finally, the resulting final value of ‘end_text’ is printed to the terminal.

Next, I created a variable titled ‘restart’ and set it to the Boolean value of True. As long as this value remains True, the entire program will continue to loop which enables the user to encode and decode as many messages as they wish without restarting the program each time.

The next four lines are the Caesar Cipher logo and three prompts from the user all assigned to the variable names passed as arguments to the caesar() function earlier. However, you may notice the last line ‘shift = user_shift % 26’. The modulo (%) operator performs a division operation and returns the remainder value. So, by assigning ‘shift’ with the remaining value of ‘user_shift’ divided by 26, we can ensure that the shift value is always within the 26 possible index positions. Otherwise, any value held in ‘shift’ above 25 would throw an index error.

After the user is prompted with the three inputs, the caesar() function is called and the arguments are passed in for processing.

Finally, we prompt the user if they would like to use the program again. If the user enters ‘no’, the ’restart’ variable is assigned with Boolean value False, and the loop is ended with ‘Goodbye’ printed. Otherwise, the loop is started over and the user is prompted for new information to be processed.


You can view and download the source code for this project on my GitHub by clicking here. Thank you for your time!

Previous
Previous

Blind Auction Tool | Python

Next
Next

Hangman Game | Python