Rail Fence Cipher Program in Python Language
Rail Fence Cipher Program in Python Language
Introduction
The Rail Fence Cipher is a simple transposition cipher that involves writing the plaintext in a zigzag
pattern across a number of "rails," and then reading off the ciphertext by row. The cipher is named after
its resemblance to a zig-zag fence.
For example, let’s consider the plaintext “This is a secret message”.
To encode this message we will first write over two lines (the “the rails of the fence”) as follows:
Note that all white spaces have been removed from the plain text:
The ciphertext is then read off by writing the top row first, followed by the bottom row:
Algorithm:
The Rail Fence Cipher is a transposition cipher that rearranges the plaintext letters by writing them in a
zigzag pattern. Here's the algorithm for the Rail Fence Cipher:
Encryption Algorithm:
● Input: Plain text message and the number of rails (num_rails).
● Remove white spaces and special characters from the plaintext message.
● Create an empty matrix with num_rails rows and the length of the cleaned plaintext message
columns, initialized with dots ('.').
● Iterate through the matrix in a zigzag pattern and place each character of the message in the
appropriate position in the matrix.
● Concatenate the characters row-wise to get the encrypted text.
Decryption Algorithm:
● Input: Cipher text message and the number of rails (num_rails).
● Remove white spaces and special characters from the cipher text message.
● Create an empty matrix with num_rails rows and the length of the cleaned cipher text message
columns, initialized with dots ('.').
● Fill the matrix in a zigzag pattern with the characters of the cipher text.
● Reorder the matrix to reconstruct the original order of characters.
● Read the characters row-wise to obtain the decrypted text.
# Decryption Function
def cipher_decryption():
msg = input("Enter the message to decrypt: ")
rails = int(input("Enter the number of rails for decryption: "))
# Removing white space from message
msg = msg.replace(" ", "")
# Creating an empty matrix
railMatrix = [['.' for _ in range(len(msg))] for _ in range(rails)]
# Putting letters of message one by one in the matrix in zig-zag
row, check = 0, 0
for i in range(len(msg)):
if check == 0:
railMatrix[row][i] = msg[i]
row += 1
if row == rails:
check = 1
row -= 1
elif check == 1:
row -= 1
railMatrix[row][i] = msg[i]
if row == 0:
check = 0
row = 1
# Reordering the matrix
ordr = 0
for i in range(rails):
for j in range(len(msg)):
temp = railMatrix[i][j]
if re.search("\\.", temp):
continue
else:
railMatrix[i][j] = msg[ordr]
ordr += 1
# Putting reordered matrix into decrypted text string to get decrypted text
check, row = 0, 0
decryp_text = ""
for i in range(len(msg)):
if check == 0:
decryp_text += railMatrix[row][i]
row += 1
if row == rails:
check = 1
row -= 1
elif check == 1:
row -= 1
decryp_text += railMatrix[row][i]
if row == 0:
check = 0
row = 1
decryp_text = re.sub(r"\.", "", decryp_text)
print("Decrypted Text: {}".format(decryp_text))
if __name__ == "__main__":
main()