Rail Fence Cipher Converter

Encrypt or decrypt a transposition cipher that writes text in a zigzag across rails.

Technology and NetUpdated 2026-11-192 min read
Rail Fence Cipher Converter
Up to 200 characters. Spaces are kept in place and do not advance the rail pattern.
A whole number from 2 to 8. This is the key of the cipher.
Converted message
...
Rails used...
Letters processed...
Operation...

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

  1. Type the message to encrypt or decrypt.
  2. Enter the number of rails, which is the key.
  3. Choose encrypt or decrypt.
  4. Read the converted message as the main result.
  5. Check the rail count and letter count for the round trip.
  6. Try different rail counts to see how the output changes.

Formula

Assign each character to a rail following a zigzag with period 2 times (rails minus 1), then read the rails in order
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.

  1. Write the letters in a zigzag across three rails.
  2. The top rail collects S, M, T and P.
  3. The middle rail collects E, D, O, E, R, O and S.
  4. The bottom rail collects N, R and O.
  5. Read the rails in order to get SMTPEDOEROSNRO.
  6. Using decrypt mode with the same key recovers SENDMORETROOPS.

Reference table

Sample rail fence conversions
MessageRailsOperationResult
SENDMORETROOPS3EncryptSMTPEDOEROSNRO
SMTPEDOEROSNRO3DecryptSENDMORETROOPS

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

How this page was checked. The formula above was matched against the cited sources, and the worked example was run through the tool on this page before publishing. If you spot an error, contact us so we can fix it and note the correction.