Kifanga Logo

Rail Fence Cipher: Encode and Decode Online

Rail Fence Cipher is a transposition cipher that encrypts by writing a text in zigzag and then read from left to right. It is also known as zigzag cipher.

To put it simply, in rail fence cipher, the plaintext is written downwards on successive rails of an imaginary fence, and starting a new column when the bottom is reached.

Usage

The Rail Fence was invented in the 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 edge of the rail fence.

Algorithm

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.

Example

Rail Fence encryption uses an integer for the number of levels of the zigzag. The encoded message is written in zigzag (like a rail fence or sawtooth) along a path with N levels or floors.

How to encrypt using Rail Fence cipher?

For example, let’s encrypt “KIFANGA” with N = 3.

K

-

-

-

N

-

-

-

I

-

A

-

G

-

-

 

F

-

-

-

A

The encoded message is read by rows.

For example, the encrypted message is “KNIAGFA”.

How to decrypt Rail fence cipher?

Rail Fence decryption requires to know the number of levels N.

For example, let’s decrypt the message “KNIAGFA” with N = 3.

Write the expected zigzag form and complete it by lines, before reading as zigzag.

Example:

X

-

-

-

X

-

-

-

X

-

X

-

X

-

-

-

X

-

-

-

X

Then:

K

-

-

-

N

-

-

 

I

 

A

 

G

 

-

-

F

-

-

-

A

For example, the original plaintext is “KIFANGA”.

A JavaScript Example

Use the above Rail Fence decoder and encoder to encrypt/decrypt simple messages.

How to recognize Rail fence ciphertext?

A message encoded by Rail-Fence has an index of coincidence equal to that of the language of the original text.

Frequency analysis reveals usual occurrences of common letters similar to a plain text.

How to decipher Rail fence without the number of levels?

Rail Fence can be cracked by testing all levels from 2 to N.

What are the variants of the Rail fence cipher?

There are several variations:

The fence may begin with a peak or a hollow (up or down).

For example, A…E: (↘↗ hollow)

.

B

.

D

.

.

.

C

.

.

 

For example,  ..C.. : (↗↘ peak)

.

B

.

D

.

A

.

.

.

E

 

The first letter is not necessarily the base of the peak or hollow, the zigzag can start in the middle (equivalent to adding spaces at the beginning).

For example, ….D. : (one shift)

.

A

.

C

.

E

.

.

B

.

.

.

 

It is possible to allow the spaces and punctuation, which shifts the characters.

What is the difference between Rail fence and Zigzag?

None, Rail Fence is the original word, zigzag is the mnemonic term.

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 possible numbers of rail levels, that might have been used during encryption.

The use of nulls can also have a detrimental effect on the security of the cipher, as an interceptor can use them to identify where the end of the line is, and so have a sensible guess at the key.

This can be averted by using a more common letter, such as “E”, to fill the null spaces, as it will still be clear to the recipient that these are not part of the message as they will appear at the end of the plaintext.

The Rail Fence cipher can also be utilized without the use of nulls.

One way to also make the encryption a little bit more secure, is to keep the spaces as characters, and include them in the encryption table.

They are treated in exactly the same way as any other letter.

If this tool was helpful, tweet it or share it.

Tools related to Rail Fence Cipher: Encode and Decode Online