Python

 

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

  1. Install Python: Make sure Python 3.x is installed on your machine.
  2. Save the program: Copy the Python code provided above into a text file and save it as simple_encryption.py.
  3. 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
  4. 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.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)