CipherSense: Your Personal Text Encryption Solution (Project 9)
Building a Caesar Cipher App: Encrypt and Decrypt Text with Python
Introduction:
As data security continues to be a top priority, encryption techniques have become vital in protecting sensitive information. The Caesar cipher, a classic encryption method, has been utilized for centuries to encode and decode messages. In this article, we will explore the development of a Caesar cipher app using Python. This app will empower users to easily encrypt and decrypt their text, ensuring their data remains secure.
Source code:
#main file save this code in main file //////////////////
import logo
print(logo.cipher_logo)
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', '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'
]
def cipher(text_t, shift_s, direction_i):
cipher_text = ''
if direction_i == 'decode':
shift_s *= -1
for i in text_t:
if i in alphabet:
firs_position = alphabet.index(i)
new_position = firs_position + shift_s
new_letter = alphabet[new_position]
cipher_text += new_letter
else:
cipher_text += i
print(f"Coded text : {cipher_text}")
should_end = False
while should_end == False:
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
shift = shift % 26
cipher(text_t=text, shift_s=shift, direction_i=direction)
restart = input("If you want to run again type 'yes' otherwise 'no'\n").lower()
if restart == 'no':
print("Goodbye")
should_end = True
else:
pass
# logo file save this file as logo name. /////////////////////
cipher_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
"""
Explanation:
The above code demonstrates the implementation of a Caesar cipher app using Python. Let's go through it step by step.
Importing the logo:
The code begins with importing the logo module, which likely contains the ASCII art for the cipher's logo or a related design element.
Initializing the Alphabet:
Next, we create a list called alphabet containing all the letters of the English alphabet. This list will be used as a reference for shifting letters during encryption and decryption.
User Input:
The user is prompted to enter whether they want to encode or decode a message. The direction variable stores this input. Additionally, the user is asked to provide the text they want to process (text) and the desired shift value (shift).
The Cipher Function:
The cipher function takes three parameters: text_t (the text to be processed), shift_s (the shift value), and direction_i (the desired action: encode or decode). Inside the function, a variable called cipher_text is initialized to store the result.
Iterating Over the Text:
A loop iterates through each character in the input text_t. For each character, its index position in the alphabet list is determined and stored in the first_position variable.
Encoding or Decoding:
Based on the direction_i value, the code either adds or subtracts the shift_s value from the first_position. This determines the new position of the letter in the shifted alphabet.
Generating the Cipher Text:
The new letter is retrieved from the alphabet list using the new_position. This letter is then added to the cipher_text string.
Displaying the Result:
Finally, the encoded or decoded text is printed to the console, providing the user with the desired output.
Conclusion:
In conclusion, the Caesar cipher app developed using Python offers a practical solution for encrypting and decrypting text. By incorporating the fundamental concepts of the Caesar cipher and leveraging Python's simplicity and versatility, the app allows users to encode or decode their messages effortlessly. With further enhancements and additional features, this app can serve as a useful tool for anyone looking to ensure the security of their sensitive information.