Rail Fence Cipher Converter
Encrypt or decrypt a transposition cipher that writes text in a zigzag across rails.
What it does
The rail fence cipher is a transposition cipher: it rearranges the letters of a message rather than replacing them. The text is written in a zigzag pattern down and then up across a number of imaginary rails, and the ciphertext is read off row by row.
This tool encrypts or decrypts with two to eight rails. It keeps spaces and punctuation at their original positions and only moves letters along the zigzag path.
The page opens with the message SENDMORETROOPS and three rails. The rows contain SMTP, EDOEROS and NRO, and reading them together gives SMTPEDOEROSNRO.
Decryption first determines how many characters sit on each rail, then places the ciphertext along the same zigzag path and reads it in the original order.
The cipher is weak because only a small number of rail counts are usable. Trying each key in turn quickly reveals readable text.
How to use
- Type the message to encrypt or decrypt.
- Enter the number of rails, which is the key.
- Choose encrypt or decrypt.
- Read the converted message as the main result.
- Check the rail count and letter count for the round trip.
- Try different rail counts to see how the output changes.
Formula
- rails
- The number of horizontal levels, which is the cipher key.
- zigzag
- The repeating down and up path that assigns characters to rails.
- codeword
- The letters read off row by row after the pattern is filled.
The rail index moves down until it reaches the last rail, then up until it reaches the first, repeating every two times the number of rails minus one positions. Decryption recovers the count of characters per rail before refilling the same path.
Worked example
Example: SENDMORETROOPS with three rails
The page opens with message SENDMORETROOPS and rails = 3.
- Write the letters in a zigzag across three rails.
- The top rail collects S, M, T and P.
- The middle rail collects E, D, O, E, R, O and S.
- The bottom rail collects N, R and O.
- Read the rails in order to get SMTPEDOEROSNRO.
- Using decrypt mode with the same key recovers SENDMORETROOPS.
Reference table
| Message | Rails | Operation | Result |
|---|---|---|---|
| SENDMORETROOPS | 3 | Encrypt | SMTPEDOEROSNRO |
| SMTPEDOEROSNRO | 3 | Decrypt | SENDMORETROOPS |
Limits
- Messages are limited to 200 characters.
- Rails must be a whole number from 2 to 8.
- Non letters are preserved and do not consume a rail position.
- The cipher offers no real security.
Common mistakes
- Using a different rail count to decrypt.
- Treating the cipher as a substitution rather than a transposition.
- Counting spaces as positions in the zigzag.
- Assuming a rail count of one changes the message.
- Expecting the cipher to resist a brute force key search.
Try it yourself
Quick checks. Answers sit tucked under each question.
Encrypt SENDMORETROOPS with three rails.
SMTPEDOEROSNRO.
Decrypt SMTPEDOEROSNRO with three rails.
SENDMORETROOPS.
Why is the cipher weak?
Only a few rail counts are possible, so trying them all is easy.
Questions
What type of cipher is this?
A transposition cipher, which rearranges letters without replacing them.
What is the key?
The number of rails used in the zigzag.
How does decryption work?
It finds how many letters belong to each rail and refills the zigzag pattern.
Are spaces encrypted?
No. They stay in their original positions in this tool.
Is it secure?
No. A small number of possible keys makes brute force trivial.
Is this the same as a scytale?
They are closely related transposition ideas and can produce the same patterns.
Sources
- Emory University, transposition ciphers and the rail fence cipher
- Trinity College, the rail fence cipher