The ZX80 as a Cipher Machine

See all articles from SYNC v2 n1

[Figure: Engraved portrait of Blaise de Vigenere]

Would you believe that you can turn your ZX80 into a code machine? It can translate messages into gibberish that would take an expert hours to solve, yet a fellow ZX80 user could translate back into English in a few seconds.

You can do this by using the Vigenere cipher. This is probably the most famous cipher of all time, and is named for Blaise de Vigenere, the Frenchman who first described it in 1586. According to legend, he called it le chiffre indechiffrable, the indecipherable cipher. By this he meant that if a secret message using this method of encoding fell into unfriendly hands, it would be impossible for the enemy to break it. Although today it can be broken, the process is hard, long, and tedious. It can be done only by experts in cryptology who have long messages to work with. Such personalites as Lewis Carroll and Sir Admiral Francis Beaufort were interested in this cipher. The Confederate Army used it extensively. Although the military does not use the cipher today, it remains the basis of some ciphers.

The Vigenere cipher was one of the first polyalphabetic ciphers, as opposed to monoalphabetic ciphers used since the time of Caesar. In a monoalphabetic cipher, each letter in the message is replaced by a unique letter in the cipher; e.g., A may stand for F, D may stand for R, and so on. In a polyalphabetic cipher, any letter may replace the message letter. This makes any attempt to break the cipher very difficult.

[Figure: Figure 1. The Vigenere Tableau (26 x 26 table of shifted alphabets; plain-text letter across the top, key letter down the side)]

The basis of the Vigenere cipher is a table known as the Vigenere tableau (what else?) shown in Figure 1. Although there are several versions of the cipher, this article deals with only two. The first uses a keyword known to both the sender and receiver. In the program the prompt will ask you to INPUT KEYWORD (1-12 LETTERS). Naturally, you may change the keyword whenever and as often as you wish as long as your receiver knows the change. To demonstrate the program, let us suppose that you must for some obscure reason send this message to a fellow ZX80 user: CRASH CURSOR IS AS BRIGHT AS A BLACK HOLE. When the screen calls for the keyword, enter SYNC. The display will prompt: INPUT TEXT. On 1K RAM the number of letters and spaces (no punctuation marks) you can enter without overloading the memory is about 52. Of course, you can do longer messages by breaking them up into units of fewer than 52 characters. You will then be asked: IS THIS A CLEAR OR A CIPHER? Clear is the technical term for your message. Type it in. In a few seconds the screen will appear as in Figure 2. Copy down the cipher, answer NO to get out of the program, or YES if you have some more messages. Send the message to your friend who will follow the same routine, except that he types in CIPHER when asked, and in a few seconds he will read your message.

Figure 2. Sample Run

KEYWORD:  SYNC

CLEAR:  CRASH CURSOR IS AS BRIGH
T AS A BLACK HOLE

CIPHER:  UPNUZ AHTKME KK YF DJGT
JL YF C TJNEC FBNW

DO YOU WISH TO CONTINUE?

The other type of Vigenere cipher we will consider is based on one letter and is a variation of the autokey system, so called because the key letter changes automatically in the message. To understand this cipher better, let us manually encipher the above message. To make the cipher even harder to break, divide the message into five-letter groups (this can be done with the keyword type also): CRASH CURSO RISAS BRIGH TASAB LACKH OLE. To encipher this you must first pick a key letter both you and your receiver have agreed upon in advance. Picking S as the key letter, we take the first letter C. Look down the plain-text column in Figure 1 to where it meets the S key letter row. At the intersection is U. This is the first letter of your message. Repeat this process with each letter and eventually you will have: ULLDK MGXPD UCUUM NEMSZ SSKKL WWYIP DOS. Since the key letter and the cipher itself changes throughout the message, it is both extremely difficult to break and extremely difficult to encipher and to decipher. However, the ZX80 removes that difficulty. Make the modifications in the program in Listing 1 as shown in Listing 2. The procedure on the computer is the same as for the keyword except that you will INPUT a key letter. With the changes you will have about 90 letters and spaces for one message in 1K RAM.

Now let us see how the program works. If you closely study the tableau, you will see that the key letter A row does not really change the message. The key letter B row actually moves the message letter up one—message letter A becomes cipher letter B, B becomes C, etc. The key letter C row moves the message letter up two. The message letter then is moved the number of letters the key letter is beyond A. Also notice that when you have to move a letter so that it is beyond Z, the table wraps around back to A. So to program this I made use of the ZX80’s character code functions that allow it to treat letters as numbers as the basis of the program. When the key letter is INPUTed, its character code -38 is stored in Y (see Listing 2). This gives the number a range of A=0 to Z=25. To encipher, the program takes the code of the first letter and adds it to the value of Y. If the resulting sum is greater than 63 (the character code for Z), the program wraps around, adding the value beyond 63 to 37. It then prints the character of the manipulated variable B and goes on the next letter, exchanging the present value of Y for the value of B-38. Deciphering is accomplished in a similar manner. The program duplicates the functioning of the cipher table.

For further information on the Vigenere cipher and other ciphers and codes, I would suggest checking:

Codes, Ciphers, and Secret Writing by Martin Gardner (a good beginner’s book).

The Codebreakers by David Kahn (a mammoth book describing in great detail the entire history of cryptology and cryptologists).

And, of course, encyclopedia articles on codes and ciphers provide a quick introduction.

8K Version

The program can be converted to 8K by making the changes in Listing 3. However, for 8K you will need additional memory.

Listing 1. Vigenere Cipher Program Keyword Form (4K ROM; 1K RAM)

  1 PRINT "VIGENERE CIPHER"
2 PRINT
3 PRINT
4 DIM A(12)
5 PRINT "INPUT KEYWORD (1-12 L
ETTERS)"
10 INPUT A$
11 CLS
12 LET E$=A$
20 FOR Y=1 TO 12
30 LET A(Y)=CODE(A$)-38
40 LET A$=TL$(A$)
45 IF CODE(A$)=1 THEN GO TO 55
50 NEXT Y
55 PRINT "INPUT TEXT"
60 INPUT C$
61 CLS
70 LET T=1
71 PRINT "IS THIS A CLEAR OR A#
CIPHER?"
72 INPUT Z$
73 CLS
74 PRINT "KEYWORD:##";E$
75 PRINT
76 PRINT Z$;":"
77 PRINT C$
78 PRINT
79 IF Z$="CIPHER" THEN GO TO 16
0
80 PRINT "CIPHER:"
83 LET B=CODE(C$)+A(T)
85 IF CODE(C$)=0 THEN LET B=0
90 IF B>63 THEN LET B=(B-63)+37
100 PRINT CHR$(B);
110 IF NOT B=0 THEN LET T=T+1
120 IF T>Y THEN LET T=1
130 LET C$=TL$(C$)
140 IF NOT CODE(C$)=1 THEN GO TO
83
150 GO TO 230
160 PRINT "CLEAR:"
165 LET B=CODE(C$)-A(T)
167 IF CODE(C$)=0 THEN LET B=0
170 IF B<38 AND B>0 THEN LET B=6
4-(38-B)
180 PRINT CHR$(B);
190 IF NOT B=0 THEN LET T=T+1
200 IF T>Y THEN LET T=1
210 LET C$=TL$(C$)
220 IF NOT CODE(C$)=1 THEN GO TO
165
230 PRINT
235 PRINT
240 PRINT "DO YOU WISH TO CONTIN
UE?"
260 INPUT W
270 CLS
280 RUN

Notes:

  4 Array for character codes
    of keyword.
  5 Underline indicates use of
    word from key board if
    desired to save memory.
 10 Keyword.
 12 Saves keyword for later dis-
    play.
 20 Loop to load keyword charac-
    ter codes into array.
 45 Takes program out of loop
    for keywords of fewer than
    12 letters.
 70 Initializes a value used in
    coding/decoding routines.
 72 Nature of text.
 74 Displays keyword.
 76 Displays, identifies text.
 79 Check if coding or decoding
    routine is to be used.
 83 Steps up letter according
    to value in array.
 85 Checks for space in message.
 90 Loops around if coded let-
    ter Z.
100 Prints coded letter.
110 Increments keyword letter
    array unless space.
120 Loops around keyword if end
    is reached.
130 Next letter.
140 Checks for end of text.
160 Decoding routine is essen-
 to tially the reverse of the
210 coding routine.

Listing 2. Vigenere Cipher Program Modification for Key Letter Form

DELETE Lines 4, 12, 30, 40, 45, 50, 70, 120, 200.

Make the following line changes:

  5 PRINT "INPUT KEY LETTER"
20 LET Y=CODE(A$)-38
74 PRINT "KEY LETTER:##";A$
83 LET B=CODE(C$)+Y
110 IF NOT CODE(C$)=0 THEN LET Y
=B-38
165 LET B=CODE(C$)-Y
190 IF NOT CODE(C$)=0 THEN LET Y
=CODE(C$)-38

Listing 3. Vigenere Cipher Program Modifications Key Letter Form (8K ROM; over 1K RAM)

  1 PRINT "VIGENERE CIPHER"
2 PRINT
3 PRINT
5 PRINT " INPUT KEY LETTER"
10 INPUT A$
11 CLS
20 LET Y=CODE A$-38
55 PRINT " INPUT TEXT"
60 INPUT C$
61 CLS
71 PRINT "IS THIS A CLEAR OR C
IPHER?"
72 INPUT Z$
73 CLS
74 PRINT "KEY LETTER: ";A$
75 PRINT
76 PRINT Z$;":"
77 PRINT C$
78 PRINT
79 IF Z$="CIPHER" THEN GOTO 16
0
80 PRINT "CIPHER:"
83 LET B=CODE C$+Y
85 IF CODE C$=0 THEN LET B=0
90 IF B>63 THEN LET B=(B-63)+3
7
100 PRINT CHR$ B;
110 IF NOT CODE C$=0 THEN LET Y
=B-38
130 LET C$=C$(2 TO )
140 IF NOT C$="" THEN GOTO 83
150 GOTO 230
160 PRINT " CLEAR :"
165 LET B=CODE C$-Y
167 IF CODE C$=0 THEN LET B=0
170 IF B<38 AND B>0 THEN LET B=
64-(38-B)
180 PRINT CHR$ B;
190 IF NOT CODE C$=0 THEN LET Y
=CODE C$-38
210 LET C$=C$(2 TO )
220 IF NOT C$="" THEN GOTO 165
230 PRINT
235 PRINT
240 PRINT "DO YOU WISH TO CONTI
NUE?"
260 INPUT W
270 CLS
280 RUN

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.