Classical ciphers: substitution and transposition
Classical substitution and transposition ciphers: definitions, maths, cryptanalysis (frequency, Kasiski, index of coincidence), and worked examples (Caesar, Vigenère, columnar transposition).
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Classical ciphers teach the basic ideas of secrecy, keys and cryptanalysis that underlie modern cryptography. An engineer uses them to understand core concepts like substitution, permutation (transposition), keyspace, and why modern systems need stronger designs (diffusion and confusion), and to practice hands-on attacks such as frequency analysis and the Kasiski test.
Key ideas
Start with definitions (clear and precise):
- Plaintext: the original readable message to be protected.
- Ciphertext: the transformed message after encryption.
- Cipher: the algorithm that transforms plaintext to ciphertext using a key.
- Key: secret parameter(s) that control the cipher operation.
Substitution vs transposition
- Substitution cipher: each symbol (usually letter) in plaintext is replaced by another symbol according to some rule depending on the key. Assumption: alphabet is finite and mapped to numbers. Example families: Caesar cipher, affine cipher, monoalphabetic cipher, polyalphabetic cipher (e.g., Vigenère), and one-time pad. Also common in exams: Playfair cipher (digraph substitution; usually merge I/J) and Hill cipher (block substitution using an invertible matrix modulo 26; require gcd(det(K),26)=1 for decryption).
- Transposition cipher: the plaintext symbols are permuted (re-ordered) but the symbol identities remain the same. Example families: rail-fence, columnar transposition.
Formal statements and derivations (common exam items)
Caesar cipher (shift cipher): Let the alphabet be A = {A=0, B=1, …, Z=25}. For key k in {0,…,25}, encryption E_k and decryption D_k are defined by
- E_k(P) = (P + k) mod 26
- D_k(C) = (C - k) mod 26 This is a special case of the affine cipher with a = 1. Derivation commonly asked: show that D_k(E_k(P)) = P because (P + k - k) mod 26 = P mod 26.
Affine cipher: With integers a, b and gcd(a,26)=1 (this is required so a has a multiplicative inverse modulo 26). Define
- E_{a,b}(P) = (a·P + b) mod 26
- D_{a,b}(C) = a^{-1}·(C - b) mod 26, where a^{-1} is the modular inverse of a modulo 26. Derivation: D(E(P)) = a^{-1}·(a·P + b - b) = P (mod 26). Exam often asks: prove requirement gcd(a,26)=1; reason: multiplicative inverse a^{-1} exists iff gcd(a,26)=1 by Bezout's identity / modular arithmetic theory.
Monoalphabetic vs polyalphabetic
- Monoalphabetic cipher: a single substitution alphabet for whole message; keyspace size is 26! permutations (very large). Vulnerable to frequency analysis because letter frequencies are preserved.
- Polyalphabetic cipher (Vigenère): uses multiple alphabets depending on key letters. Key of length m repeats (or is non-repeating for OTP). With key K = k_0..k_{m-1} (each 0..25) encryption: C_i = (P_i + k_{i mod m}) mod 26. This reduces simple frequency patterns; cryptanalysis uses Kasiski test and index of coincidence.
One-time pad (OTP): key is random, as long as message, used once. Encryption: for bitstrings, C_i = P_i XOR K_i; for alphabetic symbols mapped to 0..25, C_i = (P_i + K_i) mod 26. Perfect secrecy theorem (Shannon): OTP provides perfect secrecy if key is truly random, used only once, and kept secret. Assumptions: key length equals plaintext length and uniform random.
Transposition ciphers
- Rail-fence: write plaintext in zig-zag across r rails and read row by row. This is a permutation of indices. Diagram to draw in exam: draw r horizontal lines (rails); write successive plaintext letters moving down until bottom rail then up, forming a zig-zag; label positions with indices; show read-out order.
- Columnar transposition: write text row-wise into a table with number of columns = key length; permute columns according to numeric order of key letters (assign increasing numbers to key letters by alphabetical order; treat repeated key letters by left-to-right tie-break). Diagram to draw: rectangle grid with rows filled left-to-right with plaintext characters; above each column write key letter and its numeric order; arrows showing column reordering and resulting ciphertext read column-by-column.
Cryptanalysis principles
- Frequency analysis: because monoalphabetic substitution preserves plaintext letter frequencies, an attacker compares ciphertext frequencies with expected language frequencies.
- Kasiski test: for polyalphabetic (like Vigenère), find repeated ciphertext substrings and distances between them; the greatest common divisors of distances give likely key lengths.
- Index of coincidence (IC): measures how likely two randomly chosen letters from text are equal. For text length N and frequency f_i of letter i (0..25), IC = sum_i f_i(f_i-1) / (N(N-1)). Expected IC for English ≈ 0.0667; for random uniform text ≈ 1/26 ≈ 0.03846. Use IC to estimate key length: the average IC for m-shifted groups approximates English IC when grouping by correct key length.
Security/strength notes
- Keyspace sizes: Caesar has 26 keys only; monoalphabetic has 26! ≈ 4·10^26 permutations (large), but frequency analysis breaks it; Vigenère with short key is weak; OTP is secure but impractical for many applications.
- Shannon's terms: confusion (key hides relationship between ciphertext and key) and diffusion (spread plaintext redundancy across ciphertext) — substitution provides confusion, transposition provides diffusion; secure ciphers combine both.
Formulas
C = (P + k) mod 26 — C: ciphertext letter number (0–25), P: plaintext letter number (0–25), k: shift key (integer 0–25); applies to Caesar cipher.
P = (C - k) mod 26 — inverse for Caesar; symbols as above.
C = (a·P + b) mod 26 — affine cipher encryption; a, b integers; requires gcd(a,26)=1.
P = a^{-1}·(C - b) mod 26 — affine cipher decryption; a^{-1} is modular inverse of a modulo 26.
C = K·P mod 26 — Hill cipher encryption for block vector P and key matrix K; requires gcd(det(K),26)=1 so K has a modular inverse.
P = K^{-1}·C mod 26 — Hill cipher decryption; K^{-1} is the modular inverse matrix modulo 26.
IC = (Σ_i f_i·(f_i - 1)) / (N·(N - 1)) — Index of coincidence; f_i frequency of i-th letter, N total letters; unitless; applies to alphabetic text frequency analysis.
Keyspace_monoalphabetic = 26! — number of possible monoalphabetic substitution keys; applies when full permutation of alphabet used.
Keyspace_Vigenere = 26^m — keyspace when key length is m and each key letter is independent (26 values each).
Keyspace_Caesar = 26 — shift keys 0..25.
Number_of_permutations = n! — number of possible permutations for transposition of n symbols (factorial); applies to transposition of length n.
Worked examples
Example 1 — Caesar cipher (numerical, step-by-step) Encrypt plaintext "MEET AT DAWN" with key k = 3. Use A=0, B=1, …, Z=25. Ignore spaces in encryption (common exam instruction). Steps:
- Map letters to numbers: M=12, E=4, E=4, T=19, A=0, T=19, D=3, A=0, W=22, N=13.
- Formula: C = (P + k) mod 26.
- Compute: (show each)
- M: (12 + 3) mod 26 = 15 -> P
- E: (4 + 3) mod 26 = 7 -> H
- E: 7 -> H
- T: (19 + 3) mod 26 = 22 -> W
- A: (0 + 3) = 3 -> D
- T: 22 -> W
- D: (3 + 3) = 6 -> G
- A: 3 -> D
- W: (22 + 3) = 25 -> Z
- N: (13 + 3) = 16 -> Q
- Ciphertext letters: P H H W D W G D Z Q. Combine and insert spaces as desired: PHHWDWGDZQ; if preserving original word boundaries: PHHW DW GDZQ. Final answer: PHHWDWGDZQ.
Example 2 — Vigenère cipher (algorithm trace) Encrypt plaintext "ATTACKATDAWN" with key "LEMON" (repeat key as needed). Steps:
- Map A=0..Z=25 and key letters to numbers: L=11, E=4, M=12, O=14, N=13. Repeated key sequence for 12-letter plaintext: 11,4,12,14,13,11,4,12,14,13,11,4.
- For each position i, C_i = (P_i + k_i) mod 26.
- Compute (show several):
- A(0) + L(11) = 11 -> L
- T(19) + E(4) = 23 -> X
- T(19) + M(12) = 31 mod26 = 5 -> F
- A(0) + O(14) = 14 -> O
- C(2) + N(13) = 15 -> P
- K(10) + L(11) = 21 -> V
- A(0) + E(4) = 4 -> E
- T(19) + M(12) = 31 -> F
- D(3) + O(14) = 17 -> R
- A(0) + N(13) = 13 -> N
- W(22) + L(11) = 33 mod26 = 7 -> H
- N(13) + E(4) = 17 -> R
- Ciphertext: L X F O P V E F R N H R. Final ciphertext: LXFOPVEFRNHR.
Example 3 — Columnar transposition (small, step-by-step) Encrypt plaintext "HELLOWORLD" with key "ZEB". Use key letters to get column order: Z E B -> alphabetical order B (1), E (2), Z (3). So column order indices: Z->3, E->2, B->1.
- Set number of columns = key length = 3. Fill rows left-to-right. Grid (rows): Row1: H E L Row2: L O W Row3: O R L Row4: D _ _ (pad blanks or X as needed; here blanks allowed)
- Label columns with key and order above each column: col1 Z(3), col2 E(2), col3 B(1).
- Read columns in order of increasing numeric order: 1 -> column with B (col3), 2 -> E (col2), 3 -> Z (col1).
- Column for B (col3): L, W, L, _ -> LWL
- Column for E (col2): E, O, R, _ -> EOR
- Column for Z (col1): H, L, O, D -> HLOD
- Ciphertext (concatenate): LWLEORHLOD (if blanks are omitted) or pad with X and give consistent output. Final answer: LWLEORHLOD.
(Notes: in exams, state how you handle padding and ties in key letters.)
Common mistakes
- Using an 'a' in affine cipher with gcd(a,26) ≠ 1 (e.g., a=2) and then trying to compute inverse — mention requirement explicitly.
- In Vigenère, misaligning key letters (forgetting to repeat key properly or mis-handing non-letter characters).
- For columnar transposition, mishandling repeated letters in the key (tie-breaking left-to-right) or mis-sorting key order.
- Computing Index of Coincidence with denominator N instead of N(N−1) or forgetting to subtract 1 in numerator terms.
For your exam
Types of questions you will get:
- Definitions and short notes: define substitution, transposition, Caesar, Vigenère, Playfair, Hill, OTP; include strengths and weaknesses (e.g., invertibility condition for Hill, I/J convention for Playfair).
- Derivations: show D(E(P)) = P for Caesar/affine; prove requirement gcd(a,26)=1 for affine decryption.
- Numerical encryption/decryption: given plaintext and key, produce ciphertext (show mappings and mod arithmetic). Full-marks answer lists mappings, formulas, each arithmetic step, and final ciphertext clearly labeled.
- Cryptanalysis tasks: use frequency analysis on a short ciphertext, perform Kasiski test on repeated trigrams to estimate key length, calculate IC and interpret result (compare to 0.0667 and 0.03846).
- Algorithm traces and diagrams: draw the transposition grid, label columns by key order, show readout order; for rail-fence draw rails and zig-zag path and label indices.
What a full-marks answer includes:
- Clear definitions (bold terms if asked), statement of formulas, step-by-step numeric work with modular reductions shown, and correct handling of spaces/punctuation/padding. For cryptanalysis, show data (repeated patterns, distances), their GCDs, IC calculation with intermediate sums, and final conclusion.
Quick check
- What condition must a satisfy in the affine cipher E(P) = (a·P + b) mod 26 so decryption exists?
- Give the formula for the index of coincidence and explain what value indicates English-like text.
- In a Caesar cipher with k=5, what is ciphertext of letter Z (use A=0)?
Answers: 1) gcd(a,26) = 1. 2) IC = (Σ f_i(f_i−1)) / (N(N−1)); English-like ≈ 0.0667 (random uniform ≈ 0.03846). 3) Z=25 -> (25+5) mod26 = 4 -> E.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?