User Rating 0.0
Total Usage 0 times
Only characters 0 and 1. Spaces are stripped automatically.
Cycle Length 4
Quick Presets
Is this tool helpful?

Your feedback helps us improve.

About

The Rail Fence cipher (ZigZag transposition) rearranges characters by writing them diagonally across n rows, then concatenating each row to form ciphertext. Applied to binary data, positional errors cascade. A single misplaced bit corrupts the entire decode. This tool computes exact index mappings for n rows ranging from 2 to 10, handles both encoding and decoding, and renders the zigzag traversal path on a canvas so you can verify structure visually. The cycle length is 2n 2. Inputs shorter than the cycle produce incomplete patterns. The tool validates binary-only input and rejects non-binary characters before processing.

binary zigzag rail fence cipher encoding decoding binary pattern cipher tool

Formulas

The zigzag cycle length for n rows:

C = 2n 2

For encoding, characters at index i are placed on row r determined by the zigzag bounce pattern. The row index follows a triangular wave function:

r= triangleWave(i, C)

where the triangle wave maps position i mod C to the range [0, n 1]:

p = i mod C
r =
{
p if p < nC p otherwise

The encoded output concatenates all characters from row 0 through row n 1. Decoding reverses this by computing the length of each row segment, distributing ciphertext characters back to their original positions, then reading in column order.

Where: n = number of rails (rows), C = cycle length, i = character index in the input string, r = computed row assignment, p = position within the current cycle.

Reference Data

Rows (n)Cycle LengthMin Input LengthPattern ShapeSecurity RatingCommon Use
223Simple alternationVery WeakTeaching
345Classic zigzagWeakPuzzles
467Deep zigzagWeakCTF challenges
589Extended zigzagWeakObfuscation
61011Wide zigzagLowData scrambling
71213Broad zigzagLowProtocol layers
81415Dense zigzagLowMulti-layer encoding
91617Complex zigzagLowNested ciphers
101819Maximum depthLowLayered encryption
Rail Fence alone is not cryptographically secure. Combine with substitution ciphers for practical use.
348Classic × 2 passesModerateDouble rail fence
4612Deep × 2 passesModerateLayered obfuscation

Frequently Asked Questions

When n = 1, all characters land on a single row, producing output identical to input - no transposition occurs. When n equals the input length, each character occupies its own row and the output is simply the input read top-to-bottom, which is also the original order for a single-cycle input. Both cases produce no meaningful encryption.
If the input length is less than 2n − 2, the zigzag pattern never completes a full bounce. This means some rows receive zero or one character. The encoding still works mathematically, but the pattern becomes trivially reversible by visual inspection since the structure is incomplete.
Yes. Applying Rail Fence encoding with the same or different row counts in sequence increases diffusion. However, Rail Fence remains a transposition-only cipher. It preserves character frequency. For binary data, this means the ratio of 0s to 1s is unchanged. Combine with XOR-based substitution for practical security.
The decoder computes row lengths based on the specified n. If n differs from the original encoding parameter, row-length calculations produce incorrect boundaries. Characters are assigned to wrong positions, producing garbled output. There is no error detection built into Rail Fence - the output will be valid binary but semantically incorrect.
The input is validated against the pattern /^[01]+$/. Any character other than 0 or 1 triggers rejection with a toast notification. Spaces and line breaks are stripped automatically before validation, so formatted binary (e.g., '0101 1100') is accepted after cleanup.
No. Protocol Buffers ZigZag encoding maps signed integers to unsigned integers so that small absolute values have small encoded values - it is a numerical encoding scheme. Rail Fence ZigZag is a classical transposition cipher that rearranges character positions. The names overlap but the algorithms are completely unrelated.