Rail fence cipher - decoder and encoder

The rail fence cipher is a simple transposition cipher. It is also known as a zigzag cipher. The name comes from the way letters are arranged. In a rail fence cipher, the plaintext letters are written diagonally in a up-down pattern from left to right. The message is then read row-by-row from top to down. The number of rows can vary, and so can also the starting place for the first letter.

Boxentriq puzzle promotion

Rail fence cipher

Rail fence layout



  • The rail fence cipher is a simple form of transposition cipher.
  • Another name for the cipher is zigzag cipher.
  • The key consists of the number of rows and the offset (starting place for the first plaintext character).
  • It can encrypt any characters, including spaces, but security is increased if all characters are of equal caps.
  • A rail fence cipher can very easily be broken, since there are only a very limited number of keys.

Rail fence cipher, and variants of it, are sometimes used in easy geocaching mystery caches, and easy logic puzzles. It can easily be detected by statistical methods, but it's usually easiest and quickest to just try every possible key manually ("brute force attack").

Rail fence
Rail fence

Sample text


The ciphertext above represents "CODEBREAKING" encrypted using 3 rails and 0 offset.

C   B   K