Excerpt from the novel Cryptonomicon by Neal Stephenson
[Lawrence Pritchard] Waterhouse sketches out some plans for the upcoming work, at long last putting his hitherto misspent engineering skills to some use. He draws up a bill of required materials, naturally involving a good many numbers; 100 8′ 2 x 4s is a typical entry. He writes out the list a second time, in words not numbers: ONE HUNDRED EIGHT FOOT TWO BY FOURS. This wording is potentially confusing, so he changes it to TWO BY FOUR BOARDS ONE HUNDRED COUNT LENGTH EIGHT FEET.
Next he pulls a sheet of what looks like ledger paper, divided vertically into groups of five columns. Into these columns he transcribes the message, ignoring spaces:
T W O B Y F O U R B O A R D S O N E H U N D R E D
C O U N T L E N G T H E I G H T F E E T
and so on. Wherever he encounters a letter J he writes I in its stead, so that JOIST comes out as IOIST. He only uses every third line of the page.
Ever since he left Bletchley Park, he has been carrying several sheets of onionskin paper around in his breast pocket; when he sleeps, he puts them under his pillow. Now he takes them out and selects one page, which has a serial number typed across the top and is otherwise covered with neatly typed letters like this:
A T H O P C O G N Q D L T U I C A P R H M U L E P
and so on, all the way down to the bottom of the page.
These sheets were typed up by a Mrs. Tenney, an aged vicar’s wife who works at Bletchley Park. Mrs. Tenney has a peculiar job which consists of the following: she takes two sheets of onionskin paper and puts a sheet of carbon paper between them and rolls them into a typewriter. She types a serial number at the top. Then she turns the crank on a device used in bingo parlors, consisting of a spherical cage containing twenty-five wooden balls, each with a letter printed on it (the letter J is not used). After spinning the cage the exact number of times specified in the procedure manual, she closes her eyes, reaches through a hatch in the cage, and removes a ball at random. She reads the letter off the ball and types it, then replaces the ball, closes the hatch, and repeats the process. From time to time, serious-looking men come into the room, exchange pleasantries with her, and take away the sheets that she has produced. These sheets end up in the possession of men like Waterhouse, and men in infinitely more desperate and dangerous circumstances, all over the world. They are called the one-time pads.
He copies the letters from the one-time pad into the empty lines beneath his message:
T W O B Y F O U R B O A R D S O N E H U N D R E D
A T H O P C O G N Q D L T U I C A P R H M U L E P
When he is finished, two out of every three lines are occupied.
Finally, he returns to the top of the page one last time and begins to consider the letters two at a time. The first letter in the message is T. The first letter from the one-time pad, directly below it in the same column, is A.
A is the first letter in the alphabet and so Waterhouse, who has been doing this cipher stuff for much too long, thinks of it as being synonymous with the number 1. In the same way, T is equivalent to 19 if you are working in a J-less alphabet. Add 1 to 19 and you get 20, which is the letter U. So, in the first column beneath T and A, Waterhouse writes a U.
The next vertical pair is W and T, or 22 and 19, which in normal arithmetic add up to 41, which has no letter equivalent; it’s too large. But it has been many years since Waterhouse did normal arithmetic. He has retrained his mind to work in modular arithmetic—specifically, modulo 25, which means that you divide everything by 25 and consider only the remainder. 41 divided by 25 is 1 with a remainder of 16. Throw away the 1 and the 16 translates into the letter Q, which is what Waterhouse writes in the second column. In the third column, O and H give 14 + 8 = 22 which is W. In the fourth, B and O give 2 + 14 = 16 which is Q. And in the fifth, Y and P give 24 + 15 which is 39. 39 divided by 25 is 1 with a remainder of 14. Or, as Waterhouse would phrase it, 39 modulo 25 equals 14. The letter for 14 is O. So the first code group looks like
T W O B Y
A T H O P
U Q W Q O
By adding the random sequence ATHOP onto the meaningful sequence TWOBY, Waterhouse has produced undecipherable gibberish. When he has enciphered the entire message in this way, he takes out a new page and copies out only the ciphertext—UQWQO and so on.
The duke has a cast-iron telephone which he has put at Waterhouse’s disposal. Waterhouse heaves it out of its cradle, rings the operator, places a call across the island to the naval station, and gets through to a radio man. He reads the ciphertext message to him letter by letter. The radio man copies it down and informs Waterhouse that it will be transmitted forthwith.
Very soon, Colonel Chattan, down in Bletchley Park, will receive a message that begins with UQWQO and goes on in that vein. Chattan possesses the other copy of Mrs. Tenney’s one-time pad. He will write out the ciphertext first, using every third line. Beneath the ciphertext he will copy in the text from the one-time pad:
U Q W Q O
A T H O P
He will then perform a subtraction where Waterhouse performed an addition. U minus A means 20 minus 1 which equals 19 which gives the letter T. Q minus T means 16 minus 19 which equals -3, giving us 22 which is W. And so on. Having deciphered the whole message, he’ll get to work, and eventually two by fours one hundred count will show up at the Pier.