Rail Fence ( Zigzag ) Cipher

3.2K 17 3
                                    

Here's another cipher that's easy to understand and remember as well...

The Rail Fence Cipher!

In this 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.

Example #1 :
if we have 3 "rails" and a message of 'WE ARE DISCOVERED. FLEE AT ONCE', we will write it this way:

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

To get the ciphertext : write all the letters in the first row ( from left to right ) without spaces, followed by the letters in the second row, and so on...

WECRLTEERDSOEEFEAOCAIVDEN

Note : You will NOT use spaces to separate the words. You 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.

Example #2 :

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.

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)).

    Two Rails                                                  Three Rails

1   1   1   1   1                                 1        1        1       1       1
   2   2   2   2                                     2   4   2   4   2   4  2   4
                                                            3        3       3        3

Four Rails

1           1           1            1          1
  2      6   2       6   2      6   2      6
    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

Read it diagonally... and you will get the encoded phrase: I_REALLY_LIKE_PUZZLES!

Another Important Note: You will have to use 'null' letters if the phrase does not reach the imaginary grid / end.

Example...

I          L           I           U          E
   R   A   L     L    K    P    Z    L    S
      E         Y            E          Z          X

Ciphertext : ILIUERALLKPZLSEYEZX

Credits to Wikipedia. ^_^

CODES & CIPHERSWhere stories live. Discover now