Computer Networking › Section H · Security in computer networks › Topic H.2
Principles of cryptography
Cryptography lets Alice turn a message into something Trudy cannot read, using a key only the right people hold. This topic follows the idea from letter-shifting ciphers to the block ciphers, public-key systems and key agreement that protect network traffic today.
Keys, ciphertext and the attacker's view
Alice applies an encryption algorithm with key \(K_A\) to plaintext \(m\), producing ciphertext \(K_A(m)\). Bob applies the decryption algorithm with key \(K_B\) and recovers \(K_B(K_A(m)) = m\). In symmetric-key systems the two keys are the same secret; in public-key systems they differ. Following Kerckhoffs's principle, only the keys are secret. Ciphers are judged by what Trudy has: in a ciphertext-only attack, intercepted ciphertext alone; in a known-plaintext attack, some plaintext and ciphertext pairs too; in a chosen-plaintext attack, the encryptions of messages she chooses. A good cipher resists all three.
Substitution ciphers and frequency analysis
The Caesar cipher in "Cybersecurity: encryption and authentication" fell to brute force because it has only 25 keys. A monoalphabetic cipher fixes that by letting each letter map to any other letter, as long as no two share a target. There are 26! orderings, about \(4.0 \times 10^{26}\) keys, far too many to try. Yet ciphertext alone breaks it. Each plaintext letter always becomes the same ciphertext letter, so English letter frequencies survive: the commonest ciphertext letter probably stands for E. A huge key space is necessary but not sufficient.
Worked example: a Vigenère cipher
A polyalphabetic cipher uses several shifts in rotation. Number the letters A = 0 to Z = 25 and take the key KEY, meaning shifts of 10, 4 and 24, repeated. Encrypt SEESAW. 1. S (18) + 10 = 28, and 28 mod 26 = 2, giving C. 2. E (4) + 4 = 8, giving I. 3. E (4) + 24 = 28, giving C. 4. The key repeats: S + 10 gives C again. 5. A (0) + 4 = 4, giving E. 6. W (22) + 24 = 46, and 46 mod 26 = 20, giving U. The ciphertext is CICCEU. Check: subtracting the shifts restores SEESAW. The two E's became different letters and C stands for both S and E, so letter counts mislead. But once Trudy finds the key length, each position is just a Caesar cipher.
Block ciphers: DES and AES
Modern symmetric ciphers work on bits. A block cipher splits the message into fixed-size blocks and maps each block to a ciphertext block of the same size, in a way that depends on the key. Real designs use many rounds of key-dependent substitution and mixing. DES, standardised in 1977, used 64-bit blocks and a 56-bit key, about \(7.2 \times 10^{16}\) keys; by 1998 a purpose-built machine could find one in days, and DES was withdrawn in 2005. Triple DES lingered, but NIST disallowed it for encryption after 2023. AES, adopted in 2001, uses 128-bit blocks with 128, 192 or 256-bit keys.
Why blocks must be chained
Encrypting each block on its own, called ECB mode, has a flaw: identical plaintext blocks always give identical ciphertext blocks, so repeated structure such as standard headers or blank areas of an image shows through. Cipher block chaining (CBC) fixes this. Before encrypting block \(m_i\), the sender XORs it with the previous ciphertext block:
The first block is XORed with a random initialisation vector (IV), sent in the clear, so the same message encrypted twice looks different each time. Current protocols prefer authenticated modes such as AES-GCM, or the stream cipher ChaCha20 with Poly1305, which hide content and detect tampering together.
Public-key encryption
Symmetric ciphers leave one problem: Alice and Bob must already share a key, which is awkward when they have never met. Public-key cryptography, proposed by Whitfield Diffie and Martin Hellman in 1976, removes it. Bob has a public key \(K_B^+\) that anyone may use and a private key \(K_B^-\) that only he holds, with \(K_B^-(K_B^+(m)) = m\). Anyone can encrypt to Bob, but only Bob can decrypt, and it must be infeasible to work out \(K_B^-\) from \(K_B^+\). RSA, from 1977, achieves this because factoring large numbers is believed to be hard; the mathematics is set out in "Public-key cryptography" in Computer Science.
Worked example: RSA with tiny numbers
1. Choose primes p = 11 and q = 23, so n = 253 and \(\varphi = 10 \times 22 = 220\). 2. Choose e = 3, which shares no factor with 220. 3. Find d with \(ed \bmod 220 = 1\): d = 147, since \(3 \times 147 = 441 = 2 \times 220 + 1\). Bob's public key is (253, 3) and his private key is 147. 4. Alice encrypts m = 20: \(20^3 = 8000 = 31 \times 253 + 157\), so c = 157. 5. Bob computes pow(157, 147, 253) in Python, which returns 20. Check: the message is back, and Alice never needed d. Real moduli are 2,048 bits or more, and real RSA adds random padding.
Diffie-Hellman key agreement
Diffie-Hellman lets two parties create a shared secret over a public channel without encrypting anything. They agree openly on a large prime p and a base g. Alice picks a secret a and sends \(A = g^a \bmod p\); Bob picks a secret b and sends \(B = g^b \bmod p\). Alice computes \(B^a \bmod p\) and Bob computes \(A^b \bmod p\), and both equal \(g^{ab} \bmod p\). Trudy sees p, g, A and B, but finding a from A, the discrete logarithm problem, is infeasible for large p. Plain Diffie-Hellman does not authenticate anyone, so a man in the middle could run separate exchanges with each side; real protocols sign the values, as TLS does.
Worked example: Diffie-Hellman with tiny numbers
Public values: p = 29 and g = 2. 1. Alice's secret is a = 9: \(2^9 = 512 = 17 \times 29 + 19\), so she sends A = 19. 2. Bob's secret is b = 12: \(2^{12} = 4096 = 141 \times 29 + 7\), so he sends B = 7. 3. Alice computes pow(7, 9, 29) = 20. 4. Bob computes pow(19, 12, 29) = 20. Both now hold 20, which never crossed the network. Check: both results equal \(2^{108} \bmod 29\), since 9 × 12 = 108. With p = 29 Trudy could try all 28 exponents at once; real exchanges use primes of 2,048 bits or more, or elliptic curves such as X25519.
Session keys and the road ahead
Public-key operations are far slower than symmetric ones, so protocols combine the two. RSA or Diffie-Hellman is used once, to agree or deliver a fresh symmetric session key, and AES or ChaCha20 then encrypts the bulk data with it. This hybrid pattern recurs in e-mail, TLS, IPsec and Wi-Fi. A large quantum computer could break RSA, Diffie-Hellman and elliptic curves, though not well-sized symmetric ciphers. NIST published its first post-quantum standards in 2024, including the key-establishment method ML-KEM, and as of 2026 browsers already combine it with X25519 in hybrid key exchanges.
Common mistakes to avoid
First, assuming a large key space guarantees security; monoalphabetic ciphers have about \(4 \times 10^{26}\) keys and still fall to frequency analysis. Second, using ECB mode, which leaks repeated blocks. Third, reusing or predicting a CBC initialisation vector. Fourth, computing the RSA exponent d modulo n instead of modulo \(\varphi\). Fifth, believing plain Diffie-Hellman authenticates the other party. Sixth, encrypting bulk data with public-key methods. Seventh, choosing DES or Triple DES for new systems.
Key takeaways
Algorithms are public and keys secret, and ciphers must resist ciphertext-only, known-plaintext and chosen-plaintext attacks. Substitution ciphers leak letter frequencies; Vigenère's rotating shifts only delay analysis. Block ciphers such as AES encrypt fixed-size blocks; DES's 56-bit key is obsolete. CBC with a random IV, or an authenticated mode such as AES-GCM, hides repeated blocks. RSA lets anyone encrypt to a key owner, and Diffie-Hellman agrees a shared secret in public. Protocols use them to set up fast symmetric session keys.
Flashcards
Flashcard
Decrypting the ciphertext restores the plaintext
Test your understanding
Question 1 of 5
Using A = 0 to Z = 25 and the Vigenère key BAD (shifts 1, 0, 3), what is the encryption of CAT?
Question 2 of 5
A monoalphabetic substitution cipher has about 4 × 10²⁶ possible keys. Why is it still easy to break?
Question 3 of 5
Why is cipher block chaining preferred to encrypting every block independently?
Question 4 of 5
In a Diffie-Hellman exchange with p = 23 and g = 5, Alice's secret is a = 2 and Bob sends B = 10. What shared secret does Alice compute?
Question 5 of 5
Why do protocols use a public-key method only to set up a session key, then switch to a symmetric cipher?
