Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: railfence_cipher_tool.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- - This script provides a tool for encrypting and decrypting messages using the Rail Fence Cipher.
- - Additionally, it has the option to encrypt & decrypt a demo cipher using a quote by Margaret Thatcher.
- Requirements:
- - Python 3.x
- - The following modules:
- - math
- - itertools
- Functions:
- - main():
- The main function to run the program and interact with the user.
- - encrypt_message(plaintext):
- Encrypts a message using the Rail Fence Cipher.
- - prep_plaintext(plaintext):
- Removes spaces and converts the message to uppercase.
- - build_rails(message):
- Builds strings with every other letter in a message.
- - decrypt_message(ciphertext):
- Decrypts a message using the Rail Fence Cipher.
- - prep_ciphertext(ciphertext):
- Removes whitespace from the ciphertext.
- - split_rails(message):
- Splits the message into two parts for decryption.
- - decrypt(row1, row2):
- Builds the plaintext from two rows of ciphertext.
- - run_demo():
- Runs a demo with provided ciphertext.
- Usage:
- - Run the script and follow the on-screen menu to encrypt or decrypt messages using the Rail Fence Cipher.
- Additional Notes:
- - The Rail Fence Cipher is a transposition cipher that encrypts and decrypts messages by writing the characters in a zigzag pattern.
- - In the Rail Fence Cipher, the plaintext is written diagonally up and down in a zigzag pattern on successive "rails" of an imaginary fence, then read off row by row to produce the ciphertext.
- """
- import math
- import itertools
- def main():
- """Run program."""
- while True:
- print("_" * 21)
- print("\nRailfence Cipher Menu")
- print("_" * 21, "\n")
- print("1. Run Demo")
- print("2. Encrypt a message")
- print("3. Decrypt a message")
- print("0. Exit")
- choice = input("\nEnter your choice: ")
- if choice == '1':
- run_demo()
- elif choice == '2':
- plaintext = input("Enter the plaintext to encrypt: ")
- print()
- encrypt_message(plaintext)
- elif choice == '3':
- ciphertext = input("Enter the ciphertext to decrypt: ")
- print()
- decrypt_message(ciphertext)
- elif choice == '0':
- print()
- print("_" * 21)
- print("\nExiting program...\n\n 👋 GoodBye!")
- print("_" * 21, "\n")
- break
- else:
- print("Invalid choice. Please enter 1, 2, 3, or 4.")
- def encrypt_message(plaintext):
- """Encrypt a message using rail fence cipher."""
- message = prep_plaintext(plaintext)
- rails = build_rails(message)
- ciphertext = ' '.join([rails[i:i+5] for i in range(0, len(rails), 5)])
- print("ENCODED (Ciphertext): {}".format(ciphertext))
- print("FORMATTED (Ciphertext): {}".format(ciphertext.replace(" ", "").lower()))
- def prep_plaintext(plaintext):
- """Remove spaces & leading/trailing whitespace."""
- message = "".join(plaintext.split())
- message = message.upper() # convention for ciphertext is uppercase
- return message
- def build_rails(message):
- """Build strings with every other letter in a message."""
- evens = message[::2]
- odds = message[1::2]
- rails = evens + odds
- return rails
- def decrypt_message(ciphertext):
- """Decrypt a message using rail fence cipher."""
- formatted_ciphertext = ciphertext.replace(" ", "")
- print("ENCODED (Formatted): ", formatted_ciphertext.lower())
- message = prep_ciphertext(ciphertext)
- row1, row2 = split_rails(message)
- decrypt(row1, row2)
- def prep_ciphertext(ciphertext):
- """Remove whitespace."""
- message = "".join(ciphertext.split())
- return message
- def split_rails(message):
- """Split message in two, always rounding UP for 1st row."""
- row_1_len = math.ceil(len(message)/2)
- row1 = (message[:row_1_len]).lower()
- row2 = (message[row_1_len:]).lower()
- return row1, row2
- def decrypt(row1, row2):
- """Build list with every other letter in 2 strings & print."""
- plaintext = []
- for r1, r2 in itertools.zip_longest(row1, row2):
- plaintext.append(r1)
- plaintext.append(r2)
- if None in plaintext:
- plaintext.pop()
- print("DECODED (Plaintext): {}".format(''.join(plaintext)))
- def run_demo():
- """Run demo with provided ciphertext."""
- ciphertext = """YUNIO EYODR ALBTC NMSSR VLNNR SRCUE -AGRT HTHRO ADCMB RAORI ,UEOO ITTAE OIFAT UTR.M RAETA CE"""
- formatted_ciphertext = ciphertext.replace(" ", "")
- print("\nCIPHERTEXT: ", ciphertext, "\n")
- print("ENCODED (Formatted): ", formatted_ciphertext.lower())
- message = prep_ciphertext(ciphertext)
- row1, row2 = split_rails(message)
- decrypt(row1, row2)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement