Rail Fence Cipher

Encode and decode messages with the zig-zag Rail Fence cipher.

Result

WECRLTEERDSOEEFEAOCAIVDEN

How it is calculated

The text is split into characters and placed on a zig-zag across N rails: the first character goes on rail 1, the next ones go down to rail N, then back up, and so on. To encode, the tool reads rail 1 left to right, then rail 2, and so on, and joins them. To decode, it first works out how many characters each rail holds for that length, cuts the ciphertext into those blocks, and reads back along the zig-zag. Every character counts, including spaces, punctuation and upper or lower case, and accented letters such as ü are kept whole. N must be at least 2; if N is larger than the text, the output equals the input. When no number of rails is given the tool uses 3.

Worked example

Encode WEAREDISCOVEREDFLEEATONCE (25 letters) with 3 rails. The zig-zag puts W, E, C, R, L, T, E on rail 1, then E, R, D, S, O, E, E, F, E, A, O, C on rail 2, then A, I, V, D, E, N on rail 3. Reading the rails in order gives WECRLTE + ERDSOEEFEAOC + AIVDEN = WECRLTEERDSOEEFEAOCAIVDEN. Switch to decode with the same 3 rails to get the original text back.

Try this example →

Common mistakes

  • Decoding with a different number of rails than the one used to encode. The result is scrambled text, not an error, because every rail count is valid.
  • Leaving spaces in the message and expecting them to be dropped. Spaces are moved like any letter, so the ciphertext contains spaces in odd places; remove them first if you want a clean string.
  • Pasting the ciphertext back with changed spacing or case while decoding. Even one added or lost character shifts every rail boundary.
  • Entering 1 rail and expecting the text unchanged. At least 2 rails are needed, otherwise the tool shows nothing.
  • Treating the cipher as secure. It only reorders characters and is easy to break by trying a few rail counts.

About this calculator

This tool encrypts or decrypts text with the Rail Fence cipher, a transposition cipher that writes the message in a zig-zag pattern down and up across a chosen number of "rails" (rows), then reads the letters off row by row to produce the ciphertext. Unlike substitution ciphers, it doesn't change any letters — only their order.

It's a classic introductory example in cryptography courses and a favorite of puzzle and escape-room designers because it's easy to encode by hand yet illustrates the core idea of transposition. Enter your text and choose the number of rails to encrypt or decrypt instantly.

Was this helpful?

You can pick up to 3 reactions.

Comments (0)

  • Be the first to comment.

Popular calculators

All Calculators