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)
            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)
            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.

Python Like a Pro?

If you’re serious about learning Python, there’s nothing better than strong commits. At your request, we have created an improved version of this popular free online course.

This version has the following benefits to enhance your learning journey.

  1.  Tracking your progress in the course.
  2.  Questionaries to ensure you understand concepts between important lessons.
  3. Downloadable Cheat Sheets for fast lookup what you learned.
  4. Direct Q&A with the instructor to help you to understand the material better.
  5. Added material for better explanations and insider knowledge.
  6. Extra videos with more explanations and stories.
  7.  Certificate at completion.

Start the change in your life and commit to doing something amazing that you have always dreamed of.

Sign up and become part of the exclusive Python Like a Pro elite.

Leave a Reply

%d bloggers like this: