30 Mins

2. Classical Ciphers & Cryptanalysis

Substitution Ciphers: Caesar and Monoalphabetic

The Caesar cipher is the most basic form of substitution where each letter in a plaintext is shifted by a fixed number of positions down the alphabet. For instance, a shift of 3 turns A into D.

Monoalphabetic substitution expands this by mapping the alphabet to a random permutation, providing 26! possible keys. While stronger than Caesar, it remains vulnerable to frequency analysis.

In modern security, we often simulate this using OpenSSL or Python to demonstrate how easily weak entropy leads to plaintext recovery.

Security Operator Note: Monoalphabetic ciphers do not change the frequency distribution of characters, which is their ultimate undoing.
Python 3
def caesar_cipher(text, shift):
    result = ""
    for char in text:
        if char.isalpha():
            start = ord('A') if char.isupper() else ord('a')
            result += chr((ord(char) - start + shift) % 26 + start)
    return result

print(f"Encrypted: {caesar_cipher('VOIDX', 3)}")

Polyalphabetic Ciphers: The Vigenère Cipher

The Vigenère cipher uses a keyword to apply multiple Caesar shifts in sequence, effectively flattening the frequency distribution of the ciphertext.

To break it, an attacker must first determine the key length using the Kasiski examination or Index of Coincidence, then treat each sub-segment as a simple Caesar cipher.

In a terminal environment, you can test encryption pipelines using standard pipes to manipulate byte streams.

Security Operator Note: A Vigenère cipher with a key length equal to the message length is essentially a One-Time Pad.
Text
# Example of simple byte stream transformation
echo 'VOIDX' | tr 'A-Z' 'D-ZA-C' > encrypted.txt
cat encrypted.txt

Frequency Analysis Cryptanalysis

Frequency analysis relies on the fact that in any given language, certain characters appear more often than others (e.g., E is the most common in English).

By counting character occurrences in a large ciphertext, we can map the most frequent symbols back to the most common plaintext letters, effectively breaking substitution ciphers.

Below is a Python snippet that performs a character count to prepare for a statistical attack.

Security Operator Note: Always normalize your ciphertext to uppercase and remove punctuation before running frequency distributions.
Python 3
from collections import Counter

def analyze_frequency(ciphertext):
    count = Counter(ciphertext.replace(' ', ''))
    return count.most_common()

print(analyze_frequency('HELLOO WORLD THIS IS A TEST'))

One-Time Pad (OTP) and Perfect Secrecy

The One-Time Pad is the only mathematically proven unbreakable cipher, provided it meets three requirements: the key must be truly random, as long as the message, and used only once.

If a key is reused, an attacker can XOR two ciphertexts to eliminate the key and recover parts of the original messages.

Using Python's secrets module provides the cryptographically strong randomness required for such operations.

Security Operator Note: The security of an OTP rests entirely on the quality of the random key generation process.
Python 3
import secrets

def otp_xor(data, key):
    return bytes([b ^ k for b, k in zip(data.encode(), key)])

message = "SECRET"
key = secrets.token_bytes(len(message))
encrypted = otp_xor(message, key)
print(f"Encrypted Bytes: {encrypted.hex()}")

Knowledge Check

Ready to test your understanding of 2. Classical Ciphers & Cryptanalysis?

Back