The Rail Fence Cipher

Author: Sophia Knight '03

“Virtues of  a perfect cipher: ‘… that they be not laborious to write and read; that they be impossible to decipher; and, in some cases, that they be without suspicion.’” - Francis Bacon

The Rail Fence Cipher is a type of transposition cipher. A transposition cipher involves the rearranging of the letters in the plaintext to encrypt the message. This is in contrast to a substitution cipher, in which the plaintext letters are replaced by letters from another alphabet (or by different letters from the same alphabet).

As Laurence Smith points out,

transposition ciphers are to some extent analogous to jigsaw puzzles. In such puzzles and ciphers, if all the pieces are present and in proper order, a clear picture of the message exists. If they are mixed up, all of the elements are still present, but have no apparent meaning. If the disorder is brought about by random shuffling, only long and painstaking attempts by trial and error can bring them back to normal order. Since practical cryptography is not a puzzle aiming to test the patience, but is the science of secret communication, its object is to arrange some sort of systematic disorder, which can be set right quickly and accurately by the one for whom the secret message is intended.” (Smith, Laurence Dwight. Cryptography: The science of secret writing. Dover Publications, INC. 1955. New York. 31-32)

As we explain more fully below, in the Rail Fence Cipher, the message is written in a zig-zag pattern to represent the "rails" of a fence.

Historical Background

The first uses of the transposition cipher are traced back to the ancient Greeks. They used a device called a scytale (rhymes with "Italy") to encrypt and send messages. The scytale, a transposition machine, was comprised of a cylinder and a parchment, similar to a ribbon, which was wrapped around the cylinder. The message to be encrypted was then written on the coiled ribbon. The letters of the original message would be rearranged when the ribbon was uncoiled. However, the message was easily decrypted when the ribbon  was rewrapped on a cylinder of the same diameter as the encrypting cylinder. In this case the diameter of the encrypting cylinder would be the key to encrypting and ultimately decrypting the secret message. The diameter of the cylinder determines how the ribbon coils on the cylinder and therefore how the letters in the plaintext message would be rearranged.

Encrypting a Message with Rail Fence

Similar to the diameter of the cylinder in the scytale machine, the number of rows of the Rail Fence Cipher is the key to encrypting and decrypting secret messages.  In fact, the scytale used by the ancient Greeks can produce the exact same encrypted messages as the Rail Fence cipher  if the diameter of the cylinder produced the same number of ribbon coils as the number of rows of the Rail Fence cipher. Thus, for our implementation of Rail Fence Cipher, the number of rows used to break up the message serves as the cryptographic key. It determines the exact form that the secret message will take.

To take an example, suppose we want to encrypt the message this is a test using a Rail Fence Cipher. In a Rail Fence Cipher, after removing the spaces from the original message, we would write the characters in the message in the following zig-zag pattern, where the message is written along the "rails" of a fence.

t       i       e
  h   s   s   t   s
    i       a       t

To encrypt, we construct the ciphertext by reading across the (3) rows that result.

Plaintext : this is a test
Ciphertext: TIE HSSTS IAT
Spaces are used here indicate the ends of the rows. For added complexity, a key could be used to indicate the order in which to read the rows. For example, the key 213 would give HSSTS TIE IAT.

Decrypting the message is easy if the row boundaries are known. Just write down the rows in order:

and reconstruct the "rails" of the fence:
T   I   E
 H S S T S
  I   A   T

If no row boundaries are present, it is not difficult to reconstruct the fence, as long as you know how many rows there are and in which order they are written.

Recognizing Rail Fence Ciphers

The Rail Fence cipher and transposition ciphers in general are relatively easy to distinguish from substitution ciphers because the letter frequencies in the encrypted message remain the same as in unencrypted messages. For example, in a transposition cipher, you would expect to find that the letter 'E' is the most frequent letter if the language used is English. In general, the frequency distribution of all 26 English letters would be same in a transposition cipher as they would in plain English messages.

Analyzing Rail Fence Ciphers

In the case of the Rail Fence Cipher, the analysis isn't difficult. If you know (or suspect) that a message was encrypted with a Rail Fence Cipher, it can easily be deciphered by brute force because the letters break into rows according to certain fixed patterns based on the number of rows in the key. For example, if there are two rows, then letters 1, 3, 5, ... of the message are in row one and letters 2, 4, 6, ... are in row two. If there are 3 rows, then letters 1, 5, 9, ... are in row one, letters 2, 4, 6, 8, ... are in row two and letters 3, 7, 11, ... are in row three. Therefore, the Rail Fence Cipher is not a particularly secure cipher.

For Further Study and Enjoyment

  • CryptoToolJ. Try using CryptoToolJ to create and analyze your own Rail Fence Cipher messages.