T

Text Machine

Powerful text tools, in your browser

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.

Fence settings

Rails

3

Offset

0

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.

Plaintext
Ciphertext

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.

W
E
C
R
E
R
D
S
O
E
E
A
I
V
D

How to use Rail Fence Cipher

  1. 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. 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. 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. 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?
The rail fence cipher is a classical transposition cipher, also known as the zigzag cipher. It writes your message in a zigzag across a number of rails and then reads it off rail by rail. It does not replace any letters; it only rearranges their order, so the ciphertext is an anagram of the plaintext.
How does the rail fence cipher work?
You choose a number of rails, then write the message diagonally down and back up across those rails, one character per column. Once every character is placed, you read the grid one full rail at a time from top to bottom and join the pieces to form the ciphertext. Spaces and punctuation move along with the letters.
Can you show a rail fence example?
With three rails the message WEAREDISCOVEREDFLEEATONCE becomes WECRLTEERDSOEEFEAOCAIVDEN. The top rail reads WECRLTE, the middle rail reads ERDSOEEFEAOC, and the bottom rail reads AIVDEN. Every original letter is still there, just shuffled, and the length is unchanged.
How do I decode a rail fence cipher?
Switch to Decode and set the same number of rails and offset that were used to encode, then paste the ciphertext. The tool rebuilds the fence, works out how many characters belong on each rail, refills the rails, and reads the zigzag back to recover the original message.
What does the number of rails do?
The number of rails is the key. More rails create a deeper zigzag and a more thorough rearrangement, up to a point. One rail leaves the text unchanged, and once the rail count gets close to the length of the message the scrambling weakens again, so the useful range is roughly from two rails up to about half the message length.
What is the offset in the rail fence cipher?
The offset shifts where the zigzag begins, as if the first character started partway down the fence instead of on the top rail. Leaving it at zero gives the standard textbook cipher. If a message was encoded with an offset, you must set the same offset to decode it correctly.
Does the rail fence cipher change spaces and punctuation?
Yes. Because it is a transposition cipher, every character takes part in the zigzag, so spaces, digits, and punctuation are rearranged along with the letters rather than being left in place. The total number of characters always stays the same.
How do you break a rail fence cipher?
The key space is very small, so you simply try decoding with two rails, then three, then four, and so on, and keep whichever result is readable. This brute-force search is fast even by hand. Normal letter frequencies combined with scrambled, unreadable text are a strong hint that a transposition cipher is being used.
Why is the rail fence called a transposition cipher?
A transposition cipher hides a message by changing the order of its characters rather than by substituting new ones. The rail fence does exactly that: it keeps every original letter but moves it to a new position dictated by the zigzag, so the result is a rearrangement of the same letters.
Is the rail fence cipher secure?
No. With only a handful of practical keys it can be brute-forced in seconds, so it provides no real security for sensitive information. It is best understood as an educational and puzzle cipher. For genuine protection, modern algorithms such as AES should be used instead.
Is my text uploaded to a server?
No. All encoding and decoding happen entirely in your browser, so your text is never uploaded, logged, or stored. Even a share link keeps your text in the part of the URL after the hash, which browsers never send to a server, so it stays private until you choose to share it.
How do I write a rail fence cipher in code?
Work out, for each position in the message, which rail it lands on by counting down from the top rail to the bottom and back up in a repeating cycle. To encode, append each character to its rail and then join the rails in order. To decode, count how many characters each rail holds, slice the ciphertext into those pieces, and read them back in the zigzag order. This in-browser tool uses exactly that approach.

Related tools

Keep going with these handy tools

Columnar Transposition Cipher

Scytale Cipher

Caesar Cipher

Vigenère Cipher

Atbash Cipher

Playfair Cipher