# Rail Fence Cipher

1. Cipher Activity
2. Introduction
3. Encryption
4. Decryption
5. Discussion
6. Exercise

The railfence cipher is an easy to apply transposition cipher that jumbles up the order of the letters of
a message in a quick convenient way. It also has the security of a key to make it a little bit harder to
break.
The Rail Fence cipher works by writing your message on alternate lines across the page, and then
reading off each line in turn. For example, the plaintext "defend the east wall" is written as shown
below, with all spaces removed.

The simplest Rail Fence Cipher, where each letter is written in a zigzag pattern across the page.
The ciphertext is then read off by writing the top row first, followed by the bottom row, to get
"DFNTEATALEEDHESWL".
Encryption
To encrypt a message using the Rail Fence Cipher, you have to write your message in zigzag lines
across the page, and then read off each row. Firstly, you need to have a key, which for this cipher is
the number of rows you are going to have. You then start writing the letters of the plaintext diagonally
down to the right until you reach the number of rows specified by the key. You then bounce back up
diagonally until you hit the first row again. This continues until the end of the plaintext.
For the plaintext we used above, "defend the east wall", with a key of 3, we get the encryption process
shown below.

From this we can now read the plaintext off following the diagonals to get "they are attacking from the north". The Rail Fence Cipher with a key of 3. The third stage in the decryption process. These are called nulls. You then dash the diagonal down spaces until you get back to the top row. and dashes diagonally downwards where the letters will be. it makes the decryption process a lot easier if the message has this layout. if you receive the ciphertext "TEKOOHRACIRMNREATANFTETYTGHH". We start by making a grid with as many rows as the key is. and even more quickly with a computer. we get the successive stages shown below. Notice the nulls added at the end of the message to make it the right length. This can be . The fourth and final stage in the decryption process. Although not necessary. we place the next letter in the ciphertext. and as many columns as the length of the ciphertext. but leaving a dash in place of the spaces yet to be occupied. the length of the message needs to be at lease twice the key. The first row of the decryption process for the Rail Fence Cipher. Note that at the end of the message we have inserted two "X"s. The ciphertext is read off row by row to get "DNETLEEDHESWLXFTAAX". Discussion The Rail Fence Cipher is a very easy to apply transposition cipher. you start by placing the "T" in the first square. and 28 columns as the ciphertext has length 28. you can replace all the dashes with the corresponding letters. and start the next row when you reach the end. We do this to make the message fit neatly in to the grid (so that there are the same number of letters on the top row. Continue like this across the row. For example. encrypted with a key of 4. and place the "E" here. since there are a limited number of usable keys. The second stage in the decryption process. Gradually. When we get back to the top row. and read off the plaintext from the table. However. Decryption The decryption process for the Rail Fence Cipher involves reconstructing the diagonal grid used to encrypt the message. especially for short messages (for there to be enough movement of letters. You could process these quite quickly by hand. Continuing to fill the top row you get the pattern below. as an interceptor can use them to identify where the end of the line is. Continuing this row-by-row. We start writing the message. it is not particularly secure. We have a table with 4 rows because the key is 4. We then place the first letter in the top left square. and so have a sensible guess at the key. as on the bottom row. but preferably 3 times the key). and act as placeholders. The use of nulls can also have a detrimental effect on the security of the cipher.

Encryption 3. is to keep the spaces as characters. lets encrypt the plaintext "abort the mission. The plaintext is written in a grid. . you use the Route assigned.averted by using a more common letter. to fill the null spaces. Once the plaintext is written out in the grid. The Rail Fence Cipher can also be utilised without the use of nulls. lets say 5. using the plaintext "defend the east wall" with a key of 3 again. For example. This could be spiralling inwards from the top right corner in a clockwise direction. One way to also make the encryption a little bit more secure. They are treated in exactly the same way as any other letter. As an example. and then read off following the route chosen Encryption First we write the plaintext in a block of reasonable size for the plaintext. as it will still be clear to the recipient that these are not part of the message as they will appear at the end of the plaintext. Colour is used to emphasise where spaces are. but this time including spaces we get the table below. Part of your key is the size of this grid. The Rail Fence Cipher with spaces left in the message. you have been spotted". against the blank squares of the table. or zigzagging up and down. so you need to decide on either a number of columns or number of rows in the grid before starting. First we need to decide on the number of columns we are going to use. such as "E". Discussion The Route Cipher is a transposition cipher where the key is which route to follow when reading the ciphertext from the block created with the plaintext. Route Cipher 1. Introduction 2. Decryption 4. and include them in the encryption table. So the ciphertext would read "DNHAWXEEDTEES ALF TL".

the cipher clerks would even add whole null words to the ciphertext. with a grid width of 4. for a suitably lengthy message. reading off the plaintext we get "We are discovered". Rather than transposing letters by the given route. Discussion The Route Cipher is easy to use to jumble up a message very quickly. The plaintext written in a grid with 5 columns. and then place the ciphertext letters in the grid following the route specified. of vital importance were not protected in this way. direction. there are potentially infinitely many different routes that could be taken through the grid. We at the top right square. Some words. An historical use of the Route Cipher was the Union Route Cipher used by the Union forces during the American Civil War. Sometimes. With a route of spiralling inwards counter-clockwise from the bottom right we get: "XTEAN ITROB ATSYV NTEDX OEHOM EHSOE SPBUI". We then choose which route we want to use to encrypt the message. characters in the ciphertext. we need to know the route used and the width or height of the grid. to decrypt the ciphertext "RAEWE CREDX ESIDO V" with the route spiral inwards counter-clockwise from the top right. Decryption To decrypt a message received that has been encoded with the Route Cipher. we follow the process shown below We start by drawing a grid that is 4x4. Notice how we have used nulls at the end of the message to make a nice rectangle. often making the message humorous. it moved whole words around. It has some problems depending on what route you pick as you can end up with large chunks of plaintext in the right order within the ciphertext (or simply reversed) which gives away alot about the width of the grid and the route used. For example. However. Now. Careful selection of the route is important. With a route of reading down the columns we get the ciphertext: "ATSYV NTBHS OESEO EIUBP DRMOH EOXTI NAETX". and begin to Continue in the spiralling direction until since the width is 4. Columnar Transposition . so they were first encoded using a codeword. It is limited only by imagination and ease of communication of the route. We then start by constructing a blank grid of the right size. and there are 16 spiral inwards in a counter-clockwise the whole ciphertext is placed in the grid.