Rail Fence Cipher on Commoodore 64 and TI 99/4A
By Michael Doornbos
- 6 minutes read - 1112 wordsI have a BUNCH of nieces and nephews. Over the years, doing some secret message passing with them around birthdays and holidays has been fun.
The “secret” location of hidden Christmas presents is a favorite.
Traditionally, we’ve used a transposition cipher like the Shift or Ceasar Cipher
For something different, we’re going to use another transposition cipher called the Rail Fence Cipher. It is easy to implement and understand. Plus easy to break, so it’s a good teaching tool.
Simple and breakable on purpose
The Rail Fence Cipher is a form of transposition cipher that jumbles up the order of the letters to create the ciphertext. In its simplest form, the algorithm takes plaintext and writes it diagonally along parallel “rails” of an imaginary fence.
This diagonal writing starts at the top rail, moving downwards and upwards when the bottom or the top is reached, forming a zigzag pattern. The sequence goes from the top left to the bottom right of the imaginary fence, and then it bounces back to go upward. Once the entire plaintext has been looped over the “fence,” the characters are collected row-by-row from top to bottom to produce the ciphertext.
The classic example of the Rail Fence Cipher is the following:
With the plaintext “WE ARE DISCOVERED RUN AT ONCE,” the message is written diagonally up and down on three “rails” of an imaginary fence.
W . . . E . . . C . . . R . . . U . . . O . . .
. E . R . D . S . O . E . E . R . N . T . N . E
. . A . . . I . . . V . . . D . . . A . . . C .
The ciphertext is then read from each rail horizontally. In our example that would be:
dntleehsawffet
To decrypt the message, one needs to arrange the letters in the same zigzag pattern along the “rails” and then read the message from left to right, following the same path used to encode it.
The Rail Fence Cipher is simple to understand and implement but offers minimal security by today’s standards. Nevertheless, it introduces the essential cryptographic principle of transposition, making it a valuable teaching tool for understanding the basic concepts of cryptography. Plus, when you have a pile of kids, doing secret messages using objects they can find around the house and the yard is a lot of fun.
Commodore 64 Version
10 REM RAIL FENCE CIPHER
15 PRINTCHR$(147)
20 R=3:DIM F$(R)
30 INPUT "ENTER THE PLAIN TEXT"; TR$
40 L=LEN(TR$)
50 REM REMOVE SPACES FROM INPUT
60 FOR I=1 TO L
70 IF MID$(TR$, I, 1) <> " " THEN T$=T$+MID$(TR$, I, 1)
80 NEXT I
90 L=LEN(T$)
100 REM MAKE BLANK FENCE
110 FOR CR=0 TO R-1
120 FOR I=0 TO L-1
130 F$(CR)=F$(CR)+"."
140 NEXT I
150 NEXT CR
160 REM FILL FENCE
170 CR=0:CC=0:D=1
180 FOR I=1 TO L
190 F$(CR)=LEFT$(F$(CR), CC)+MID$(T$, I, 1)+RIGHT$(F$(CR), L-CC-1)
200 CC=CC+1
210 CR=CR+D
220 IF CR<0 OR CR>R-1 THEN D=-D: CR=CR+D*2
230 NEXT I
240 REM PRINT FENCE
250 FOR I=0 TO R-1
260 PRINT F$(I)
270 NEXT I
280 REM READ FENCE
290 FOR CR=0 TO R-1
300 FOR I=1 TO L
310 IF MID$(F$(CR), I, 1) <> "." THEN CT$=CT$+MID$(F$(CR), I, 1)
320 NEXT I
330 NEXT CR
340 PRINT "THE CIPHER TEXT IS "
350 PRINT CT$
The C64 version is a pretty straightforward translation of the BASIC version. I’ve added some comments to help explain what’s going on.
- Line 20: Here,
R
is set to 3, representing the number of rows in the “fence.”DIM F$(R)
is creating an arrayF$
with 3 elements to store the rows of the fence. This can be any number of rows you want, but 3 is the minimum. - Line 70: This checks each character in
TR$
and only adds it toT$
if it’s not a space. - Line 90: Updates
L
to the length of the cleaned-up stringT$
. - Line 120: This nested loop iterates over each position in a row.
- Line 130: Appends a period to each row in
F$
to initialize it as empty. - Line 170: Initialize
CR
(Current Row) to 0,CC
(Current Column) to 0, andD
(Direction) to 1. - Line 180: Loop over each character in
T$
. - Line 190: Replace the dot at the current row and column in
F$
with a character fromT$
. - Line 200: Increment the current column.
- Line 210: Move to the next row in the direction specified by
D
. - Line 220: Change direction if you reach the top or bottom of the fence, and correct the current row accordingly.
- Line 250-260: Loop over each row. Print each row of
F$
. - Line 290-320: Loop over each row in
F$
and each column in the row. If the position in the fence is not a period, append it to the cipher textCT$
.
It’s important to note that you can use as many fence rails as you want. The more rails you use, the more secure the cipher becomes. However, the more rails you use, the more difficult it becomes to encrypt and decrypt the message. Chaging the value of R in line 20 will change the number of rails.
TI Version
I thought it would be fun to do a version on the TI 99/4A. It’s a new platform to me, but I’m enjoying it so far.
TI BASIC doesn’t have straightforward string manipulation, so we’ll use the Extended Basic Cart to make this more palitable. I’m not a TI person so I’m not sure if this is the best way to do this, but it works.
5 CALL CLEAR
10 REM RAIL FENCE CIPHER
20 R=3
30 INPUT "ENTER THE PLAIN TEXT: ":TR$
40 L=LEN(TR$)
50 F$=RPT$(".",R*L)
60 T$=""
70 REM REMOVE SPACES FROM INPUT
80 FOR I=1 TO L
90 IF SEG$(TR$,I,1)<>" " THEN T$=T$&SEG$(TR$,I,1)
100 NEXT I
110 L=LEN(T$)
120 REM FILL FENCE
130 CR=1
140 CC=1
150 D=1
160 FOR I=1 TO L
170 F$=SEG$(F$,1,(CR-1)*L+CC-1)&SEG$(T$,I,1)&SEG$(F$,(CR-1)*L+CC+1,R*L)
180 CC=CC+1
190 CR=CR+D
200 IF CR<1 OR CR>R THEN D=-D :: CR=CR+2*D
210 NEXT I
220 REM PRINT FENCE
230 FOR I=1 TO R
240 PRINT SEG$(F$,(I-1)*L+1,L)
250 NEXT I
260 REM READ FENCE
270 CT$=""
280 FOR I=1 TO R
290 FOR J=1 TO L
300 IF SEG$(F$,(I-1)*L+J,1)<>"." THEN CT$=CT$&SEG$(F$,(I-1)*L+J,1)
310 NEXT J
320 NEXT I
330 PRINT "THE CIPHER TEXT IS ";CT$
What’s Next?
Should we try and break these codes using 8 Bit machines, or let “the kids” do it? Let me know.