RAILFENCE DECRYPTION - PE
def rail_fence_decrypt(ciphertext, num_rails):
# Create a grid with empty placeholders
grid = [['' for _ in range(len(ciphertext))] for _ in range(num_rails)]
#Create the zigzag pattern on the grid
index, step = 0, 1
for i in range(len(ciphertext)):
grid[index][i] = '*'
if index == 0:
step = 1
elif index == num_rails - 1:
step = -1
index += step
#Fill the grid with ciphertext characters
idx = 0
for r in range(num_rails):
for c in range(len(ciphertext)):
if grid[r][c] == '*':
grid[r][c] = ciphertext[idx]
idx += 1
#Read the message from the grid in a zigzag pattern
decrypted_message = []
index, step = 0, 1
for i in range(len(ciphertext)):
decrypted_message.append(grid[index][i])
if index == 0:
step = 1
elif index == num_rails - 1:
step = -1
index += step
return ''.join(decrypted_message)
def main():
# Sample Ciphertext
ciphertext = "HOIEL REDLFN"
num_rails = 3
#Decrypting the message
decrypted = rail_fence_decrypt(ciphertext, num_rails)
#Display the results
print("\n--- Output ---")
print("Ciphertext:", ciphertext)
print("Decrypted (Rail Fence):", decrypted)
if name == "main":
main()