Encodes or decodes a typed message using a key number from 1 to 65535. The key seeds the random-number generator, and each letter, digit or symbol between 0 and z is shifted by a pseudo-random amount and wrapped round within that range; spaces and other characters pass through unchanged. Decoding with the same key reverses the shifts, so both parties only need to agree on the number. The result can be shown on screen only or also sent to the printer, and afterwards you can copy the screen, start a new message, or encode another message continuing with the same key sequence.
A REM in line 8 describes it as the “2068 vers. of Cambridge Crypto adapted from Explorers Guide to the ZX81”, the book The Explorer’s Guide to the ZX-81 and Timex Sinclair 1000 by Mike Lord. The opening screen compares it with the Enigma machine of the Second World War. Who made the 2068 version is not recorded.
Program Analysis
Structure
- Lines 1–9: a five-second pause, the introduction, and
POKE 23658,8to turn on CAPS LOCK. - Lines 10–109: choose encode (7) or decode (8), the key, and screen or hard copy.
- Lines 110–210: the cipher loop over the message.
- Lines 211–230: C copies the screen, Q clears the program with
NEW, R starts again, K takes another message without reseeding.
The Cipher
RANDOMIZE K fixes the sequence produced by RND. For every character with a code from 48 to 122, X=INT (74*RND) is drawn; encoding subtracts it and decoding adds it, and line 190 adds or subtracts 74 to wrap the result back into the 48–122 range. Because the decoder draws the same sequence of X values in the same order, the shifts cancel.
Notable Techniques
- The Sinclair random-number generator is deterministic from its seed, so
RANDOMIZEwith the key turns it into a repeatable key stream. - Line 190 wraps the code with
(74 AND C<48)-(74 AND C>122), a Sinclair BASIC idiom whereANDreturns the left value when the condition is true and 0 otherwise. - Characters outside 48–122 skip the
RNDcall, so spaces do not use up a key value and stay in place in the coded text.
Potential Issues
- Option K continues the random sequence rather than restarting it, so a message encoded with K can only be decoded by repeating the same sequence of messages from the start.
- Option Q uses
NEW, which erases the program from memory. - With CAPS LOCK on, lower-case letters are not normally typed, although the cipher would accept and produce them, along with symbols such as
[and^.
This program came from an Oliger (JLO SAFE) disk and was converted to load from tape. It needed no changes and was tested in ZEsarUX.
Source Code
1 BORDER 0
2 PAUSE 500: CLS
3 PRINT "THE PURPOSE OF THIS PROGRAM IS TO ENCODE OR DECODE MESSAGES. THIS PROGRAM EMULATES THE ENIGMA MACHINE OF WORLD WAR TWO. YOU WILL BE ASKED TO SUPPLY A NUMBER WHICH KEYS IN A PSEUDO-RANDOM SEQUENCE WHICH IS NEEDED TO DECODE THE MESSAGE. IT SHOULD BE MORE FUN THAN WORLDWAR THREE."
4 PRINT ''"PRESS ANY KEY TO CONTINUE": PAUSE 0
8 REM 2068 VERS. OF CAMBRIDGE CRYPTO ADAPTED FROM EXPLORERS GUIDE TO THE ZX81
9 CLS : POKE 23658,8
10 PRINT "PLEASE ENTER"
20 PRINT "THE NUMBER 7 TO ENCODE","THE NUMBER 8 TO DECODE"
30 INPUT E$
40 IF E$<>"7" AND E$<>"8" THEN GO TO 30
50 PRINT E$
60 PRINT " ENTER KEY (1-65535)"
70 INPUT K: PRINT K
80 IF (K>65535 OR K<1) THEN GO TO 70
90 RANDOMIZE K
100 CLS
108 PRINT ''"DO YOU WISH TO HAVE THIS MESSAGE ON PAPER ?"''"ENTER 1 FOR HARD COPY","ENTER 2 FOR SCREEN DISPLAY ONLY"
109 INPUT Z$: PRINT Z$
110 CLS : PRINT "ENTER MESSAGE"
120 INPUT I$
130 PRINT I$
140 FOR A=1 TO LEN I$
150 LET C=CODE I$(A)
160 IF C<48 OR C>122 THEN GO TO 209
170 LET X=INT (74*RND)
180 IF E$="7" THEN LET X=-X
185 LET C=C+X
190 LET C=C+(74 AND C<48)-(74 AND C>122)
209 IF Z$="1" THEN LPRINT CHR$ C;
210 PRINT CHR$ C;: NEXT A: IF Z$="1" THEN LPRINT
211 PRINT ''"HIT 'C' TO COPY HIT 'Q' TO QUITHIT 'R' FOR ANOTHER MESSAGE HIT 'K' FOR ANOTHER MESSAGE WITHTHE SAME KEY": INPUT K$
212 IF K$="C" THEN COPY
213 IF K$="Q" THEN NEW
214 IF K$="R" THEN GO TO 10
215 IF K$="K" THEN GO TO 100
230 CLS : GO TO 10
250 SAVE "CRYPTO" LINE 0Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

