Binary ZigZag Creator
Encode and decode binary strings using the ZigZag (Rail Fence) cipher pattern. Visualize the zigzag path across configurable rows.
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.
Formulas
The zigzag cycle length for n rows:
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:
where the triangle wave maps position i mod C to the range [0, n − 1]:
r =
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 Length | Min Input Length | Pattern Shape | Security Rating | Common Use |
|---|---|---|---|---|---|
| 2 | 2 | 3 | Simple alternation | Very Weak | Teaching |
| 3 | 4 | 5 | Classic zigzag | Weak | Puzzles |
| 4 | 6 | 7 | Deep zigzag | Weak | CTF challenges |
| 5 | 8 | 9 | Extended zigzag | Weak | Obfuscation |
| 6 | 10 | 11 | Wide zigzag | Low | Data scrambling |
| 7 | 12 | 13 | Broad zigzag | Low | Protocol layers |
| 8 | 14 | 15 | Dense zigzag | Low | Multi-layer encoding |
| 9 | 16 | 17 | Complex zigzag | Low | Nested ciphers |
| 10 | 18 | 19 | Maximum depth | Low | Layered encryption |
| Rail Fence alone is not cryptographically secure. Combine with substitution ciphers for practical use. | |||||
| 3 | 4 | 8 | Classic × 2 passes | Moderate | Double rail fence |
| 4 | 6 | 12 | Deep × 2 passes | Moderate | Layered obfuscation |