def railfence_encrypt(plaintext, rails): plaintext = plaintext.replace(" ", "").lower() n = len(plaintext) fence = [[] for _ in range(rails)] rail = 0 direction = 1 for char in plaintext: fence[rail].append(char) rail += direction if rail == 0 or rail == rails - 1: direction *= -1 cipher_text = ''.join(''.join(row) for row in fence) return cipher_text def railfence_decrypt(ciphertext, rails): n = len(ciphertext) fence = [[None] * n for _ in range(rails)] index = 0 rail = 0 direction = 1 for i in range(n): fence[rail][i] = '*' rail += direction if rail == 0 or rail == rails - 1: direction *= -1 for r in range(rails): for i in range(n): if fence[r][i] == '*': fence[r][i] = ciphertext[index] index += 1 plaintext = [] rail = 0 direction = 1 for i in range(n): plaintext.append(fence[rail][i]) rail += direction if rail == 0 or rail == rails - 1: direction *= -1 return ''.join(plaintext) plaintext = "network ecurity" rails = 3 ciphertext = railfence_encrypt(plaintext, rails) print(f"Plaintext: {plaintext}") print(f"Ciphertext: {ciphertext}") decrypted_text = railfence_decrypt(ciphertext, rails) print(f"Decrypted text: {decrypted_text}")