I wrote about the multiplication code used by the double agent Eddie Chapman, known as Agent Zigzag. By using a memorised keyword and the date, Chapman could produce a unique numerical shift key for his cipher every day, leaving no physical evidence for counter-intelligence to find.

To show how this works in a modern context, I have translated this manual process into Python. The script replicates the original algorithm and includes functions to handle the keyword, the daily shift, and the encryption process.

How the code works

  • Sorting with duplicates: The process_keyword function pairs each letter with its original index before sorting. Since Python uses a stable sorting algorithm, duplicate letters are numbered from left to right as they appear in the word.
  • Alphabet wrapping: The line chr(((ord(char) - ord('A') + shift) % 26) + ord('A')) manages the wrap-around. It converts a character to a 0–25 index, adds the shift, and uses the modulo operator to loop from ‘Z’ back to ‘A’.
  • Grouping for transmission: The script divides the output into blocks of five characters. This follows the Morse code format used by operatives in the field.