Rail fence cipher
The rail fence cipher is a transposition cipher: the letters keep their identity but change places. Pick the number of rails, then encode a message or decode one when you know how many rails were used.
Runs in your browser: nothing you enter is sent anywhere.
How to use it
- Enter your text and choose Encode or Decode.
- Select the number of rails, from 2 to 10.
- Copy the result. To decode a message you received, use the same number of rails as the sender.
How it works
The characters are laid on the rails in a zigzag: down from the first rail to the last, then back up, and so on. The cipher text is rail 1 read left to right, then rail 2, then the rest. Decoding works out which positions belong to each rail, fills them in order from the cipher text, and reads the zigzag again.
Worked example
With 3 rails, “WEAREDISCOVEREDFLEEATONCE” is laid out as W-E-C-R-L-T-E on the top rail, E-R-D-S-O-E-E-F-E-A-O-C on the middle rail and A-I-V-D-E-N on the bottom rail. Reading the rails in turn gives “WECRLTEERDSOEEFEAOCAIVDEN”.
Tips
Rail counts of 1 or at least the length of the text change nothing, so pick a number smaller than the message. Combine this cipher with a substitution cipher such as Atbash or Caesar for a harder puzzle.
Frequently asked questions
What is a rail fence cipher?
It is a transposition cipher. The message is written diagonally down and up over a number of rows, like a zigzag, and then read off one row at a time. Nothing is replaced; the characters are only reordered.
How do I decode a rail fence message?
Choose Decode and set the number of rails that were used. If you do not know it, try the values from 2 to 10 until one gives readable text. The tool rebuilds the same zigzag and puts the characters back in order.
Are spaces and punctuation included?
Yes. Every character, including spaces and punctuation, is placed on the fence and moves like any other. If you want a cleaner result, remove spaces before encoding.
Is the rail fence cipher secure?
No. With only a handful of useful rail counts it is easy to break by trying them all, so use it for puzzles and teaching rather than for private messages.