Rail Fence Cipher

The Rail Fence Cipher is a transposition cipher, which rearranges the plaintext letters by drawing them in a way that they form a shape of the rails of an imaginary fence.


The Rail Fence Cipher was invented in ancient times. It was used by the Greeks, who created a special tool, called scytale, to make message encryption and decryption easier. Currently, it is usually used with a piece of paper. The letters are arranged in a way which is similar to the shape of the top edge of the rail fence.


To encrypt the message, the letters should be written in a zigzag pattern, going downwards and upwards between the levels of the top and bottom imaginary rails. The shape that is formed by the letters is similar to the shape of the top edge of the rail fence.

Next, all the letters should be read off and concatenated, to produce one line of ciphertext. The letters should be read in rows, usually from the top row down to the bottom one.

The secret key is the number of levels in the rail. It is also a number of rows of letters that are created during encryption. This number cannot be very big, so the number of possible keys is quite limited.

For example, let us encrypt a name of one of the countries in Europe: The United Kingdom. Let's assume that the secret key is 3, so three levels of rails will be produced.

First, we will remove the empty spaces, and encrypt only the capitalized letters:


Next, the plaintext letters will form the shape of the fence:


Then, the letters should be read row by row, starting from the top one. Finally, they ought to be concatenated to form one ciphertext message. In our example, the calculated ciphertext sequence would be:


To decrypt the message, the receiver should know the secret key, that is the number of levels of the rail. Based on the number of rows and the ciphertext length, it is possible to reconstruct the grid and fill it with letters in the right order (that is, in the same way as used by the sender during encryption).

Security of the Rail Fence Cipher

Ciphertexts produced by transposition ciphers are relatively easy to recognize, because the frequency distribution of all ciphertext alphabet letters is the same as in plain messages written in the same language.

Due to the small number of possible keys, the Rail Fence Cipher can be broken quite easily by using brute force attacks. The attacker should check all the practicable numbers of rail levels, that might have been using during encryption.


The encryption function in the Rail Fence Cipher performs two major steps. First, the letters are entered into a table, that represents the imaginary fence. Then, the letters should be read off in rows.

Below, there is a JavaScript function which performs encryption of the input message and returns the result. Note, that rowNumber input parameter is determined by the cipher's secret key:

function encrypt(messageInput, rowNumber) {
  var messageOutput = '';

  var fanceTable = [];
  for (var pos = 0; pos < rowNumber; ++pos) {
    fanceTable[pos] = [];

  // First, enter the letters into the fence table:

  var r = 0;
  var direction = 1;

  for (var c = 0; c < messageInput.length; ++c) {

    if (((r == rowNumber - 1) && (direction == 1)) ||
        ((r == 0) && (direction == -1))) {
      direction = -direction;

    r = r + direction;

  // Then, read off the ciphertext:

  var row = 0;
  while (row < rowNumber) {
    for (var pos = 0; pos < fanceTable[row].length; ++pos) {
      messageOutput = messageOutput.concat(fanceTable[row][pos]);

  return messageOutput;

You may try out the Rail Fence Cipher online on Crypto-Online website.