Classic Computer Magazine Archive CREATIVE COMPUTING VOL. 9, NO. 5 / MAY 1983 / PAGE 166

The secret code machine. Gordon Ritchie.

The Secret Code Machine

Millions of dollars are spent every year by governments, east and west, on computers to make and break secret codes. Using a century-old technique on a modern microcomputer, the user of the program described in this article can encipher and decipher secret messages and may also get a taste of the fascinating history of cryptanalysis.

The Playfair Cipher

The ideal cipher should be very simple to construct but impossible to break. One of the simplest, and most effective (though by no means unbreakable with modern methods) is the Playfair Cipher. Invented more than 125 years ago, this cipher retains its utility for casual use today, having made its mark on history in helping save the life of a future president of the United States.

The inventor was a British genius, Charles Wheatstone. Wheastone had many and varied interests. Among other accomplishments, he invented that charming musical instrument, the concertina. He produced some of the earliest stereoscopic or three-dimensional drawings, and he published numerous papers on the science of acoustics. He also improved the electric dynamo and gave his name to a method, still in use today, for the very accurate measurement of electrical resistance.

For his many achievements, Wheatstone was knighted by Queen Victoria. He was elected to the Royal Society, and named professor of experimental philosophy at King's College, London, despite the fact that this remarkable man was so shy he almost never appeared in person to give a lecture.

At the center of his interests lay telegraphy. He constructed an electric telegraph well before Samuel Morse succeeded, and made a contribution to the study of underwater telegraphy. Finally, and most important for this article, he devised an entirely new method for encoding secret messages to be sent by telegraph.

The cipher was ultimately named after Wheatstone's friend, Lyon Playfair, first Baron Playfair of St. Andrews. He first demonstrated what he described as "Wheatstone's newly-discovered symmetrical cypher' at a very private dinner in London in January 1854. The select company that night included Queen Victoria's husband, Prince Albert, and the Home Secretary (and late distinguished Prime Minister), Lord Palmerston. The demonstration was received with great interest. Indeed, several days later, while Playfair was visiting Dublin, he received letters from several of that night's guests, encoded, naturally, in the new cipher which had proved so easy to master.

The event marked the introduction of a method of enciphering letters in pairs such that the result depended upon the interaction of both elements. Today, this is recognized as the first "literal, digraphic' cipher in history.

The Playfair cipher begins with a 5 X 5 grid. Each block of the grid holds one of 25 letters of the alphabet (the j is dropped, to be replaced as needed by an i throughout).

The initial letters of this array, in the usual order (left to right along each row from top to bottom) are taken from a keyword known to both sender and receiver. This can be any word or phrase whatsoever. Its letters are arrayed in order of the first appearance. Thus, the key word "playfair' would be read as "playfir.' The remaining letters of the alphabet follow, in alphabetical order, to make up the codesquare. Thus, using the keyword "playfair,' the codesquare is:






The secret message is processed through this codesquare, in pairs of letters. First, all j's are replaced with i's. Then the message is broken down into pairs. If both letters in any pair are identical, a dummy (an x) is inserted. A dummy is also used to round off the message to an even number of letters. Thus, the short message, "a good cipher is the playfair' becomes.


The rules for encoding a message are simplicity itself. Each letter in a pair is found in the codesquare. It is replaced by the letter on the same row but in the column of the other letter of the pair. Thus, AG becomes LH, OX becomes SV, etc. There are two special cases:

If both letters are on the same row (e.g., YF), each is replaced by the letter to the right, scrolling around to the beginning of the row as required (i.e., YF becomes FP).

If both letters are in the same columm (e.g., EP), each is replaced by the letter below, scrolling to the top of the column (i.e., EP becomes NI).

Our sample message, using the keyword "playfair' thus becomes: LHSVTR DRAEGI CNQMNI AYFPPB CV. (The grouping of letters is purely arbitrary, to mislead prying eyes.) The reader may wish to double check-- it is really rather easy once you understand the method.

The power of the cipher can be seen in the way that O is enciphered S in one place and T another, while A in the ciphertext stands for P in one case and L in the other. The result is that the simplest method of attack on secret ciphers, frequency analysis, is made much more difficult.

The two most frequent letters in English are e and t which together account for more than 20 percent of all normal usage. By contrast, the two most common di-graphs, th and he are found less than 6 percent of the time. This method of enciphering in pairs can be broken by modern methods, to be sure, but not without some skill.

The proof of this came in the rescue of an American sailor who later became president of the United States of America. When PT 109 was rammed and sunk in August 1943, the coastwatchers were on the alert. A member of the Australian naval volunteer reserve hidden in the Solomon Islands saw the explosion.

That morning, he received a message in Playfair cipher using the keyword "Royal New Zealand Navy' which said, "PT boat one owe nine lost in action in Blackett Strait two miles SW Meresu Cove X Crew of twelve X Request any information X.' He reported his sightings and over the next few days he and his brave comrades on different islands exchanged a stream of messages about the probable whereabouts of the survivors.

Five days later, he learned that two natives had, indeed, found the sailors, and he fired off a message, using a Playfair cipher with the keyword "physical examination' to report eleven survivors and call for a rescue. In the heart of Japanese-controlled territory, with the radio waves continuously monitored, the use of a simple Playfair cipher kept the proceedings secret until the rescue could be effected.

The Playfair Program

You don't need to be on a South Sea island surrounded by enemy forces to use and enjoy the Playfair cipher. The following program was written for Boy Scout Martin Ritchie of Ottawa, Canada, as part of his requirement for a green star. The user simply inputs an appropriate key word and the computer does the rest of the work, enciphering or deciphering the secret message.

The program was originally written on the 6502 system of a SuperPet with disk drives and a CBM printer. It can be easily adapted to other Commodore machines or, with a little effort, other 6502-based systems using disk or tape. (A neighbor is running a version on a tape-supported CBM 2032.)

Unlike the Waterloo Basic of the 6809 chip, which the SuperPet also supports, Commodore's Basic 4.0 is not structured, nor does it permit "pretty printing.' The program has, however, been written in modular format, with extensive use of subroutines and a proliferation of REMS to make it easily readable by the novice programmer. Pseudo pretty printing has been used as well.

All this adds to the intelligibility, but also the memory requirement of the program. Users faced with limited memory capacity may wish to shorten the message array (DIM in line 680) as a first step. If the program is to be compressed, care should be taken with branching to REM statements.

The program is relatively foolproof. It also uses input time to do the main processing so that it appears to do the encipherment instantly (a useful technique known as "parallel processing').

Program Organization

Lines 10 to 310 provide a synopsis of the program using REM statements.

Lines 500 to 680 introduce the program to the screen and establish DIM, character sets, etc.

Lines 700 to 1000 accept the keyword input, strip it of j's and repeats and set up the magic square while waiting.

Lines 1020 to 1200 display the magic codesquare for the user.

Lines 1210 to 1350 accept the input of the secret message to be enciphered/ deciphered and do the job while waiting.

Lines 1360 to 1500 print to the screen both the original message and the enciphered/deciphered version.

Lines 2000 to 2150 close the operation with a set of printout options to the CBM printer.

The remaining lines comprise subroutines for everything from turning the page to enciphering the secret message, all clearly marked with REMs. The basic algorithm for enciphering the letter pairs is in the 15000 block, calling subroutines for special cases in the 20000 block.

Further Information

Readers whose appetite has been whetted to learn more of the fascinating history of cryptanalysis are encouraged to get The Codebreakers by David Kahn or Secret and Urgent by Fletcher Pratt. Both provide thrilling accounts of crypt-analysis in action and a good technical guide to many of the basic techniques.

Readers who prefer not to type in the following listing may obtain a copy of the Playfair program--either version: 80-character plus instructions on disk; or 40-character on disk or tape. Just send $5 with your name and address to me at the address printed at the beginning of the article.