Rail fence cipher
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)(Learn how and when to remove this template message)
The rail fence cipher (also called a zigzag cipher) is a form of transposition cipher. It derives its name from the way in which it is encoded.
In the rail fence cipher, the plain text is written downwards and diagonally on successive "rails" of an imaginary fence, then moving up when we reach the bottom rail. When we reach the top rail, the message is written downwards again until the whole plaintext is written out. The message is then read off in rows. For example, if we have 3 "rails" and a message of 'WE ARE DISCOVERED. FLEE AT ONCE', the cipherer writes out:
W . . . E . . . C . . . R . . . L . . . T . . . E . E . R . D . S . O . E . E . F . E . A . O . C . . . A . . . I . . . V . . . D . . . E . . . N . .
Then reads off to get the ciphertext:
Note that this particular example does NOT use spaces separating the words. The decipherer will need to add them based on context. If spaces are shown in the ciphertext, then they must be included in the count of letters to determine the width of the solution grid.
Keys can also be used in this cipher eg In this example shown above have Key=3 which means there is three rails, or three lines of text.
Here is another example to see how to actually solve a Rail Fence cipher. We'll use a 3-rail fence to encode a new phrase and include spacing in between the words. Our ciphertext comes out as IA_EZS_ELYLK_UZERLIPL. Note that our ciphertext has a total of 21 units (letters + spaces). This will be important later on as we try to decipher it.
To solve the cipher, you must know the height and cycle of the puzzle. The height is simply the number of fence rails used to create it. In this example, we said that 3 fence rails were used, so the height is 3. The height will always be higher than 2 and no more than the number of letters in the ciphertext (in this case 21) or else the phrase will not be properly encoded; the height can thus be discovered by process of elimination if not known.
To determine the puzzle width, which will tell us how many total units will be in each row, you must determine the "cycle" of letters. A "cycle" of letters runs from the top row, down through each subsequent row, and then up again, but stopping before reaching the top row again. (The next letter on the top row will actually begin the next cycle.) So a 2-rail puzzle has a "cycle" of 2 units; a 3-rail puzzle has a "cycle" of 4 letters; a 4-rail puzzle has a "cycle" of 6 letters; etc. (See below.) The math equation for this is: "Cycle" = ([# of rails] x 2) - 2 (since the top and bottom rows have half as many units per cycle as any middle row(s)).
1 . 1 . 1 . 1 . 1 1 . . . 1 . . . 1 . . . 1 . . . 1 1 . . . . . 1 . . . . . 1 . . . . . 1 . . . . . 1 . 2 . 2 . 2 . 2 . . 2 . 4 . 2 . 4 . 2 . 4 . 2 . 4 . . 2 . . . 6 . 2 . . . 6 . 2 . . . 6 . 2 . . . 6 . . . 3 . . . 3 . . . 3 . . . 3 . . . . 3 . 5 . . . 3 . 5 . . . 3 . 5 . . . 3 . 5 . . . . . 4 . . . . . 4 . . . . . 4 . . . . . 4 . . .
Our 3-rail fence example has a "cycle" of 4 units. So divide the total units (letters + spaces) by the cycle number and round down to the next whole number. There are 21 units in the example, so our "base puzzle width" is 5 (21 / 4 = 5.25, which rounds down to 5). It is important to realize that there are 5 "full cycles" plus a "partial cycle" of 1 more letter (5 x 4 = 20 and 20 + 1 = 21 units). Therefore, the top row has 6 units in it (5 "full cycles" + the 1 extra letter that is starting off the 6th cycle all by itself). The middle row has 10 units (5 "full cycles" x 2 units for each cycle). The bottom row has 5 units (5 "full cycles" x 1 unit for each cycle since it is the bottom-most row).
Take the first 6 units from our ciphertext and write them across the top row, leaving much space between the units: [IA_EZS]_ELYLK_UZERLIPL.
I . . . A . . . _ . . . E . . . Z . . . S
The middle row takes the next 10 units and adds 1 unit just after and 1 unit just before each unit in the top row: IA_EZS[_ELYLK_UZE]RLIPL.
I . . . A . . . _ . . . E . . . Z . . . S . _ . E . L . Y . L . K . _ . U . Z . E .
The bottom row gets the final 5 units written below and in between the pairs of units in the middle row: IA_EZS_ELYLK_UZE[RLIPL].
I . . . A . . . _ . . . E . . . Z . . . S . _ . E . L . Y . L . K . _ . U . Z . E . . . R . . . L . . . I . . . P . . . L . .
Now just follow the down-up-down-up pattern to determine the original message: I_REALLY_LIKE_PUZZLES!
Problems with the rail fence cipher
The rail fence cipher is not very strong; the number of practical keys (the number of rails) is small enough that a cryptanalyst can try them all by hand.
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 system that looks like a zigzag line going from the top of the page to the bottom. As described in Fletcher Pratt's 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."
- Helen Fouché Gaines, Cryptanalysis, a study of ciphers and their solution, Dover, 1956, ISBN 0-486-20097-3