Tools to decode/encode Rail Fence, a cipher using zig-zag writing, automatic decryption with brute-force mode.
Rail Fence (Zig-Zag) Cipher - dCode
Tag(s) : Transposition Cipher
dCode is free and its tools are a valuable help in games, maths, geocaching, puzzles and problems to solve every day!
A suggestion ? a feedback ? a bug ? an idea ? Write to dCode!
The Rail Fence (or zig-zag) cipher is a transposition cipher that involves writing text in a wave pattern across multiple lines and reading it line by line to obtain the encrypted message.
The Rail Fence cipher follows these steps:
— Define a number of levels N (or rows or floors), this number is sometimes called the cipher key.
— Write the message following a sawtooth pattern (alternating up and down) along a path of N levels/floors.
Example: Encode DCODEZIGZAG with N=3:
D---E---Z-- |
-C-D-Z-G-A- |
--O---I---G |
— Read the message line by line to obtain the encrypted text.
Example: The encrypted message is DEZCDZGAOIG
Deciphering Rail Fence requires knowing the number of levels N and is broken down into three steps:
Example: Decipher the message DEZCDZGAOIG and N=3
— Reconstruct the zigzag pattern with the expected number of levels.
Example:
X---X---X-- |
-X-X-X-X-X- |
--X---X---X |
— Write the numbered letters in the pattern, line by line.
Example:
D---E---Z-- |
-C-D-Z-G-A- |
--O---I---G |
— Read the letters along the zigzag path to reconstruct the original message.
Example: The plain text is DCODEZIGZAG.
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.
All references to zig-zag, sawtooth, up and down, uphill and downhill, path/track/route, wave, etc. are clues.
Anything in the shape of bumps can also be a clue: camel, speed bump, etc.
The word rail can be the target of puns about trains, railroads, railway tracks, cocaine/coke, etc.
If the number of levels is unknown, several approaches exist:
— Automatic brute force: Test different levels and search for readable text (dCode uses this method).
— Manual detection: Try to find possible words using the letters in the text and deduce the key.
There are several variations:
— The fence pattern may begin with a peak or a hollow (up or down).
Example: (↘↗ hollow)
A---E
-B-D-
--C--
Example: (↗↘ peak)
--C--
-B-D-
A---E
— The first letter is not necessarily the base of the peak or hollow, the zig zag can start in the middle with an offset (equivalent to add spaces at the beginning)
Example: (Offset of +1)
----D-
-A-C-E
--B---
— It is possible to allow spaces and punctuation, which shifts the characters.
— It is possible to encrypt a message by applying the decryption steps. dCode denotes this variant with the symbol ⁻¹.
If the value of the number of levels is greater than or equal to the size of the text, then the text undergoes no encryption (no change).
None, Rail Fence is the original word, ZigZag is the mnemonic term.
Sometimes Zig-Zag is the name given to a reading method. The message is hidden in a written grid, as with Rail Fence, but the empty spaces in the grid are then filled with neutral letters.
Example: (Zig-zag reading of ABCDE)
AxyzE
xByDz
wxCyz
Rail-Fence is a basic transposition, no date or creator is recognized.
dCode retains ownership of the "Rail Fence (Zig-Zag) Cipher" source code. Any algorithm for the "Rail Fence (Zig-Zag) Cipher" algorithm, applet or snippet or script (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, breaker, translator), or any "Rail Fence (Zig-Zag) Cipher" functions (calculate, convert, solve, decrypt / encrypt, decipher / cipher, decode / encode, translate) written in any informatic language (Python, Java, PHP, C#, Javascript, Matlab, etc.) or any database download or API access for "Rail Fence (Zig-Zag) Cipher" or any other element are not public (except explicit open source licence like Creative Commons). Same with the download for offline use on PC, mobile, tablet, iPhone or Android app.
Reminder: dCode is an educational and teaching resource, accessible online for free and for everyone.
The content of the page "Rail Fence (Zig-Zag) Cipher" and its results may be freely copied and reused, including for commercial purposes, provided that dCode.fr is cited as the source.
Exporting the results is free and can be done simply by clicking on the export icons ⤓ (.csv or .txt format) or ⧉ (copy and paste).
To cite dCode.fr on another website, use the link:
In a scientific article or book, the recommended bibliographic citation is: Rail Fence (Zig-Zag) Cipher on dCode.fr [online website], retrieved on 2025-04-14,