<br>
Practical No. 8
Aim: Write a program to implement Rail fence technique.
Introduction:
Rail- fence cipher is a transposition cipher that encrypts the plain text by
changing the position of each character.
This cipher takes an input string and a key and arranges the letters in the string in a
diagonal fashion. 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. Then the ciphertext is generated by
reading the resultant grid row by row.
For example
Plain Text: CODESPEEDY
Key= 4
c m
m
P
Cipher Text: CEOPEDSDEY
Here, The number of rows in the grid = key = 4
<br>
The number of columns in the grid= length of plain text = 10
Program:
# Python3 program to illustrate
# Rail Fence Cipher Encryption
# and Decryption
# function to encrypt a message
def encryptRailFence(text, key):
# create the matrix to cipher
,
# plain text key = rows
# length(text) = columns
# filling the rail matrix
# to distinguish filled
# spaces from blank ones
rail = [[\n' for i in range(len(text)]
for j in range(key)]
# to find the direction
dir_down = False
row, col= 0,0
for i in range(len(text):
# check the direction of flow
# reverse the direction if we've just
# filled the top or bottom rail
if (row = 0) or (row == key - 1):
= not dir_down
dir down
# fill the corresponding alphabet
rail[rowl[col] = text[i]
col += 1l
# find the next row using
# direction flag
if dir_down:
<br>
row += 1
else:
row -= 1
# now we can construct the cipher
# using the rail matrix
result = ([0
for i in range(key):
for j in range(len(text)):
if rail[i]lj] =n':
result.append(rail[i][j)
return(""- join(result)
# This function receives cipher-text
# and key and returns the original
# text after decryption
def decryptRailFence(cipher, key):
# create the matrix to cipher
# plain text key = rows ,
# length(text) = columns
# filling the rail matrix to
# distinguish filled spaces
# from blank ones
rail = [[\n' for iin range(len(cipher)]
for j in range(key)]
# to find the direction
dir down = None
row, col=0,0
# mark the places with '*
for iin range (len(cipher)):
if row ==0:
dir_down = True
if row == key - 1:
dir down = False
# place the marker
rail[row][col] = *
<br>
col +=1
# find the next row
#using direction flag
if dir_down:
row += 1
else:
row -=1
# now we can construct the
# fill the rail matrix
index = 0
for i in range (key):
for j in range(len(cipher):
if (rail[i]li] == "*) and
(index < len(cipher))):
raillilj] = cipher[index]
index +=1
# now read the matrix in
# zig-zag manner to construct
# the resultant text
result = [I
row, col=0,0
for in range(len(cipher)):
i
# check the direction of flow
if row == 0:
dir down = True
if row == key-1:
dir _down = False
# place the marker
if (rail[rowl[col] != '*):
result.append(rail[row][col)
col += 1
# find the next row using
# direction flag
<br>
if dir down:
row +=]
else:
row -=1
return(""join(result)
# Driver code
if name ==" main
print(encryptRailFence("attack at once", 2))
print(encryptRailFence("GeeksforGeeks ", 3))
print(encryptRailFence("defend the east wall", 3)
# Now decryption of the
# same cipher-text
print(decryptRailFence("GsGsekfrek eoe", 3))
print(decryptRailFence("atc toctaka ne", 2))
print(decryptRailFence("dnhaweedtees alf tl", 3))
Output:
<br>
4 Python 3.83 Shell
ile Edit Shell Debug Options Window Help
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) (MSC v.1924 64 bit (AM
364) ] on win32
type "help",
>>
"copyright", "credits" or "license ()" for more intormation.
RESTART: C:/Users/Rohit/AppData/Local/Proqrams/ Python/Python 38/rail fence.py
ne
stc toctaka eoe
3sGsekfrek
inhaweedtees alf tl
jeeksforGeeks
Attack at once
jelendfthe east wal
>>
Conclusion:
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
cipher text is then read off in rows.