Introduction
Encryption is the process of converting information or data into a secure format that cannot be easily understood by unauthorized parties. Decryption is the reverse process, where encrypted data is transformed back into its original format.
This program implements a simple encryption and decryption algorithm using a basic Caesar cipher technique. The Caesar cipher is one of the oldest and simplest encryption techniques, where each letter in the plaintext is shifted by a certain number of places in the alphabet. The program allows for both encryption and decryption of user input based on a specified shift key.
The goal of this program is to provide a simple, easy-to-understand encryption and decryption system that demonstrates the basic principles of data security.
Objective
The objective of this program is to:
- Implement a basic encryption algorithm using the Caesar cipher.
- Implement a basic decryption algorithm using the Caesar cipher.
- Allow users to input text and a shift value for both encryption and decryption.
- Provide a simple, secure way to understand how encryption and decryption work using Python programming language.
Code
# Simple Encryption and Decryption using Caesar Cipher
def encrypt(text, shift):
encrypted_text = ""
for char in text:
if char.isalpha(): # Check if the character is a letter
shift_value = 65 if char.isupper() else 97 # Adjust based on case (upper or lower)
encrypted_text += chr((ord(char) + shift - shift_value) % 26 + shift_value)
else:
encrypted_text += char # Non-alphabetic characters remain unchanged
return encrypted_text
def decrypt(text, shift):
decrypted_text = ""
for char in text:
if char.isalpha():
shift_value = 65 if char.isupper() else 97
decrypted_text += chr((ord(char) - shift - shift_value) % 26 + shift_value)
else:
decrypted_text += char # Non-alphabetic characters remain unchanged
return decrypted_text
def main():
print("Simple Encryption and Decryption Program")
choice = input("Do you want to (e)ncrypt or (d)ecrypt? ").lower()
text = input("Enter the text: ")
shift = int(input("Enter the shift value (1-25): "))
if choice == 'e':
encrypted = encrypt(text, shift)
print(f"Encrypted Text: {encrypted}")
elif choice == 'd':
decrypted = decrypt(text, shift)
print(f"Decrypted Text: {decrypted}")
else:
print("Invalid choice. Please choose 'e' for encryption or 'd' for decryption.")
if __name__ == "__main__":
main()
Explanation of the Program Structure
The program consists of three main functions:
- encrypt(text, shift): This function takes a string (text) and a shift value. It shifts each alphabetic character in the text by the specified number of positions, while non-alphabetic characters (like spaces or punctuation) are not altered. The result is the encrypted text.
- decrypt(text, shift): This function takes the encrypted text and the same shift value to reverse the process. It shifts each character in the opposite direction to recover the original text.
- main(): This is the main function where the user is prompted to choose between encryption or decryption, and it processes their input accordingly. It calls the encrypt or decrypt function based on user input and displays the result.
Steps to Run the Program
- Install Python: Make sure Python 3.x is installed on your machine.
- Save the program: Copy the Python code provided above into a text file and save it as
simple_encryption.py
. - Run the program: Open a terminal or command prompt, navigate to the directory where the file is saved, and run the program using the following command:
python simple_encryption.py
- Follow the prompts: The program will ask if you want to encrypt or decrypt text, input your text and shift value, and the result will be shown.