


To do this, we will parse through each character in every row and append all the characters, which are not spaces, to an initially empty list. Voila! Our railfence is perfect! Getting our CipherText:įor getting the ciphertext, we need to read our grid row by row and eliminate the spaces between each letter in a row. For this, we use the join() function which will convert our list to a string. Now, let’s check if they are in the right position. for i in range(len(s)):Īt this point, we have filled in our plaintext characters into our grid.

So, if the row number of the current character is 0, the flag will be 0, and if the row number is Key-1 ie. We also need a condition to change the value of flags. So, if flag=0, increment row number, and if flag=1, decrement row number.

If flag=0, then we need to continue in the downward direction, and if flag=1, then travel in an upward direction. So we only need to determine the row number now. The character index will be the same as the column number in the grid. The next step is to parse through all the characters in the plain text and determine its position in the grid. Along with this, we also define a condition variable ‘flag’ which determines whether we should travel in an upward or a downward direction. So we define a ‘row’ variable to determine which row to add our character to. first or last row, it changes its direction and continues in the opposite direction. Once it reaches any extremity of the grid, ie. Then the following characters are put diagonally in the downward direction. Now, as we saw earlier, the first character is put in the first box of the grid ie. Thus a blank list of size 4*10 is created. Note that the size of the list is also defined along with the value initialization. Here we used list comprehension for initializing the list. To initialize the list, we first fill the list with ‘ ‘(single space). The size of the list as mentioned above will be “the value of key” * “length of the string”. You can also use NumPy arrays, but for simplicity, we have used lists here. To create the grid for our encryption process, we are using a blank list. The key decides the number of rows in the grid. This will act as our plain text.Īlso, we’ll need a key to encrypt our plain text. To start off, we’ll need an input string from the user. The number of columns in the grid= length of plain text = 10 Here, The number of rows in the grid = key = 4 Then the ciphertext is generated by reading the resultant grid row by row. For implementing Rail-fence Cipher in Python, a rectangular grid is required with the number of rows corresponding to the key, and the number of columns corresponding to the length of string to be encrypted. This cipher takes an input string and a key and arranges the letters in the string in a diagonal fashion. Let’s first see what Rail-fence cipher actually does. Rail- fence cipher is a transposition cipher that encrypts the plain text by changing the position of each character. In today’s tutorial, we will be Implementing Rail-fence Cipher in Python.
