In this lesson, students will learn about the Caesar cipher, a simple encryption technique. They will then implement their own Caesar cipher encoder and decoder using Python.
- Brief history of cryptography.
- Overview of substitution ciphers.
- Explanation of the Caesar cipher:
Discuss the logic:
def caesar_cipher_encrypt(text, shift):
encrypted_text = ""
for char in text:
if char.isalpha():
shift_amount = shift % 26
new_char = chr(((ord(char.lower()) - ord('a') + shift_amount) % 26) + ord('a'))
encrypted_text += new_char.upper() if char.isupper() else new_char
else:
encrypted_text += char # Preserve spaces and punctuation
return encrypted_text
def caesar_cipher_decrypt(text, shift):
return caesar_cipher_encrypt(text, -shift)
message = "Hello, World!"
shift_value = 3
encrypted_message = caesar_cipher_encrypt(message, shift_value)
decrypted_message = caesar_cipher_decrypt(encrypted_message, shift_value)
print(f"Original: {message}")
print(f"Encrypted: {encrypted_message}")
print(f"Decrypted: {decrypted_message}")