Rail Fence Cipher
Encode and decode the Rail Fence cipher, the classic zigzag transposition that writes your message diagonally across a set of rails and reads it back row by row. Adjust the rails and offset and watch the fence form live. Everything runs in your browser.
Rails
Offset
Rails set how many rows the zigzag uses. Offset shifts where on the fence the first character starts — leave it at 0 for the standard cipher.
Enter text above to see the rail fence result here.
Zigzag fence diagram
Example with the word WEAREDISCOVERED. Type your own text above to see it on the fence.
How to use Rail Fence Cipher
- 1
Choose encode or decode
Pick Encode to turn plaintext into a rail fence ciphertext, or Decode to recover the original message from ciphertext.
- 2
Set the rails and offset
Choose how many rails the zigzag uses. Leave the offset at zero for the standard cipher, or change it to match a message that was encoded with an offset. To decode, use the same settings that were used to encode.
- 3
Type or paste your text
Enter your message. The cipher runs automatically as you type, and the zigzag fence diagram below updates to show exactly how your text sits on the rails.
- 4
Read, view, and share the result
Your converted text appears instantly. Open the fence diagram to see the zigzag, then copy the result, download it as a text file, or share a link that reopens the tool with your exact settings and text.
Understanding the Rail Fence Cipher
What is the Rail Fence cipher?
The Rail Fence cipher, also called the zigzag cipher, is a classical transposition cipher. Instead of replacing letters with other letters the way a substitution cipher does, it keeps every letter exactly the same and simply rearranges their order. The message is written in a zigzag down and up across an imaginary set of horizontal lines called rails, and the ciphertext is produced by reading those rails one after another.
Because the only secret is the number of rails, the Rail Fence is one of the simplest ciphers to use by hand. It was used as a quick field cipher in the American Civil War and remains a staple of puzzles, escape rooms, capture-the-flag challenges, and introductory cryptography lessons, where it is the usual first example of transposition.
How the Rail Fence cipher works
Pick a number of rails, say three. Starting on the top rail, you write the message one character per column, moving diagonally down to the bottom rail, then bouncing back up to the top, then down again, tracing a zigzag. When every character has been placed, you read the grid back one full rail at a time, from the top rail to the bottom, and join those pieces together to form the ciphertext.
Every character takes part in the zigzag, so spaces and punctuation are moved around just like letters rather than being left in place. The number of characters never changes, which is the signature of a transposition cipher: the ciphertext is always an anagram of the original message.
Worked example
Take the message WEAREDISCOVEREDFLEEATONCE with three rails. Writing it in a zigzag places W, E, C, R, L, T, E on the top rail; E, R, D, S, O, E, E, F, E, A, O, C on the middle rail; and A, I, V, D, E, N on the bottom rail. Reading the rails in order gives WECRLTE, then ERDSOEEFEAOC, then AIVDEN.
Joined together the ciphertext is WECRLTEERDSOEEFEAOCAIVDEN. Notice that every letter of the original is still present, just shuffled into a new order, and that the result is exactly the same length as the input.
How to decode a Rail Fence cipher
To decode, you first rebuild the empty fence for the known number of rails and the same message length, marking which rail each position belongs to. Counting those marks tells you how many characters sit on each rail, so you can cut the ciphertext into the right-sized piece for every rail.
You then place each piece back onto its rail and read the fence in the original zigzag order, top-rail-bounce-to-bottom-and-back, to recover the plaintext. This tool does all of that for you: switch to Decode, set the same number of rails and offset that were used to encode, and paste the ciphertext.
Rails, offset, and the key
The number of rails is the cipher key. With one rail there is no zigzag and the text is unchanged, so the useful range starts at two rails. As the rail count approaches the length of the message the zigzag flattens out and the scrambling weakens, which means only the values roughly between two and half the message length meaningfully rearrange the text.
The optional offset shifts the starting point of the zigzag, as if the first character began partway down the fence rather than on the top rail. Leaving the offset at zero gives the standard textbook cipher; changing it produces a related variant, and the same offset must be set again to decode. One full zigzag cycle spans 2 × (rails − 1) columns before the pattern repeats.
How to break the Rail Fence cipher
The Rail Fence has a tiny key space. For a message of a given length there are only a handful of sensible rail counts to try, so an attacker can simply decode with two rails, then three, then four, and so on, and read whichever result makes sense. This brute-force search takes only moments even by hand and is instant by computer.
Because the cipher is a pure transposition, the letters themselves are untouched, so the letter frequencies of the ciphertext exactly match ordinary text. That is itself a clue: text that has normal letter statistics but reads as nonsense is a strong sign that a transposition cipher such as the Rail Fence is in use.
Is the Rail Fence cipher secure?
No. With so few possible keys and a quick brute-force break, the Rail Fence offers no real protection for information that must stay secret. Historically it was valued for speed and simplicity in the field rather than strength, and it was sometimes combined with a substitution step to make a stronger product cipher.
Today its value is educational and recreational. It is an ideal way to teach the idea of transposition, a fun mechanism for puzzles and games, and a small piece of cryptographic history. For genuine security, modern algorithms such as AES are used instead.
Frequently asked questions
What is the rail fence cipher?
How does the rail fence cipher work?
Can you show a rail fence example?
How do I decode a rail fence cipher?
What does the number of rails do?
What is the offset in the rail fence cipher?
Does the rail fence cipher change spaces and punctuation?
How do you break a rail fence cipher?
Why is the rail fence called a transposition cipher?
Is the rail fence cipher secure?
Is my text uploaded to a server?
How do I write a rail fence cipher in code?
Related tools
Keep going with these handy tools