The rail fence cipher (also called a zigzag cipher) is a classical type of transposition cipher. It derives its name from the manner in which encryption is performed, in analogy to a fence built with horizontal rails.
In the rail fence cipher, the plaintext is written downwards diagonally on successive "rails" of an imaginary fence, then moving up when the bottom rail is reached, down again when the top rail is reached, and so on until the whole plaintext is written out. The ciphertext is then read off in rows.
For example, to encrypt the message 'WE ARE DISCOVERED. RUN AT ONCE.' with 3 "rails", write the text as:
W . . . E . . . C . . . R . . . U . . . O . . . . E . R . D . S . O . E . E . R . N . T . N . E . . A . . . I . . . V . . . D . . . A . . . C .
(Note that spaces and punctuation are omitted.) Then read off the text horizontally to get the ciphertext:
WECRUO ERDSOEERNTNE AIVDAC
Let
N
N=3
2(N-1)
Let
L
L
2(N-1)
K={L\over{2(N-1)}}
K
2K
L=24
K=6
WECRUO ERDSOEERNTNE AIVDAC
Write each string on a separate line with spaces after each letter in the first and last line:
W E C R U O E R D S O E E R N T N E A I V D A C
Then one can read off the plaintext down the first column, diagonally up, down the next column, and so on.
If
L
2(N-1)
2(N-1)
If the ciphertext has not been padded, but you either know or are willing to brute-force the number of rails used, you can decrypt it using the following steps.
As above, let
L
N
x
y
x+1
y
1= | L+y |
N+((N-1)*x) |
Next solve for
x
y
x
L
y
W.........V.........O .E.......O.E.......T.N ..A.....C...R.....A...C ...R...S.....E...N.....E ....E.I.......D.U....... .....D.........R........The resulting cipher text is: WVO EOETN ACRAC RSENE EIDU DRWe know that
L=24
N=6
1= | 24+y |
6+(5*x) |
1= | 18+y |
5*x |
1= | 18+y |
5*4 |
x
1= | 18+2 |
20 |
y
We now have
N=6
x=4
y=2
W V O E O E T N A C R A C _ _ _ _ _ _ _ _ _ X _ _ X
The cipher's key is
N
N
N
L
The term zigzag cipher may refer to the rail fence cipher as described above. However, it may also refer to a different type of cipher described by Fletcher Pratt in Secret and Urgent. It is "written by ruling a sheet of paper in vertical columns, with a letter at the head of each column. A dot is made for each letter of the message in the proper column, reading from top to bottom of the sheet. The letters at the head of the columns are then cut off, the ruling erased and the message of dots sent along to the recipient, who, knowing the width of the columns and the arrangement of the letters at the top, reconstitutes the diagram and reads what it has to say."[1]