# What the f*ck is Rail Fence Cipher? - A Complete Tutorial

## The rail fence cipher’s encryption process requires choosing the number of rails, writing the message diagonally in a zigzag pattern.

## Table of contents

Before understanding rail fence cipher, let’s discuss classical cryptography techniques, namely substitution and transposition. In the substitution technique, the original message’s characters are replaced with different characters, numbers, or symbols. The Caesar Cipher is an example of the substitution technique. Conversely, the transposition technique involves rearranging the plaintext through permutation.

Rail fence cipher falls into the category of transposition techniques where we change the position of each plaintext letter. The term “Rail-Fence” is attributed to the resemblance of this technique to a cluster of zigzagging rails.

### 1 . Encryption 🔒

The rail fence cipher’s encryption process requires choosing the number of rails, writing the message diagonally in a zigzag pattern determined by the selected number of rails, and then combining the characters along each rail from left to right to obtain the encrypted message. Below, we’ll explain each step with an example.

First, consider “RAILFENCE” as a plain text. Next, let’s take the number of rails or fences as three, which can also be referred to as a key. The key will determine the height of the zigzag pattern. Subsequently, we can write the message diagonally in a zigzag pattern from left to right:

Lastly, we’ll combine individual rows to generate the cipher text, which in this case will be “RFEALECIN”.

### 2 . Decryption 🔓

To begin with decryption, we first need to know the number of rows and columns in the cipher text. The number of columns is equal to the length of the cipher text. Then, we have to figure out the number of rows, which serves as the key, that was used for encryption. After determining the number of rows and columns, we can construct the table and identify suitable positions for letters, as the rail fence cipher encrypts the text diagonally from left to right in a zigzag pattern.

The * represent the positions where letters from the ciphertext are placed to form the plaintext. We begin by filling in letters from the first “rail” (the top row) and move from left to right. Then, we continue this pattern on the next rail and so forth, until all the asterisk positions are filled with letters from the ciphertext:

At last, we can combine the characters from top to bottom and left to right to obtain the plain text, which is “RAILFENCE”.

### 3 . Conclusion🎷

The rail fence cypher’s encryption can be easily broken using frequency analysis. The key of the encryption is a number that is less than or equal to the length of the cypher text. As a result, it is extremely susceptible to brute-force attacks. In a nutshell, the rail fence technique isn’t the best choice for keeping our secrets safe today.