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. Encryption 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 Decryption Let be the number of rails used during encryption. Observe that as the plaintext is written, the sequence of each letter's vertical position on the rails varies up and down in a repeating cycle. In the above example (where ) the vertical position repeats with a period of 4. In general the sequence repeats with a period of . Let be the length of the string to be decrypted. Suppose for a moment that is a multiple of and let . One begins by splitting the ciphertext into strings such that the length of the first and last string is and the length of each intermediate string is . For the above example with , we have , so we split the ciphertext as follows: 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 is not a multiple of ,