Rail Fence or the Zig-Zag cipher is a transposition cipher. The cryptogram is created by combining a sequence of letters based on the simplified shape of the fence. Successive letters of the plaintext are written alternately in n rows in such a way that they form the shape of the upper edge of the fence.
The fence cipher was known and often used in ancient times. The ancient Greeks even developed a special tool called scytale to make it easier to encrypt and decrypt messages.
To encrypt the plaintext with a hurdle cipher, the letters of the text must be written vertically in rows, so that each subsequent letter is shifted to the right by one character. The record should resemble the top edge of the fence rungs.
The written letters are read horizontally in order from highest to lowest row.
For encryption, we use a digital key that indicates the height of the fence, i.e. the number of lines of text. We can additionally use an offset to indicate the line from which we start typing the letters of the plaintext.
For example, if we want to encode the word CALCOOLATOR we set the key first. Let's assume that our key is the number 3 , which determines the number of lines. Then write the letters of the text individually in each of the lines in separate rows, i.e. so that they form the pattern of the upper edge of the petal rungs.
C---O---T--
-A-C-O-A-O-
--L---L---R
Now we read the created ciphertext. Each letter of the ciphertext is read successively from the first line to the last. The scrambled word CALCOOLATOR when read from the ciphertext will be COTACOAOLLR
If we set the key to 5 then our fence will look like this:
C-------T--
-A-----A-O-
--L---L---R
---C-O-----
----O------
The ciphertext will be CTAAOLLRCOO . Adding to this the offset 2 , i.e. starting from the 3rd line after coding, we get LOACOTACOLR.
------L----
-----O-A---
C---O---T--
-A-C-----O-
--L-------R