Simple Implementation of Caesar Cipher in Python

What will we cover?

We will implement the Caesar Cipher in Python using functions and for-loops.

But wait, we will also describe what the Caesar Cipher is – this will actually help us understand how to solve it with functions.

In the video I will walk you through all you need including how to implement it.

Step 1: Describing Caesar Cipher

  • The Caesar Cipher is a simple substitution cipher, which is limited to only shift the characters by fixed number.
  • Let’s take an example
    • Imagine you got the message: BRX DUH DZHVRPH. What to make out of it. Makes no sense.
    • But what if we get help from this diagram

As you see, the lette B is the first in our cipher message (BRX DUH DZHVRPH).

Above that we find B in the red line and see the letter above is Y on the green line. That means, that the letter Y was encrypted to the cipher letter B.

If you follow along, you will see this is how it works to decrypt the message.

The reverse process will encrypt it.

Step 2: Implement the encryption function

Let’s implement a helper function encrypt_char(char, key) to encrypt a single character, char, with key. What this encrypt function does, is it makes the process above for us. Hence, we have this function to do the process of encrypting one letter.

def encrypt_char(char, key):
    return chr(ord('A') + (ord(char) - ord('A') + key) % 26)

Then we can implement encrypt_message(message, key), which uses the helper function. Hence, this function below is doing the process of encrypting a whole message.

def encrypt_message(message, key):
    message = message.upper()
    cipher = ''
    for char in message:
        if char not in ' ,.':
            cipher += encrypt_char(char, key)
        else:
            cipher += char
    return cipher

Let’s try it.

encrypt_message("you are awesome.", 3)

Which results in BRX DUH DZHVRPH.

Step 3: Implement the decryption function

This is quite similar.

def decrypt_char(char, key):
    return chr(ord('A') + (ord(char) - ord('A') + 26 - key) % 26)

def decrypt_message(cipher, key):
    cipher = cipher.upper()
    message = ''
    for char in cipher:
        if char not in ' ,.':
            message += decrypt_char(char, key)
        else:
            message += char
    return message

Let’s try it.

decrypt_message('BRX DUH DZHVRPH.', 3)

Then you get YOU ARE AWESOME.

What next?

I am happy you asked.

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Learn Python

Leave a Reply Cancel reply

Exit mobile version