🔐 Practical - 1

Caesar Cipher - Interactive Practical on Substitution Technique

📋 Objective

To implement a classic substitution cipher — the Caesar Cipher — and understand how simple cryptographic techniques work by shifting letters of the alphabet.

🎯 Purpose of the Practical

To understand and implement the Caesar Cipher, a classic example of a substitution cipher in cryptography. This practical introduces students to the basic concepts of encryption and decryption using a shift-based algorithm.

📚 Theoretical Background

What is a Caesar Cipher?

The Caesar Cipher is one of the simplest and oldest encryption techniques. It is a substitution cipher where each letter in the plaintext is shifted a certain number of places down the alphabet.

Example (with shift = 3):

A → D
B → E
C → F
D → G
E → H
F → I
G → J

Formula

For encryption:

Ci = (Pi + k) mod 26

For decryption:

Pi = (Ci - k) mod 26

Where:

Pi = position of the original letter (0 to 25)

Ci = position of the encrypted letter

k = shift (key)

🛠️ Interactive Caesar Cipher Tool

Results will appear here...

💻 Code Implementation

def caesar_encrypt(text, shift):
   result = ""
   for char in text:
      if char.isalpha():
         base = ord('A') if char.isupper() else ord('a')
         result += chr((ord(char) - base + shift) % 26 + base)
      else:
         result += char
   return result

def caesar_decrypt(text, shift):
   return caesar_encrypt(text, -shift)

# Example
plaintext = "GEC,BHARUCH"
key = 3
encrypted = caesar_encrypt(plaintext, key)
decrypted = caesar_decrypt(encrypted, key)

print("Encrypted:", encrypted)
print("Decrypted:", decrypted)

Output:

Encrypted: JHF,EKDUXFK

Decrypted: GEC,BHARUCH

🔑 Key Observations

  • The cipher uses modulo operation to wrap characters
  • Decryption is effectively the same logic as encryption, just using -shift
  • Works for both uppercase and lowercase letters while ignoring non-alphabet characters
  • Symmetric cipher: Same key (shift) used for encryption and decryption

⚠️ Limitations of Caesar Cipher

  • Very easy to break (only 25 possible shifts)
  • No security in the modern context
  • Vulnerable to brute force and frequency analysis
  • Very weak by today's standards
  • Not suitable for any real-world secure communication

🛡️ Security Aspects

The Caesar Cipher is vulnerable to:

  • Brute force attack: Only 25 possible shifts to try
  • Frequency analysis attack: Letter frequencies are preserved

This teaches why modern encryption must be complex and secure against known attacks.

🌍 Real-Life Applications

While the Caesar Cipher itself is outdated for modern cryptography, its concepts are foundational. Real-life applications include:

  • Learning cryptography basics
  • Simple text obfuscation (e.g., in puzzles, games)
  • Educational tools to teach children or beginners how encryption works
  • Understanding modular arithmetic and its use in secure communications

🎓 Learning Outcomes

After performing this practical, students will:

  • Understand classical encryption methods and their weaknesses
  • Learn substitution-based cryptography techniques
  • Gain knowledge of how modular arithmetic is used in cryptography
  • Be able to encrypt and decrypt messages using Caesar Cipher
  • Understand the importance of key management in cryptography
  • Realize how modern cryptography evolved from these classical methods

📝 Important Notes

  • Caesar Cipher is case-sensitive in this implementation
  • Only alphabetic characters are shifted; spaces and punctuation remain unchanged
  • The shift wraps around (e.g., 'Z' shifted by 1 becomes 'A')
  • Decryption uses the negative of the encryption shift