Symmetric encryption: from Caesar to AES, modes of operation
The basic language of encryption, hand encryption with Caesar and Vigenère, XOR and the one-time pad, the idea of DES/AES, the ECB/CBC/CTR modes and the key distribution problem. Hand encryption and mode questions are here.
Cryptography is the engine of network security. The goals of confidentiality, integrity and authentication all end up resting on an encryption, hash or signature operation. This week has two pages: on this page, symmetric encryption; on the next page, public key, hash functions, signatures and TLS. From this page the exam asks hand encryption (Caesar, Vigenère, XOR), mode traces and key-space calculations. Golden rules:
- Symmetric = one shared key. The one who encrypts and the one who decrypts know the same key.
- Convert letters to numbers, take mod 26. A = 0, B = 1, …, Z = 25.
- XOR cancels itself. .
- Don’t use ECB. The same block gives the same output, and patterns leak.
- Don’t reuse the key. In OTP and CTR the keystream is used once.
Basic concepts
The plaintext is turned into ciphertext by the encryption algorithm and key : . Decryption: . In symmetric encryption both sides have the same .
Kerckhoffs’s principle: the algorithm is not secret; all the security lies in the secrecy of the key. Security that relies on hiding the algorithm (security through obscurity) collapses at the first reverse engineering.
Depending on what the attacker has, the kinds of cryptanalysis are: ciphertext-only, known-plaintext, chosen-plaintext. The crudest attack is brute force: trying all the keys. For a -bit key, the worst case is attempts and the average is .
Classical ciphers
The Caesar (shift) cipher
Each letter is shifted steps in the alphabet:
Example: NETWORK, .
N(13)+3=16 Q E(4)+3=7 H T(19)+3=22 W W(22)+3=25 Z
O(14)+3=17 R R(17)+3=20 U K(10)+3=13 N
NETWORK → QHWZRUN
There are only 25 meaningful keys; trying all of them takes seconds.
Monoalphabetic substitution and frequency analysis
If you map each letter to a random permutation of the alphabet, there are keys. Brute force is impossible, but the cipher is still broken: letter frequencies in a language are stable (in English E, T, A are the most frequent). The most frequent letter in the ciphertext is most likely E. Lesson: a large key space alone does not mean security.
The Vigenère cipher
A keyword is repeated and each letter is shifted by the key letter beneath it. The same plaintext letter is encrypted differently in different positions, and the frequency distribution flattens.
Example: SECURE, key KEY (K = 10, E = 4, Y = 24).
plain S(18) E(4) C(2) U(20) R(17) E(4)
key K(10) E(4) Y(24) K(10) E(4) Y(24)
sum 28 8 26 30 21 28
mod 26 2 8 0 4 21 2
cipher C I A E V C
SECURE → CIAEVC. The two E letters came out differently (I and C). If the key length is found (from repeating patterns), the cipher is still broken by splitting it into Caesar pieces.
XOR and the one-time pad
On a computer we work with bits instead of letters. XOR (): 0 if the bits are the same, 1 if different.
P = 1011 0110
K = 0110 1101
C = 1101 1011 (P ⊕ K)
C ⊕ K = 1011 0110 = P
One-time pad (OTP): the key is completely random, as long as the message, and used once. Under these conditions it is theoretically unbreakable. In practice, distributing the key is very hard. If the key is used twice, and security collapses.
Stream ciphers imitate the OTP: from a short key they produce a long pseudorandom keystream and XOR it with the plaintext.
Modern block ciphers: DES and AES
A block cipher turns a fixed-size block into another block by a key-dependent permutation. Over several rounds, substitution (confusion) and permutation (diffusion) are repeated. The goal: when a single bit of the plaintext or key changes, about half of the output changes (avalanche).
| DES | AES | |
|---|---|---|
| Block | 64 bits | 128 bits |
| Key | 56 bits (effective) | 128 / 192 / 256 bits |
| Rounds | 16 | 10 / 12 / 14 |
| Status | Broken by brute force | Today’s standard |
One round of AES has four steps: SubBytes (byte-by-byte substitution with an S-box), ShiftRows (shifting the rows), MixColumns (mixing the columns) and AddRoundKey (XOR with the round key). You aren’t expected to memorize the details; it is enough to know the order and the purpose.
Brute-force comparison ( attempts per second):
DES 2^56 ≈ 7.2 × 10^16 → ≈ 72 058 s ≈ 20 hours
AES-128 2^128 ≈ 3.4 × 10^38 → ≈ 1.1 × 10^19 years
Modes of operation
A block cipher encrypts a single block. For a long message a mode of operation is needed. To demonstrate, let us use a toy 4-bit cipher: = rotate left by 1 bit. , plaintext blocks .
ECB (electronic codebook)
Each block is independent: .
E(1010): 1010 ⊕ 0110 = 1100 → rotate → 1001
E(1010): same → 1001
E(0011): 0011 ⊕ 0110 = 0101 → rotate → 1010
ECB: 1001 1001 1010
The first two blocks are the same, and so are their ciphertexts. The attacker learned that “these two blocks are the same”. In images the pattern stays visible.
CBC (cipher block chaining)
Before each block is encrypted, it is XORed with the previous ciphertext block. For the first block a random initialization vector (IV) is used: , . :
C1 = E(1010 ⊕ 1001) = E(0011) = rotl(0101) = 1010
C2 = E(1010 ⊕ 1010) = E(0000) = rotl(0110) = 1100
C3 = E(0011 ⊕ 1100) = E(1111) = rotl(1001) = 0011
CBC: 1010 1100 0011
The same two plaintext blocks are now encrypted differently. Decryption: . CBC encryption is sequential (it cannot be parallelized), while decryption can be parallel. The IV doesn’t have to be secret but must be unpredictable.
CTR (counter)
The block cipher encrypts a counter value, and the resulting keystream is XORed with the plaintext: . The block cipher becomes a stream cipher. Let the counter start at :
E(0100) = rotl(0010) = 0100 C1 = 1010 ⊕ 0100 = 1110
E(0101) = rotl(0011) = 0110 C2 = 1010 ⊕ 0110 = 1100
E(0110) = rotl(0000) = 0000 C3 = 0011 ⊕ 0000 = 0011
(In the toy cipher the last keystream block came out as 0000; in real AES this possibility is negligible.) CTR is fully parallel and needs only for decryption. The cost: the same nonce/counter with the same key must never be repeated, otherwise you fall into the OTP’s key-reuse problem.
PITFALL · what is XORed in CBC? The plaintext is XORed with the previous ciphertext block, not with the previous plaintext block. The first block is XORed with the IV. In an exam, anyone who uses the previous plaintext block gets the whole chain wrong.
TIP · tell the modes apart in one sentence. ECB: independent, patterns leak. CBC: chain, random IV. CTR: encrypt the counter and XOR, unique nonce. Modern systems use modes like GCM that combine encryption with integrity.
The key distribution problem
Symmetric encryption is fast, but the two sides must share the same key in advance. If people each want a separate key for every pair:
And these keys must be distributed over a secure channel. How do you share a key with a server you have never met on the Internet? The answer is on the next page: public-key cryptography and Diffie-Hellman. In practice the two are used together: a session key is established with the public key, and the data is encrypted with a fast symmetric cipher (AES).
Common mistakes
| Wrong idea | Correct |
|---|---|
| In Caesar, Z + 3 = ”]” | Mod 26: Z(25) + 3 = 28 → 2 = C |
| Large key space = secure | Substitution with 26! keys is broken by frequency analysis |
| In Vigenère the key is written once | It is repeated along the message |
| An OTP key can be reused | If repeated, leaks |
| The AES-256 block is 256 bits | The block is always 128 bits; 256 is the key size |
| ECB is secure because it uses AES | If the mode is weak, identical blocks come out identical |
| In CBC, XOR with the previous plaintext block | With the previous ciphertext block (the IV for the first block) |
| In CTR the nonce can be reused | Repeating the nonce with the same key repeats the keystream |
| Hiding the algorithm increases security | Kerckhoffs: security lies only in the key |
| A key 1 bit longer is slightly more secure | Each extra bit doubles the brute-force time |
Test yourself
Exam-style questions. Solve them on paper first, then reveal the answer.
Encrypt the word ATTACK with a Caesar (shift) cipher using key k = 7.
Show answer
Rule: , A = 0.
A(0) → 7 = H. T(19) → 26 mod 26 = 0 = A. T → A. A → H. C(2) → 9 = J. K(10) → 17 = R.
Result: HAAHJR. When you go past 26 at the letter T, don't forget to wrap around (mod 26).
The ciphertext KHOOR was produced by a Caesar cipher with key 3. Recover the plaintext.
Show answer
Decryption: . Shift each letter back by 3.
K → H, H → E, O → L, O → L, R → O. The result is HELLO.
Even if the key were unknown, since there are only 25 meaningful keys, all of them would be tried. Caesar is completely defenseless against brute force.
Encrypt PACKET with the Vigenère cipher using the keyword LOCK (A = 0).
Show answer
The key is repeated: write L O C K L O under P A C K E T. Add each pair of letters, mod 26.
P(15) + L(11) = 26 → 0 = A
A(0) + O(14) = 14 = O
C(2) + C(2) = 4 = E
K(10) + K(10) = 20 = U
E(4) + L(11) = 15 = P
T(19) + O(14) = 33 → 7 = H
The result is AOEUPH. The same letter can be encrypted differently in different positions; this is what makes frequency analysis harder.
Compute the one-time pad ciphertext C = P XOR K for P = 11001010 and K = 01010011.
Show answer
XOR bit by bit: 0 if the same, 1 if different.
P = 1 1 0 0 1 0 1 0
K = 0 1 0 1 0 0 1 1
C = 1 0 0 1 1 0 0 1. Check: . Applying XOR twice cancels itself out, so decryption is the same operation.
A toy 4-bit block cipher is E_K(x) = rotate-left-by-1(x XOR K) with K = 0110. In ECB mode, what is the ciphertext of the block 1100?
Show answer
First the XOR: .
Rotate left by 1 bit: the leftmost 1 moves to the far right. .
In ECB every block is encrypted independently, and that's all. The same block always gives 0101.
Same toy cipher (K = 0110). In CBC mode with IV = 0011, what is the first ciphertext block C1 for plaintext block P1 = 1100?
Show answer
CBC: .
.
, rotate left: .
. In the previous question the same block gave 0101 in ECB; the IV changed the result.
An image is encrypted with AES, and the outline of the original picture is still visible in the ciphertext. Which mode of operation was most likely used?
- CBC with a random IV
- CTR with a unique nonce
- ECB
- GCM
Show answer
ECB encrypts each block independently and with the same key: the same plaintext block always gives the same ciphertext block. Areas of the same colour in the image turn into the same blocks, and the shape stays visible. CBC and CTR use chaining or a counter, so identical blocks come out different. ECB should not be used for data longer than one block.
A cipher has a 40-bit key. An attacker can test 10^9 keys per second. How long does an exhaustive key search take in the worst case?
Show answer
Number of keys .
s minutes. On average, half of that.
For comparison: 56-bit DES at attempts per second takes about 20 hours, and 128-bit AES about years. Each extra bit doubles the time.
A group of 20 people wants every pair to communicate privately using symmetric encryption, with a separate key per pair. How many keys are needed?
Show answer
One key for each pair: .
As the number of people grows, the number of keys grows quadratically (4950 for 100 people). In a public-key system each person holds one key pair: 20 pairs are enough. The key distribution problem is the main motivation for public-key cryptography.
What is the block size of AES?
- 56 bits
- 64 bits
- 128 bits
- 256 bits
Show answer
The AES block size is always 128 bits. The key size can be 128, 192 or 256 bits (10, 12, 14 rounds). 64 bits is the block size of DES, and 56 bits is the effective key size of DES. "AES-256" describes the key, not the block.
Two messages are encrypted with a stream cipher (or one-time pad) using the SAME keystream K. What can an eavesdropper who sees both ciphertexts compute without knowing K?
- K itself, directly
- P1 XOR P2
- The hash of K
- Nothing useful
Show answer
. The key is gone. If part of one plaintext is guessed, the same part of the other and the key at that position are revealed. That is why a one-time pad is used truly once, and in CTR mode the nonce is never repeated.
Kerckhoffs's principle states that a cryptosystem should be secure even if:
- the key is published
- everything about the system except the key is public
- the attacker controls the network
- the algorithm is kept secret
Show answer
Kerckhoffs's principle: security should rest only on the secrecy of the key; the system should stay secure even if the algorithm is known to everyone. Hiding the algorithm (security through obscurity) is broken by reverse engineering. The AES algorithm is public, and its strength comes from the 128-bit key.
Verification: Caesar and Vigenère ciphers, XOR operations, ECB/CBC/CTR traces with a toy 4-bit block cipher (CBC checked back by decryption), key-space times, 26! and pairwise key counts were computed with node (verify/SEN2008/12.mjs). Mode and principle questions are conceptual.