SEN2008 · Week 12 · Computer and Network Security: Security Techniques (Encryption, digital signatures, message authentication, and hash functions, Use of cryptography for network security, Protection and defense mechanisms and tools

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.

plaintextciphertextkeysymmetric encryptionKerckhoffs's principlebrute forceCaesar ciphersubstitution cipherfrequency analysisVigenère cipherXORone-time padblock cipherstream cipherDESAESECBCBCCTRinitialization vectornoncekey distribution

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:

  1. Symmetric = one shared key. The one who encrypts and the one who decrypts know the same key.
  2. Convert letters to numbers, take mod 26. A = 0, B = 1, …, Z = 25.
  3. XOR cancels itself. P⊕K⊕K=PP \oplus K \oplus K = P.
  4. Don’t use ECB. The same block gives the same output, and patterns leak.
  5. Don’t reuse the key. In OTP and CTR the keystream is used once.

Basic concepts

The plaintext PP is turned into ciphertext CC by the encryption algorithm EE and key KK: C=EK(P)C = E_K(P). Decryption: P=DK(C)P = D_K(C). In symmetric encryption both sides have the same KK.

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 kk-bit key, the worst case is 2k2^k attempts and the average is 2k−12^{k-1}.

Classical ciphers

The Caesar (shift) cipher

Each letter is shifted kk steps in the alphabet:

C=(P+k) mod 26P=(C−k) mod 26C = (P + k) \bmod 26 \qquad P = (C - k) \bmod 26

Example: NETWORK, k=3k = 3.

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 26!≈4×102626! \approx 4 \times 10^{26} 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 (⊕\oplus): 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, C1⊕C2=P1⊕P2C_1 \oplus C_2 = P_1 \oplus P_2 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).

DESAES
Block64 bits128 bits
Key56 bits (effective)128 / 192 / 256 bits
Rounds1610 / 12 / 14
StatusBroken by brute forceToday’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 (101210^{12} 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: EK(x)E_K(x) = rotate x⊕Kx \oplus K left by 1 bit. K=0110K = 0110, plaintext blocks 1010, 1010, 00111010,\ 1010,\ 0011.

ECB (electronic codebook)

Each block is independent: Ci=EK(Pi)C_i = E_K(P_i).

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: Ci=EK(Pi⊕Ci−1)C_i = E_K(P_i \oplus C_{i-1}), C0=IVC_0 = IV. IV=1001IV = 1001:

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: Pi=DK(Ci)⊕Ci−1P_i = D_K(C_i) \oplus C_{i-1}. 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: Ci=Pi⊕EK(nonce+i)C_i = P_i \oplus E_K(\text{nonce} + i). The block cipher becomes a stream cipher. Let the counter start at 01000100:

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 EE 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 nn people each want a separate key for every pair:

n(n−1)2 keysn=50⇒1225,n=100⇒4950\frac{n(n-1)}{2} \text{ keys} \qquad n = 50 \Rightarrow 1225, \quad n = 100 \Rightarrow 4950

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 ideaCorrect
In Caesar, Z + 3 = ”]”Mod 26: Z(25) + 3 = 28 → 2 = C
Large key space = secureSubstitution with 26! keys is broken by frequency analysis
In Vigenère the key is written onceIt is repeated along the message
An OTP key can be reusedIf repeated, C1⊕C2=P1⊕P2C_1 \oplus C_2 = P_1 \oplus P_2 leaks
The AES-256 block is 256 bitsThe block is always 128 bits; 256 is the key size
ECB is secure because it uses AESIf the mode is weak, identical blocks come out identical
In CBC, XOR with the previous plaintext blockWith the previous ciphertext block (the IV for the first block)
In CTR the nonce can be reusedRepeating the nonce with the same key repeats the keystream
Hiding the algorithm increases securityKerckhoffs: security lies only in the key
A key 1 bit longer is slightly more secureEach extra bit doubles the brute-force time

Test yourself

Exam-style questions. Solve them on paper first, then reveal the answer.

Question 1easy

Encrypt the word ATTACK with a Caesar (shift) cipher using key k = 7.

Show answer
HAAHJR

Rule: C=(P+k) mod 26C = (P + k) \bmod 26, 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).

Question 2easy

The ciphertext KHOOR was produced by a Caesar cipher with key 3. Recover the plaintext.

Show answer
HELLO

Decryption: P=(C−k) mod 26P = (C - k) \bmod 26. 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.

Question 3medium

Encrypt PACKET with the Vigenère cipher using the keyword LOCK (A = 0).

Show answer
AOEUPH

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.

Question 4easy

Compute the one-time pad ciphertext C = P XOR K for P = 11001010 and K = 01010011.

Show answer
10011001

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

C=10011001C = 10011001. Check: C⊕K=11001010=PC \oplus K = 11001010 = P. Applying XOR twice cancels itself out, so decryption is the same operation.

Question 5medium

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
0101

First the XOR: 1100⊕0110=10101100 \oplus 0110 = 1010.

Rotate left by 1 bit: the leftmost 1 moves to the far right. 1010→01011010 \to 0101.

In ECB every block is encrypted independently, and that's all. The same block always gives 0101.

Question 6medium

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
0011

CBC: C1=EK(P1⊕IV)C_1 = E_K(P_1 \oplus IV).

P1⊕IV=1100⊕0011=1111P_1 \oplus IV = 1100 \oplus 0011 = 1111.

1111⊕K=1111⊕0110=10011111 \oplus K = 1111 \oplus 0110 = 1001, rotate left: 1001→00111001 \to 0011.

C1=0011C_1 = 0011. In the previous question the same block gave 0101 in ECB; the IV changed the result.

Question 7easy

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?

  1. CBC with a random IV
  2. CTR with a unique nonce
  3. ECB
  4. GCM
Show answer
C) ECB

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.

Question 8medium

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
about 1100 s (about 18.3 minutes)

Number of keys 240=1 099 511 627 776≈1.1×10122^{40} = 1\,099\,511\,627\,776 \approx 1.1 \times 10^{12}.

1.1×1012/109≈11001.1 \times 10^{12} / 10^9 \approx 1100 s ≈18.3\approx 18.3 minutes. On average, half of that.

For comparison: 56-bit DES at 101210^{12} attempts per second takes about 20 hours, and 128-bit AES about 101910^{19} years. Each extra bit doubles the time.

Question 9medium

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
190

One key for each pair: (n2)=n(n−1)2=20⋅192=190\binom{n}{2} = \frac{n(n-1)}{2} = \frac{20 \cdot 19}{2} = 190.

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.

Question 10easy

What is the block size of AES?

  1. 56 bits
  2. 64 bits
  3. 128 bits
  4. 256 bits
Show answer
C) 128 bits

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.

Question 11hard

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?

  1. K itself, directly
  2. P1 XOR P2
  3. The hash of K
  4. Nothing useful
Show answer
B) P1 XOR P2

C1⊕C2=(P1⊕K)⊕(P2⊕K)=P1⊕P2C_1 \oplus C_2 = (P_1 \oplus K) \oplus (P_2 \oplus K) = P_1 \oplus P_2. 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.

Question 12medium

Kerckhoffs's principle states that a cryptosystem should be secure even if:

  1. the key is published
  2. everything about the system except the key is public
  3. the attacker controls the network
  4. the algorithm is kept secret
Show answer
B) everything about the system except the key is public

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.

Review card

Symmetric: the same key encrypts and decrypts. Kerckhoffs: only the key is secret Caesar: C = (P + k) mod 26, P = (C − k) mod 26, A = 0. 25 keys → brute force Monoalphabetic substitution: 26! ≈ 4×10^26 keys but broken by frequency analysis Vigenère: the keyword is repeated, add letter by letter mod 26 XOR: same 0, different 1. C = P ⊕ K, P = C ⊕ K OTP: key random, as long as the message, USED ONCE. C1 ⊕ C2 = P1 ⊕ P2 DES: 64-bit block, 56-bit key (broken). 3DES as a stopgap AES: 128-bit block, 128/192/256 key, 10/12/14 rounds Brute-force time = 2^bits / rate; each extra bit ×2 ECB: C_i = E(P_i) → same block same output (don't use) CBC: C_i = E(P_i ⊕ C_{i−1}), C_0 = IV (random) CTR: C_i = P_i ⊕ E(nonce + i), parallel, nonce never repeats Symmetric pairwise keys: n(n−1)/2 (20 people → 190)