TTKTheTextKit

Rail Fence Cipher

Type a message and a number of rails, and get the rail fence ciphertext together with the zigzag it was read from. Switch to Decrypt to turn a ciphertext back into text with the same key, or to Try every key to see the result of every number of rails at once when you do not know the key. An offset starts the zigzag part way along for more keys, and you can keep the spaces and punctuation, remove spaces, or keep letters and digits only. Everything runs in your browser.

The zigzag

W...E...C...R...U...O...
.E.R.D.S.O.E.E.R.N.T.N.E
..A...I...V...D...A...C.

The rail fence cipher is a transposition cipher: it keeps your letters and only changes their order. The text is written in a zigzag down and up over a number of rails, and the ciphertext is read off one rail at a time, from the top. The only key is the number of rails, so WE ARE DISCOVERED. RUN AT ONCE. on 3 rails becomes WECRUOERDSOEERNTNEAIVDAC once the spaces and punctuation are dropped. The offset starts the zigzag part way along, from 0 up to 2 times the rails minus 3, which gives more keys. To decrypt, use the same number of rails and the same offset. Because there are so few keys, anyone can try them all, which the third option does, so this is a puzzle and a lesson in how ciphers work, not a way to keep a secret. Everything runs in your browser.

Related Tools

[6 suggestions]

About the Rail Fence Cipher

The rail fence cipher is a transposition cipher: it does not change the letters, it only moves them. The plain text is written in a zigzag down and up over a number of rails, which are rows, and the ciphertext is read off one rail at a time from the top. For the example on Wikipedia, WE ARE DISCOVERED. RUN AT ONCE. on 3 rails, the letters go to rail 1, rail 2 and rail 3 in the pattern 1 2 3 2 1 2 3 2 and so on, and the ciphertext is WECRUOERDSOEERNTNEAIVDAC once the spaces and punctuation are dropped. The zigzag repeats after 2 x (rails - 1) positions, so 3 rails repeat every 4 letters. To decrypt, the tool works out which rail each position belongs to, counts how many letters each rail holds, cuts the ciphertext into those pieces and reads the zigzag back. The only key is the number of rails, and a key as large as the text leaves it unchanged. The offset, from 0 up to 2 x rails - 3, starts at a later point of the zigzag, which multiplies the number of keys by the length of one cycle. The Groups of five option writes the ciphertext in blocks of five letters, the old convention for cipher text. Because there are so few keys, a message can be broken by trying them all, which the Try every key option does: the key 3 gives the readable line. It is a good cipher for a puzzle, a classroom or a game, and a poor one for secrets, since it keeps the letter frequencies of the text and falls to a short search.

How does the rail fence cipher work?▾

You write the text in a zigzag over a number of rails, going down and then up, and read the rails one after another from the top. The number of rails is the key. Decryption rebuilds the same zigzag and puts the letters back.

How do I decrypt a rail fence cipher?▾

Choose Decrypt, paste the ciphertext and enter the number of rails and the offset used to encrypt it. If you do not know the key, choose Try every key and look for the line that reads as normal text.

What does the offset do?▾

It starts the zigzag part way along its cycle instead of at the top-left corner. An offset of 0 is the standard cipher. A different offset gives a different ciphertext for the same number of rails, and the same offset must be used to decrypt.

Why are the spaces and punctuation removed in the example?▾

The classic way to write the cipher uses only letters, so the spaces and punctuation are dropped before the zigzag is drawn. You can choose to keep every character if you want the spaces to move as well.

Is the rail fence cipher secure?▾

No. There are very few keys, so an attacker can try them all in seconds, and the letters are only moved, not changed, so the frequency of each letter gives no protection. Use it for puzzles and learning, and use modern encryption for anything private.