The zigzag cipher, also called the Rail Fence cipher, rearranges characters by writing them diagonally down and up across a chosen number of rails, then reading the rails from top to bottom. It is a transposition cipher: the characters are not replaced, only their positions change.
The practical task is either encoding exact plaintext or decoding a sequence that already uses a zigzag pattern. The Rail Fence Cipher Decoder supports both directions, accepts a whole-number rail count from 2 to 100, and performs the reordering locally without dropping any Unicode code point.
Different implementations can use different conventions, so the rail count and movement rules must be settled before one tool’s output is compared with another. This guide uses the Rail Fence Cipher Decoder’s fixed convention: start at the top, move downward first, use no initial offset, and preserve all formatting.

How the zigzag path is calculated
With r rails, the path repeats every 2(r − 1) positions. For a character at zero-based position i, the implementation calculates p = i mod 2(r − 1). If p is below r, that character belongs on rail p; otherwise it belongs on rail 2(r − 1) − p. The formula makes the path descend, turn at the bottom rail, rise, and turn again at the top.
Encryption appends each code point to an ordered bucket for the rail it visits. The buckets are then joined from the top rail to the bottom rail. Decryption reverses that structure: it counts how many original positions belong to each rail, divides the ciphertext into buckets of those sizes, and consumes one character from the appropriate bucket for every original position. The process adds or guesses no padding characters.
Because the operation is a transposition, the message keeps the same characters and character frequencies. A space can be visible in the result even though its location has moved, and punctuation is rearranged rather than encoded as a different symbol.
For one complete example, use three rails and the plaintext WEAREDISCOVEREDFLEEATONCE. The repeated row pattern is 0, 1, 2, 1. The rail buckets are WECRLTE, ERDSOEEFEAOC, and AIVDEN; reading them from top to bottom produces WECRLTEERDSOEEFEAOCAIVDEN. Decrypting that result with the same three rails restores the original plaintext.
Set the convention before comparing ciphertext
The exact rules are part of the result, not cosmetic settings. This decoder uses the following definition.
| Setting | Rule used by the decoder | Effect |
|---|---|---|
| Starting position | Top rail, numbered 0 | The first code point goes into rail 0. |
| Movement | Downward first | The path reverses at the bottom and top rails. |
| Initial offset | None | No movement occurs before the first character. |
| Rail count | Whole number from 2 through 100 | The count sets the path cycle and resulting output. |
| Read order | Top rail through bottom rail | The ordered rail buckets are concatenated in that order. |
| Text treatment | Every code point participates | Spaces and line breaks affect later rail positions. |
Some Rail Fence versions add an initial offset, begin on the bottom rail, move upward first, or remove nonletters before arranging the characters. The same plaintext and rail count can then produce a different sequence. If ciphertext comes from a worksheet or another implementation, establish its convention before attempting to reverse it. A focused guide to matching Rail Fence conventions before deciphering helps check those variables.
For a round trip, keep the convention fixed rather than changing rails between modes. A successful encrypt-then-decrypt result confirms consistency with that convention; it does not prove confidentiality or authenticity.
Encrypt or decrypt a zigzag cipher
- Choose the direction. Select Encrypt to create a rearranged sequence from plaintext. Select Decrypt to restore the original order from ciphertext.
- Enter the rail count. Use a whole number from 2 through 100. The same count must be used to reverse the operation. An invalid count produces a separate failure rather than being replaced by a default.
- Paste the exact text. Keep all spaces, punctuation, line breaks, and other code points that are meant to participate. Do not trim the input. Empty input is a separate failure, and each input is capped at 200,000 code points.
- Run and copy the result. Run the selected mode, then copy the output. The copy action writes the exact string, including visible whitespace and line breaks, so do not clean it up unless you intend to change the message.
- Use identical settings in reverse. To undo encryption, choose Decrypt, enter the same rail count, and paste the unmodified ciphertext. The decoder does not guess a rail count; checking alternative counts is a separate cryptanalysis task.
When checking your work, encrypt the exact plaintext, copy the result, and then decrypt it with the identical convention and rail count. A matching result shows that the positions were reconstructed correctly. It is only a convention check because Rail Fence provides no cryptographic secrecy.
Keep every character and every line break
The decoder does not remove spaces or normalize text. Uppercase and lowercase letters, digits, whitespace, punctuation, and emoji all participate as Unicode code points. Their identity is preserved, while their positions may change in the ciphertext.
The implementation iterates code points rather than partial UTF-16 units, so a supplementary character such as an emoji is not split into two surrogate halves. This is also why visible formatting can be copied exactly from the result.
A space occupies a real position on the zigzag. Remove one before encoding and every later character can be assigned to a different rail. A line break participates as a character too, so it changes all subsequent positions. If a line break was part of the original input, it must not be removed from the ciphertext before decryption.
Identical input with an identical rail count always gives identical output. That makes the operation predictable and suitable for exercises, but it also reveals why Rail Fence should not be treated as a secret-keeping system.
Know when Rail Fence is the wrong tool
| Method | What changes | Key property | Best fit |
|---|---|---|---|
| Zigzag or Rail Fence | Character positions only | A small rail count and visible character frequencies | Learning and puzzles |
| Substitution cipher | Characters are replaced through a mapping | Depends on its key or alphabet | Classic decoding exercises |
| Authenticated encryption such as AES-GCM | Plaintext is protected with a secret key | Designed for confidentiality and tamper detection | Credentials, files, and confidential data |
Rail Fence is a historical teaching cipher with no secret beyond the rail count. It preserves character frequencies and is easy to brute force. Do not use it for credentials, personal data, tokens, files, or confidential communication. For real security, use AES-GCM or another reviewed authenticated-encryption construction.
Its practical value is still useful: it demonstrates how a permutation can obscure the original sequence while leaving every character available to a solver who reconstructs the path.
Limits that affect the result
- Rail range: The rail count must be a whole number from 2 through 100, including when the text is shorter than the selected number of rails.
- Input size: Each input is limited to 200,000 code points so the rail construction and reconstruction can remain responsive.
- Empty input: Empty text produces a separate failure rather than being treated as an empty successful result.
- Many rails relative to length: If the rail count equals or exceeds the number of characters, every used position lies on a separate initial descent, so the output remains unchanged.
- No inferred settings: The decoder does not guess a rail count, insert padding, or repair altered ciphertext. It applies the convention and input it receives.
These are operating boundaries, not automatic normalization rules. A successful result does not mean that spaces were removed or missing characters were supplied; it means the supplied text followed the supplied rail convention.
For a deeper look, see Vigenere Cipher Decoder Safe? Privacy and Security.