Chapter 11 of 22 Part III: Cybersecurity and Privacy

The Art of Secret Messages

Each project below is tailored to a specific algorithm and scientific question from Chapter 11, with concrete inputs, verifiable numerical outputs, boundary cases, and links to the related lab and pioneer.

Project 1 of 3 · v1-c11-p01 Foundational · ~25 min

Caesar Cipher & Automated Frequency Breaker

Lab activity: A. The secret alphabet · Related code: block-01.js · Pioneer: View profile

Real-World Problem: Historical ciphers hid messages by shifting letters, yet cryptanalysts broke them systematically without the key.
Scientific Question: Why does preserving single-letter frequency distributions make monoalphabetic shift ciphers trivially breakable?

Objective: Encrypt text with a Caesar shift and write an automated breaker using English letter frequency scoring.

Hypothesis to Test: Scoring all 26 candidate shifts against English letter frequencies (E, T, A, O, I, N) ranks the true key #1 on sentences of 30+ letters.

Step-by-Step Implementation

  1. Implement shiftText(str, k) wrapping A–Z modulo 26 while preserving spaces/punctuation.
  2. Compute letter frequency histograms for all 26 shifts.
  3. Score each candidate against reference English frequencies using chi-squared distance.
  4. Test a very short 3-letter word ("CAT") to see when frequency statistics become ambiguous.
Concrete Input: Ciphertext: "WKH TXLFN EURZQ IRA MXPSV RYHU WKH ODCB GRJ" (shift = 3).
Expected Output: Top-ranked key = 3 -> "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG".

Test Cases: Shift 3 sentence -> recovered shift 3; Shift 0 -> unchanged; Shift 26 -> identical to shift 0.

Boundary Condition: Very short ciphertexts (< 10 letters) may not match average English frequencies.

Common Mistake to Avoid: Using JavaScript % on negative numbers without adding 26 first ((-3 % 26) === -3).

Interpreting Results: Security must never rely on tiny keyspaces or statistics-preserving substitutions.

Show Hint, Extension Challenge & Reflection

Hint: Review the worked example and equation in Chapter 11 before testing the boundary case: Very short ciphertexts (< 10 letters) may not match average English frequencies.

Extension Challenge: Compare Caesar breaking against a Vigenère polyalphabetic cipher of key length 4.

Reflection Question: Why did Alan Turing and Bletchley Park cryptanalysts emphasize mathematical structure over guesswork?

Project 2 of 3 · v1-c11-p02 Intermediate · ~30 min

Diffie-Hellman Public Key Agreement Simulator

Lab activity: B. The pattern detective · Related code: block-02.js · Pioneer: View profile

Real-World Problem: Two strangers connecting over the public Internet need a shared secret key without ever meeting in person.
Scientific Question: How does modular exponentiation create a one-way trapdoor that lets two parties derive the same secret publicly?

Objective: Implement modular exponentiation (modPow) with BigInt, run a Diffie-Hellman exchange, and verify an eavesdropper cannot trivially multiply public values.

Hypothesis to Test: Alice's (B^a mod p) and Bob's (A^b mod p) always produce the exact same shared secret g^(ab) mod p.

Step-by-Step Implementation

  1. Implement fast binary modular exponentiation modPow(base, exp, mod) using BigInt.
  2. Compute Alice's public A = g^a mod p and Bob's public B = g^b mod p.
  3. Verify modPow(B, a, p) === modPow(A, b, p).
  4. Demonstrate brute-forcing small p = 23 vs. explaining why production uses 2048-bit groups or X25519.
Concrete Input: p = 23, g = 5, Alice secret a = 6, Bob secret b = 15.
Expected Output: Alice sends A = 8; Bob sends B = 19; Both derive shared secret S = 2.

Test Cases: (p=23, g=5, a=6, b=15) -> A=8, B=19, S=2; (p=97, g=5, a=36, b=58) -> identical S.

Boundary Condition: Using regular JS Number instead of BigInt loses precision past 2^53 − 1.

Common Mistake to Avoid: Computing Math.pow(g, a) % p directly, which overflows floating-point numbers immediately.

Interpreting Results: Whitfield Diffie, Martin Hellman, and Ralph Merkle revolutionized security by separating public exchange from private keys.

Show Hint, Extension Challenge & Reflection

Hint: Review the worked example and equation in Chapter 11 before testing the boundary case: Using regular JS Number instead of BigInt loses precision past 2^53 − 1.

Extension Challenge: Demonstrate a man-in-the-middle attack when public values A and B are not authenticated with signatures.

Reflection Question: Why does HTTPS pair Diffie-Hellman key exchange with digital certificates (Chapter 13)?

Project 3 of 3 · v1-c11-p03 Applied · ~30 min

Web Crypto AES-GCM Authenticated Encryption Lab

Lab activity: C. Agree on a secret · Related code: block-03.js · Pioneer: View profile

Real-World Problem: Real web traffic needs confidentiality AND automatic detection if an attacker flips even one ciphertext bit in transit.
Scientific Question: What happens when a single bit of an AES-GCM ciphertext or 96-bit IV is modified before decryption?

Objective: Encrypt and decrypt messages using browser Web Crypto AES-256-GCM and verify tamper rejection.

Hypothesis to Test: Flipping any bit of the ciphertext or IV causes AES-GCM decryption to throw an authentication error instead of returning corrupted text.

Step-by-Step Implementation

  1. Run in the Chapter 11 Lab over HTTPS (https://howdigitalworks.dev) or localhost.
  2. Generate a fresh 256-bit AES-GCM key and a unique 12-byte random IV via crypto.getRandomValues.
  3. Encrypt a test message and decrypt it back to exact UTF-8 text.
  4. Flip 1 bit in the ciphertext hex/bytes and confirm decryption rejects the tampered message.
Concrete Input: Plaintext: "Meet Mira at Lab 11", random 256-bit key, fresh 12-byte IV.
Expected Output: Intact ciphertext -> exact plaintext; 1-bit flip -> OperationError (tampering detected).

Test Cases: Untampered -> exact plaintext; 1-bit ciphertext flip -> throws; Wrong IV -> throws.

Boundary Condition: Never reuse the same (key, IV) pair in AES-GCM; nonce reuse breaks confidentiality and authenticity.

Common Mistake to Avoid: Testing AES-GCM with a hardcoded all-zero IV instead of a fresh random 12-byte IV.

Interpreting Results: Modern authenticated encryption (AEAD) protects both privacy and integrity in one vetted standard.

Show Hint, Extension Challenge & Reflection

Hint: Review the worked example and equation in Chapter 11 before testing the boundary case: Never reuse the same (key, IV) pair in AES-GCM; nonce reuse breaks confidentiality and authenticity.

Extension Challenge: Inspect why two encryptions of the exact same plaintext produce completely different ciphertexts when using fresh random IVs.

Reflection Question: Why should developers always use vetted APIs like Web Crypto rather than inventing custom ciphers?