Caesar Cypher Python3

Veni, vidi, et coded

Let’s get down to business. You are looking for a Python solution to generate a Caesar Cypher. It is the simplest of cyphers, reportedly used by Julius himself to obfuscate confidential messages.

Basically it is an alphabet shift routine using a secret number. It is the great grand daddy of cryptography and here it is in Python3:

def caesar_code(message, key):
    message = str(message)
    key = int(key)
    alphabet1 = ''.join(chr(x) for x in range(32, 127))
    alphabet2 = alphabet1[key:]+alphabet1[:key]
    coded_message = ''
    for i in range(len(message)):
        x = alphabet1.find(message[i])
        coded_message += (alphabet2[x])
    print(coded_message)

Or at least, my version of it!

It is a function which takes as arguments, the message to be encoded and the key (an integer) to offset the letter in the message by.

That is. If the message is ‘aaa’ and the key is 1. The encoded message will be ‘bbb’ Simples!

You can get these data from the user by simple terminal Python code such as:

message = str(input('Enter your message…'))
key = integer(input('Enter an integer number to encode the message…')

In line 4, we use all the UTF characters between 32 and 127 to create an alphabet. Python uses the name ‘chr’ to access UTF characters. We join all the characters to create a string which we can find our message characters in. ‘a’ is 61 for instance.
Line 5 creates a second alphabet using string slicing. alphabet1[key:] returns every character after the integer, key’
alphabet1[:key] returns all characters up to key.
As an example with alphabet1 being a ‘a..z’ and the key being 2 we would get ‘c..z’ and ‘ab’.
Concatenate these two strings to give alphabet2. We get: ‘cdefghijklmnopqrstuvwxyzab’.
Line 6 just sets a variable, coded_message, as an empty string we can append to.
Line 7 starts a ‘for’ loop with a range of the length of the message and finds the integer location of the character in our message in alphabet1. i.e. ‘a’ = 1. This letter, with a key of 2 would give the location of ‘a’ as character 1 of alphabet2. Which is ‘c’. And so on until our message has been encoded.
Line 10 just prints the coded_message variable.

There are other ways to accomplish the same task using such ‘rot_13’ or ‘str.maketrans’. These rely on importing libraries and do not give a basic understanding of the problem you are trying to solve with Python.

In the next exciting episode I will show you how to build a GUI for the Caesar Cypher using tkinter.

Like me, you can learn how to use Python Classes in tkinter and how to set up all the fiddly bits like fonts, canvases, frames and buttons.

Learn how to get variables into your app and use them in your code

If you have your own code which you would like to share, then please comment and submit.

In the meantime: b{++4:j-“-{(($)”:2$/#:j4/#)M

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s