GITHUB REPO | FOLLOW ME ON -> TWITTER | LINKEDIN
Secure Your Messages with Caesar Cipher in Python
The Caesar Cipher is one of the most basic encryption methods, in which each letter in the plaintext is replaced by a letter a specified number of places down the alphabet. We will write Python code that will allow users to encode and decode messages using the Caesar Cipher.
Step-by-Step Explanation of Code
Before we get started, you need make sure Python is installed on your computer. After that, open your favorite text editor and create a new Python file name caesar_cypher.py
.
art.py
logo = """
,adPPYba, ,adPPYYba, ,adPPYba, ,adPPYba, ,adPPYYba, 8b,dPPYba,
a8" "" "" `Y8 a8P_____88 I8[ "" "" `Y8 88P' "Y8
8b ,adPPPPP88 8PP""""""" `"Y8ba, ,adPPPPP88 88
"8a, ,aa 88, ,88 "8b, ,aa aa ]8I 88, ,88 88
`"Ybbd8"' `"8bbdP"Y8 `"Ybbd8"' `"YbbdP"' `"8bbdP"Y8 88
88 88
"" 88
88
,adPPYba, 88 8b,dPPYba, 88,dPPYba, ,adPPYba, 8b,dPPYba,
a8" "" 88 88P' "8a 88P' "8a a8P_____88 88P' "Y8
8b 88 88 d8 88 88 8PP""""""" 88
"8a, ,aa 88 88b, ,a8" 88 88 "8b, ,aa 88
`"Ybbd8"' 88 88`YbbdP"' 88 88 `"Ybbd8"' 88
88
88
"""
caesar_cypher.py
We begin our code by importing the required art
module and defining the alphabet
and reset
variable is set to True.
import art
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']
reset = True
The caesar()
function:
It takes the start_text
, shift_amount
, and cipher_direction
as parameters and returns the encrypted or decrypted message. The function checks whether the cipher_direction
is decode
, and if so, multiplies the shift_amount
by -1.
For each character in the input text
, the function checks if it is a letter or not. If it is a letter, the function gets the position of the letter in the alphabet
list and adds the shift_amount
. The new position is used to retrieve the corresponding letter from the alphabet
list and add it to the end_text
variable.
Finally, we print the encrypted or decrypted message.
def caesar(start_text, shift_amount, cipher_direction):
end_text = ""
if cipher_direction == "decode":
shift_amount *= -1
for char in start_text:
if char in alphabet:
position = alphabet.index(char)
new_position = position + shift_amount
end_text += alphabet[new_position % len(alphabet)]
else:
end_text += char
print(f"Here's the {cipher_direction}d result: {end_text}\n")
In the loop, first we print the logo from art
module. After that, we take the user’s input for cipher_direction
, text
, and shift
. The caesar()
function is then called with these inputs as parameters.
while reset:
print(art.logo)
direction = input("Type 'encode' to encrypt, 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
caesar(start_text=text, shift_amount=shift, cipher_direction=direction)
At last, after displaying the encrypted or decrypted message, we invite the user to run the application again. If the choice is no, then we will set the reset
value to False, and it will end the loop with a thank you message.
choice = input("Do you want to run this program. Type Yes or No.\n").lower()
if choice == 'no':
reset = False
print("Thank you for using our program. Goodbye")
Here is the full code.
Sample Output
Features
Program’s Features
- Encrypt and decrypt messages using Caesar Cipher
- User-friendly with clear instructions
Python Concepts and Techniques Used
- List: We use the
alphabet
list to store all the letters of the alphabet. - Function: We define the
caesar()
function to encrypt or decrypt messages. - Loop: We use a
for
loop to iterate over each character in the inputtext
. And awhile
loop for the code to run until user chose to end. - Conditional statements: We use
if-else
statements to check if the character is a letter or not andcipher_direction
is decode or not.
We developed a Python program in this project that allows users to encryption and decryption messages using Caesar Cipher. We explored Python concepts and techniques such as lists, functions, loops, and conditions. We may quickly encrypt our messages with this application and keep them safe from suspicious eyes.
This article is a part of my Python Project Journal. To learn more about my journey in learning Python and to see my other projects, check out this project journal. Thank you!