Chapter 11 of 22 Part III: Trust, Cryptography & Security

The Art of Secret Messages

3 Verified Book Snippets

Every .js file below is byte-identical (SHA-256 verified) to the printed code in Volume 1. Runnable blocks execute inside an isolated sandboxed iframe and Web Worker with a 2-second timeout and zero network access.

Download Chapter ZIP

block-01.js

Printed book snippet (byte-identical) · 316 bytes · SHA-256: 1eacac1f8f5a…
Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated browser sandbox and logs output.

function shift(text, key) {
  return text.replace(/[A-Za-z]/g, letter => {
    const base = letter >= "a" ? 97 : 65;
    const x = letter.charCodeAt(0) - base;
    const y = ((x + key) % 26 + 26) % 26;
    return String.fromCharCode(base + y);
  });
}
console.log(shift("HELLO", 3));
console.log(shift("KHOOR", -3));
Download .js

block-02.js

Printed book snippet (byte-identical) · 287 bytes · SHA-256: 4a121dd12ee1…
Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated browser sandbox and logs output.

function countLetters(text) {
  const counts = {};
  for (const ch of text.toUpperCase()) {
    if (ch >= "A" && ch <= "Z") {
      counts[ch] = (counts[ch] || 0) + 1;
    }
  }
  return Object.entries(counts)
    .sort((a, b) => b[1] - a[1]);
}
console.log(countLetters("XQ XQ XQ AB"));
Download .js

block-03.js

Printed book snippet (byte-identical) · 391 bytes · SHA-256: 5bf2cf630855…
Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated browser sandbox and logs output.

function modPow(base, exp, mod) {
  let result = 1n;
  base %= mod;
  while (exp > 0n) {
    if (exp % 2n === 1n) {
      result = (result * base) % mod;
    }
    base = (base * base) % mod;
    exp /= 2n;
  }
  return result;
}
const p = 23n, g = 5n;
const a = 6n, b = 15n;
const A = modPow(g, a, p);
const B = modPow(g, b, p);
console.log(A, B);
console.log(modPow(B,a,p), modPow(A,b,p));
Download .js