Information Security 7 min read

Implementing a Simple Caesar Cipher for Data Encryption in Python

This tutorial explains the fundamentals of data encryption, introduces differential privacy, and guides you through implementing a simple Caesar Cipher encrypt‑and‑decrypt program in Python, complete with code examples, execution instructions, and sample output.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Implementing a Simple Caesar Cipher for Data Encryption in Python

Goal : Understand the basic concepts of data encryption and implement a simple encryption algorithm.

Learning Content : Data encryption converts plaintext into ciphertext to prevent unauthorized access. Differential privacy protects individual privacy by adding noise to data.

Practice : We will implement a simple symmetric encryption method using the Caesar Cipher, a basic substitution technique that shifts each character by a fixed number of positions.

1. Install Required Libraries

No additional libraries are needed; the implementation uses pure Python.

2. Implement the Caesar Cipher Encryption Function

def caesar_cipher_encrypt(text, shift):
    """Use Caesar Cipher to encrypt text.
    Parameters:
        text (str): plaintext to encrypt
        shift (int): number of positions to shift
    Returns:
        str: ciphertext
    """
    encrypted_text = ""
    for char in text:
        if char.isalpha():
            shift_amount = shift % 26
            if char.islower():
                new_char = chr(((ord(char) - ord('a') + shift_amount) % 26) + ord('a'))
            else:
                new_char = chr(((ord(char) - ord('A') + shift_amount) % 26) + ord('A'))
        else:
            new_char = char
        encrypted_text += new_char
    return encrypted_text

# Test the encryption function
plain_text = "你好,世界!Hello, World!"
shift = 3  # shift by 3 positions
encrypted_text = caesar_cipher_encrypt(plain_text, shift)
print(f"明文: {plain_text}")
print(f"密文: {encrypted_text}")

3. Implement the Caesar Cipher Decryption Function

def caesar_cipher_decrypt(cipher_text, shift):
    """Use Caesar Cipher to decrypt text.
    Parameters:
        cipher_text (str): ciphertext to decrypt
        shift (int): number of positions to shift
    Returns:
        str: plaintext
    """
    decrypted_text = ""
    for char in cipher_text:
        if char.isalpha():
            shift_amount = shift % 26
            if char.islower():
                new_char = chr(((ord(char) - ord('a') - shift_amount) % 26) + ord('a'))
            else:
                new_char = chr(((ord(char) - ord('A') - shift_amount) % 26) + ord('A'))
        else:
            new_char = char
        decrypted_text += new_char
    return decrypted_text

# Test the decryption function
decrypted_text = caesar_cipher_decrypt(encrypted_text, shift)
print(f"密文: {encrypted_text}")
print(f"解密后的明文: {decrypted_text}")

4. Full Code Example

def caesar_cipher_encrypt(text, shift):
    """Use Caesar Cipher to encrypt text.
    Parameters:
        text (str): plaintext
        shift (int): shift amount
    Returns:
        str: ciphertext
    """
    encrypted_text = ""
    for char in text:
        if char.isalpha():
            shift_amount = shift % 26
            if char.islower():
                new_char = chr(((ord(char) - ord('a') + shift_amount) % 26) + ord('a'))
            else:
                new_char = chr(((ord(char) - ord('A') + shift_amount) % 26) + ord('A'))
        else:
            new_char = char
        encrypted_text += new_char
    return encrypted_text

def caesar_cipher_decrypt(cipher_text, shift):
    """Use Caesar Cipher to decrypt text.
    Parameters:
        cipher_text (str): ciphertext
        shift (int): shift amount
    Returns:
        str: plaintext
    """
    decrypted_text = ""
    for char in cipher_text:
        if char.isalpha():
            shift_amount = shift % 26
            if char.islower():
                new_char = chr(((ord(char) - ord('a') - shift_amount) % 26) + ord('a'))
            else:
                new_char = chr(((ord(char) - ord('A') - shift_amount) % 26) + ord('A'))
        else:
            new_char = char
        decrypted_text += new_char
    return decrypted_text

# Test encryption and decryption
plain_text = "你好,世界!Hello, World!"
shift = 3
encrypted_text = caesar_cipher_encrypt(plain_text, shift)
print(f"明文: {plain_text}")
print(f"密文: {encrypted_text}")

decrypted_text = caesar_cipher_decrypt(encrypted_text, shift)
print(f"密文: {encrypted_text}")
print(f"解密后的明文: {decrypted_text}")

5. Run the Code

Save the above script to a file named caesar_cipher.py and execute it from the command line:

python caesar_cipher.py

Sample Output

明文: 你好,世界!Hello, World!
密文: 你好,世界!Khoor, Zruog!
密文: 你好,世界!Khoor, Zruog!
解密后的明文: 你好,世界!Hello, World!

Summary : By completing this practice you have learned how to implement a basic Caesar Cipher encryption and decryption algorithm in Python, which illustrates core encryption principles. In real‑world applications more robust algorithms such as AES are used to provide stronger security.

PythonEncryptionTutorialData Securitycryptographycaesar-cipher
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.