Rail Fence Cipher
Write a message in a zigzag across a number of rails and read it off row by row. Choose the rails, then encode or decode.
Worked examples
Three rails
WEAREDISCOVEREDFLEEATONCE
WECRLTEERDSOEEFEAOCAIVDEN
The message is read along the top rail, then the middle, then the bottom.
Two rails
abcdef
acebdf
Positions 1, 3 and 5 first, then 2, 4 and 6.
About Rail Fence Cipher
The rail fence cipher scrambles a message without changing a single letter. The text is written on a zigzag across a number of rows, called rails, going down and then back up like the line of a fence, and is then read off one rail at a time. The letters are all still there, just in a different order, which makes it a transposition cipher and not a substitution one.
Set the number of rails, type your message and choose encode, or choose decode to rebuild a message you were given. To read it you need to know the number of rails, which is the key. With three rails, WEAREDISCOVEREDFLEEATONCE becomes WECRLTEERDSOEEFEAOCAIVDEN. Every character is moved, including spaces, accents and emoji, unless you remove the spaces first, and the characters are counted as whole symbols so emoji stay in one piece.
How to use Rail Fence Cipher
- Type or paste your message into the box.
- Set the number of rails.
- Choose encode or decode, and tick the option to remove spaces first if you want.
- Copy the result.
Common uses
- Setting or solving a puzzle that uses a transposition cipher
- Learning how transposition differs from substitution
- Checking a worked example from a course or a book
- Making a clue for a game or a treasure hunt
Frequently asked questions
How do I decode a rail fence message?
Enter the coded text, set the same number of rails and choose decode. If you do not know the number of rails, try small numbers until the text reads properly.
What does it do with two rails?
It reads the letters in odd positions first, then the letters in even positions. For example abcdef becomes acebdf.
What if there are more rails than characters?
Then no letter moves, and the text comes back unchanged, with a note telling you so.
Why remove spaces first?
Spaces are moved like any other character, and they give clues to word lengths. Removing them first makes the coded text harder to read by eye.