Rail Fence Cipher

Introduction

The Rail Fence cipher is a transposition cipher. It gets its name from the way that letters are transposed during encryption. First determine the number of 'rails' to use. Then write the message out, a letter at a time, starting from the top rail. When you reach the bottom rail, climb back up the rails to the top. The message, 'The secret is out' is written below on a rail fence of size 3, with all of the spaces having been removed from the message.

T---E---T---U-
-H-S-C-E-I-O-T
--E---R---S---

To form the cipher text, the letters are read off horizontally, starting from the top left. In this case, the cipher text becomes TETUHSCEIOTERS.

Programming The Rail Fence

Transposition ciphers are a little tricky to program. Moving letters around willy-nilly is always a recipe for disaster if you don't work in stages. Most of our substitution ciphers could be decrypted using only the tiniest variation on the code, swapping cipher and plain alphabets around. This is not the case with Rail fence. Two slightly different processes are needed for the job.

Encryption

// Tidy up the message
upper ← UPPERCASE(plain)
tidy ← ""
FOR letter ← 0 TO upper.LENGTH - 1
   tmpASC ← ASCII CODE OF upper[letter]
   IF tmpASC > = 65 AND tmpASC <= 90 THEN
      tidy ← tidy + CHR(tmpASC)
   END IF
END FOR
plain ← tidy

// Declare variables
rails ← 3
DECLARE STRING lines[rails-1]
currentLine ← 0
increment ← 1
FOR i ← 0 TO rails - 1
   lines[i] ← ""
END FOR

// Build rails
FOR i ← 0 TO plain.LENGTH - 1
   lines[currentLine] ← lines[currentLine] + plain(i)
   IF currentLine = 0 THEN
      increment = 1
   ELSE IF currentLine = rails - 1 THEN
      increment = -1
  END IF
   currentLine ← currentLine + increment
END FOR

// Output cipher text
cipher ← ""
FOR i ← 0 TO rails - 1
   cipher ← cipher + lines[i]
END FOR
OUTPUT cipher

Decryption

// Declare variables
rails ← 3
DECLARE STRING lines[rails-1]
DECLARE INTEGER linesLengths[rails-1]
plain ← ""
cipher ← "message to decrypt"
start ← 0
currentLine ← 0
increment ← 0
FOR i ← 0 TO rails - 1
   lineLengths[i] ← 0
END FOR

// Calculate line lengths
FOR i ← 0 TO cipher.LENGTH - 1
   lineLengths[currentLine] ← lineLengths[currentLine] + 1
   IF currentLine = 0 THEN<
      increment = 1
   ELSE IF currentLine = rails - 1 THEN
      increment = -1
  END IF
   currentLine ← currentLine + increment
END FOR

// Build lines
FOR i ← 0 TO rails - 1
   lines[i] ← cipher.SUBSTRING(start, lineLengths[i])
   start ← start + lineLengths[i]
END FOR

// Create message
FOR i ← 0 TO cipher.LENGTH - 1
   plain ← plain + lines[currentLine](0)
// Remove the used character if it is not the last in the line
   IF lines[currentLine].LENGTH > 1 THEN
      lines[currentLine] ← lines[currentLine].SUBSTRING(1)
   END IF
   IF currentLine = 0 THEN<
      increment = 1
   ELSE IF currentLine = rails - 1 THEN
      increment = -1
  END IF
   currentLine ← currentLine + increment
END FOR
OUTPUT plain

Challenges

Combine the rail fence with one of the substitution ciphers and you have a more secure message than either cipher can provide by itself.

There are many variations on this cipher. Sometimes the cipher is implemented without the zigzag - a letter is not added to the middle rails on the way up the fence, just on the way down. You could write a nice program that allows you to choose which version of the cipher you use.