Rail fence cipher (or zig-zag)

limit 1000 characters

The Rail Fence algorithm is a substitution cipher algorithm that rearranges the letters of a message by writing them in a zigzag grid pattern. The grid pattern is defined by the number of "rails" or lines used to write the message.

The Rail Fence algorithm works as follows:

• Write the message to be encrypted on the zigzag rails, alternating between the top and bottom rails. The number of rails corresponds to the encryption key.

• Read the encrypted message letters from left to right, following the zigzag pattern. This gives the encrypted text.

• To decrypt the message, draw the zigzag rails as before, but this time fill them with the letters of the encrypted message.

• Read the decrypted message letters by following the zigzag pattern. The Rail Fence algorithm is considered unsafe because it is vulnerable to brute force attacks and frequency analysis attacks.

Example of Rail Fence encryption

Let's take the message to be encrypted: "ENJOY LIFE OK" and assume the encryption key is 3.

We write the message in a zigzag pattern on 3 rails:

E   Y   F   K
 N O  -  I E O 
  J   L    -   

Then, the encrypted message is read from left to right by starting from the top rail and moving downward rail by rail. In this example, the encrypted text is: "EYFKNO-IEOJL-".
Note that the hyphen character (-) replaces space as in the calculator above.

See also

Cryptographic Calculators