Secret Messages with Python

In this blog post, we are going to be using Python to write a shift cipher so that we can create a secret language that only my friends and I know the key to! To do this, We are going to write two parts of the code: one part to encode (translate normal language into the secret message), and one part to decode (so that my friend can decode the secret message). In a later blogpost, I’ll be adding to this code so we can learn how to figure out a secret message in a shift cypher if we don’t know what the key is!

What is a Shift Cipher?

A cipher is basically just a fancy word for saying a secret or disguised way of writing. There are lots of different reasons that we would want to make a message unreadable — Maybe it’s a bank account number that you don’t want everyone knowing, maybe you’re a spy and need to safely and secretly send messages back to your country, or maybe you just want to write notes in class that your teacher can’t read!

There are LOTS of different types of ciphers. For this blog post, we are going to be using a Caesar shift cipher. It’s called this because it was originally used by Julius Caesar to send secret messages to his troops so that his enemies could not read them if they got ahold of them. Basically, we choose a number (let’s say 3) and we shift the alphabet 3 letters (This is called a key, and is important for whoever is receiving our message to know so that they can decode it). A now becomes D, B now becomes E, C becomes F, and so on. Our original alphabet we can call our plaintext alphabet. The new alphabet that we are going to use to write our secret messages we are going to call our ciphertext alphabet.

190935_orig

Now that you know we have a shift of 3, using the above try to decode this secret message:

L KLG WKH ODVW FXSFDNH LQ WKH OHIW FDELQHW

That took a lot of work to do by hand, didn’t it? This is where Python can come to the rescue to help you quickly and easily write and read ciphertext (you just have to know the key!)

Turning Messages Into Secret Messages

The first thing that we are going to want to do is open up a new python file and save it as shift_cipher.py

At the top, we are going to have to make an alphabet variable, which we will later use to make both our plaintext and ciphertext messages. This variable is going to contain an array that lists off every letter in the alphabet. Each letter should be it’s own string (so put it in quotes!) and separated out by a comma.

alphabet = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’]

As mentioned, there will be two parts to this: the code to encrypt our messages, and then the code to decrypt messages. We are going to first start on the encoding piece. Let create a function called encrypt, which will require two arguments: plaintext and secret_key. In order to encrypt a message, we absolutely need to know the plaintext message and also number to shift our message by (secret_key). Inside of this function, we are going to create a variable called ciphertext and give it a value of empty quotations.

Screen Shot 2019-07-16 at 2.21.42 PM

Next, we are going to create a for loop, that loops through every character in our plaintext message. While we are looping, I only want it to encrypt messages that have only letters (and not numbers or special characters). So, IF the character is in the alphabet, then I want it to change the character to lowercase (if it wasn’t already), I want it to be able to find that letter and its position in our alphabet, and then I want to create a new alphabet based off of the position in the original alphabet, the shift number, and the number of letters in the alphabet. Let’s break that down:

Create your loop we described above.

for character in plaintext:

Inside our loop, add an if statement to check that the characters in the message are in the alphabet. We can do this in python using isalpha()

if character.isalpha():

Next, we are going to make a variable that changes the character to be the lowercase of that character. We can easily do this in python using lower()

character = character.lower()

Next, we are going to make a variable called char_index and give it a value of its position on our alphabet. In python, we can use index() — a method that finds the given element in a list and returns its position.

char_index = alphabet.index(character)

Now let’s create a new variable called new_index — This is going to be that letter’s new position using the char_index + our secret key (the number we want to shift the alphabet by) and the number of letters in the alphabet.

new_index = (char_index + secret_key) % 26

Lastly, we are going to create a variable called cipher_char, which will be the alphabet using the new_index, and setting all of it to upper case.

cipher_char = alphabet[new_index].upper()

Your code up to now should look like the below. Make sure that you have proper indenting!

Screen Shot 2019-07-16 at 2.40.47 PM

Great! Now that we have our if, we need to put in our else, or rather, what do we want to happen if the message contains any characters that are not in the alphabet? Things like spaces are not considered part of the alphabet, so I’m going to have to just stay as that same character and set it to our cipher_char for now.

Screen Shot 2019-07-16 at 2.43.16 PM

The last part of our encrypt piece is to put all of our new characters together and then have it print “Your ciphertext is: ” and then the encrypted message

Screen Shot 2019-07-16 at 2.46.48 PM.png

Try it out! You’ll want to call your encrypt method, and fill in what your message is with what you want the shift # to be.

encrypt(“my sister is downstairs”, 3)

Decoding Secret Messages

Once you’ve got the encrypting piece above working, we can move onto the second part where we can decrypt secret message that we have received from a friend!

To do this, we are going to create another method and call it decrypt. Since we need to know the secret message and the # to shift the alphabet back by, we’ll want to pass both of those things in as arguments. We will also want to create a variable called plaintext and give it a value of empty quotations.

Screen Shot 2019-07-16 at 2.53.34 PM

This method is going to look a lot like our encrypt method with a few key differences. We are going to create a loop that will go through every character, but this time it will be in the ciphertext that we will be providing instead of the plaintext. We will want to again have an IF that checks if the character is in the alphabet or not, and then change it to lowercase and index the position of the character in the alphabet. Now, when we make our variable for our new_index, instead of having the character position PLUS the key, we want to reverse that, so we will do the character position MINUS the key. And lastly, we are going to want to be making a plaintext char instead of a cipher char, and stringing them together and printing out the message. Your code should look like the below.

Screen Shot 2019-07-16 at 2.59.48 PM

Give it a try! See if you and your friend can send each other encrypted secret messages and decrypt them.

What’s Next?

In the next blog post we will be exploring ways to figure out how to crack a secret message when we don’t know what the shift # is using the brute force method.

If you are still having trouble and need to see the full code from this blog post, I’ve pasted it below. Feel free to reach out if you need any help debugging! Happy Encrypting!

Screen Shot 2019-07-16 at 3.04.31 PM

 

Leave a comment

Create a website or blog at WordPress.com

Up ↑