Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: cipher_tool_101.py
- # Version: 1.0.1
- # Author: Jeoi Reqi
- """
- This script serves as a comprehensive tool for encoding and decoding various classical ciphers as well as Fernet encryption and decryption.
- It offers functionalities for a range of encoding and decoding techniques for the following:
- 1: Caesar Cipher:
- - Allows encoding and decoding text by shifting characters a fixed number of positions in the alphabet.
- 2: A1Z26 Cipher:
- - For encoding, it converts alphabetic characters into their corresponding numeric positions in the alphabet.
- - During decoding, it reverses this process, converting numeric sequences back into alphabetic characters.
- - Note: Spaces in the encoded text are represented by '0'.
- 3: Atbash Cipher:
- - Inverts the alphabet, substituting each letter with its reverse counterpart.
- 4: Rail Fence Cipher:
- - Utilizes a transposition technique, encoding text by writing it in a zigzag pattern across a specified number of "rails".
- Then it reads off the encoded text row by row to decode to plaintext.
- 5: Vigenère Cipher:
- - Employs a keyword to encode text, shifting characters in a repeating pattern based on the keyword's letters.
- - Decoding with Vigenère reverses this process, decrypting text based on the keyword.
- 6: Fernet Encryption/Decryption:
- - Encrypts the input text using the Fernet symmetric encryption algorithm.
- - Decrypts the input text using the same Fernet key used for encryption.
- Requirements:
- - Python 3.x
- - Colorama (install with 'pip install colorama')
- - cryptography library (install with 'pip install cryptography')
- Usage:
- - Run the script and choose a cipher type by entering the corresponding number.
- - Follow the prompts to enter the text and any required parameters.
- - The script will output the encoded or decoded text.
- Additional Notes:
- - For Rail Fence cipher, the number of rails must be a positive integer.
- - For Vigenère cipher, the key should be a string of alphabetic characters.
- - For A1Z26 cipher:
- - When encoding, the input text should consist of alphabetic characters.
- - When decoding, the input text should consist of numeric characters separated by spaces.
- """
- # IMPORTS
- from colorama import Fore, Style
- from cryptography.fernet import Fernet
- import os
- # CAESAR CIPHER
- def caesar_cipher(text, shift, mode):
- """
- Caesar cipher encoding or decoding.
- Args:
- text (str): The text to be encoded or decoded.
- shift (int): The shift value for encoding or decoding.
- mode (str): '1' for encoding, '2' for decoding.
- Returns:
- str: The encoded or decoded text.
- """
- result = ""
- for char in text:
- if char.isalpha():
- if mode == '1': # Encode
- if char.islower():
- result += chr((ord(char) - 97 + shift) % 26 + 97)
- else:
- result += chr((ord(char) - 65 + shift) % 26 + 65)
- elif mode == '2': # Decode
- if char.islower():
- result += chr((ord(char) - 97 - shift) % 26 + 97)
- else:
- result += chr((ord(char) - 65 - shift) % 26 + 65)
- else:
- result += char
- return result
- # A1Z26 CIPHER
- def a1z26_cipher(text, mode):
- """
- A1Z26 cipher encoding or decoding.
- Args:
- text (str): The text to be encoded or decoded.
- mode (str): '3' for encoding, '4' for decoding.
- Returns:
- str: The encoded or decoded text.
- """
- if mode == '3': # ENCODE
- result = ' '.join(str(ord(char) - 96) if char.isalpha() else '0' for char in text)
- elif mode == '4': # DECODE
- result = ''
- nums = text.split()
- for num in nums:
- if num == '0':
- result += ' '
- else:
- result += chr(int(num) + 96)
- else:
- result = "\nInvalid mode! Please choose '3' for encoding or '4' for decoding.\n"
- return result
- # ATBASH CIPHER
- def atbash_cipher(text, mode):
- """
- Atbash cipher encoding or decoding.
- Args:
- text (str): The text to be encoded or decoded.
- mode (str): '5' for encoding, '6' for decoding.
- Returns:
- str: The encoded or decoded text.
- """
- result = ""
- for char in text:
- if char.isalpha():
- if char.islower():
- result += chr(122 - ord(char) + 97)
- else:
- result += chr(90 - ord(char) + 65)
- else:
- result += char
- return result
- # RAILFENCE CIPHER
- def encode_RailFence(text, key):
- """
- Rail Fence cipher encoding.
- Args:
- text (str): The text to be encoded.
- key (int): The number of rails.
- Returns:
- str: The encoded text.
- """
- rail = [['\n' for i in range(len(text))] for j in range(key)]
- dir_down = False
- row, col = 0, 0
- for i in range(len(text)):
- if (row == 0) or (row == key - 1):
- dir_down = not dir_down
- rail[row][col] = text[i]
- col += 1
- if dir_down:
- row += 1
- else:
- row -= 1
- result = []
- for i in range(key):
- for j in range(len(text)):
- if rail[i][j] != '\n':
- result.append(rail[i][j])
- return ''.join(result)
- # VIGENERE CIPHER
- def vigenere_cipher(text, key, mode):
- """
- Vigenère cipher encoding or decoding.
- Args:
- text (str): The text to be encoded or decoded.
- key (str): The key for encoding or decoding.
- mode (str): '9' for encoding, '10' for decoding.
- Returns:
- str: The encoded or decoded text.
- """
- result = ""
- key_length = len(key)
- key_index = 0
- for char in text:
- if char.isalpha():
- if mode == '9': # Encode
- if char.islower():
- shift = ord(key[key_index].lower()) - 97
- result += chr((ord(char) - 97 + shift) % 26 + 97)
- else:
- shift = ord(key[key_index].lower()) - 97
- result += chr((ord(char) - 65 + shift) % 26 + 65)
- elif mode == '10': # Decode
- if char.islower():
- shift = ord(key[key_index].lower()) - 97
- result += chr((ord(char) - 97 - shift) % 26 + 97)
- else:
- shift = ord(key[key_index].lower()) - 97
- result += chr((ord(char) - 65 - shift) % 26 + 65)
- key_index = (key_index + 1) % key_length
- else:
- result += char
- return result
- # GENERATE KEY
- def generate_key():
- """
- Generate a new Fernet key and save it to a file.
- Returns:
- bytes: The generated Fernet key.
- """
- key = Fernet.generate_key()
- with open("key.txt", "wb") as key_file:
- key_file.write(key)
- return key
- # LOAD STORED KEY FROM FILE
- def load_key():
- """
- Load the Fernet key from the key file if it exists.
- Returns:
- bytes: The loaded Fernet key if exists, otherwise None.
- """
- if os.path.exists("key.txt"):
- with open("key.txt", "rb") as key_file:
- return key_file.read()
- else:
- print("\nNo saved key found!\n\n- Generating a new 32-byte Fernet key...\n")
- return generate_key()
- # DELETE STORED KEY
- def delete_key():
- """
- Delete the key file if it exists.
- """
- if os.path.exists("key.txt"):
- os.remove("key.txt")
- print("\nKey deleted!")
- else:
- print("\nNo key to delete!")
- def fernet_encode():
- """
- Encode the input text using the Fernet symmetric encryption algorithm.
- """
- while True:
- print("\nChoose an option:")
- print("1. Generate Key")
- print("2. Delete Key")
- print("3. Use Stored Key")
- key_choice = input("\nEnter your choice: ")
- if key_choice == '1': # Generate Key
- key = generate_key()
- print("\nGenerated new key:\n", key.decode())
- text = input("\nEnter the text to encode using Fernet: ")
- print("Encoding with the generated key...")
- cipher_suite = Fernet(key)
- encoded_text = cipher_suite.encrypt(text.encode())
- print("\nEncoded text:\n", encoded_text.decode())
- break # Exit the loop after encoding
- elif key_choice == '2': # Delete Key
- delete_key()
- elif key_choice == '3': # Use Stored Key
- key = load_key()
- if key is not None:
- print("Encoding with the stored key:\n", key.decode())
- text = input("\nEnter the text to encode using Fernet: ")
- cipher_suite = Fernet(key)
- encoded_text = cipher_suite.encrypt(text.encode())
- print("\nEncoded text:\n", encoded_text.decode())
- break # Exit the loop after encoding
- else:
- print("\nNo key found! Please generate or load a key.")
- else:
- print("\nInvalid choice! Please enter 1, 2, or 3.")
- def fernet_decode():
- """
- Decode the input text using the Fernet symmetric encryption algorithm.
- Returns:
- str: The decoded text.
- """
- while True:
- print("\nChoose an option:")
- print("1. Input Key Manually")
- print("2. Use Stored Key")
- key_choice = input("\nEnter your choice: ")
- if key_choice == '1': # Input Key Manually
- key = input("\nEnter the Fernet key: ").encode()
- cipher_suite = Fernet(key)
- encoded_text = input("\nEnter the text to decode using Fernet: ")
- decoded_text = cipher_suite.decrypt(encoded_text.encode()).decode()
- print("\nDecoded text:\n", decoded_text)
- break # Exit the loop after decoding
- elif key_choice == '2': # Use Stored Key
- key = load_key()
- if key is not None:
- encoded_text = input("\nEnter the text to decode using Fernet: ")
- cipher_suite = Fernet(key)
- decoded_text = cipher_suite.decrypt(encoded_text.encode()).decode()
- print("\nDecoded text:\n", decoded_text)
- break # Exit the loop after decoding
- else:
- print("\nNo key found! Please generate or load a key.")
- else:
- print("\nInvalid choice! Please enter 1 or 2.")
- # MAIN MENU & FUNCTION CALLS
- def main():
- """
- Main function to run the cipher tool.
- """
- while True:
- print("-" * 28)
- print(Style.BRIGHT + Fore.GREEN + " :: CIPHER TOOL ::" + Style.RESET_ALL)
- print("-" * 28)
- print(Style.BRIGHT +"\nCHOOSE A CIPHER TYPE:\n" + Style.RESET_ALL)
- print(Style.BRIGHT + "01. " + Fore.GREEN + "Caesar Encode [+]" + Style.RESET_ALL)
- print(Style.BRIGHT + "02. " + Fore.YELLOW + "Caesar Decode [-]" + Style.RESET_ALL)
- print(Style.BRIGHT + "03. " + Fore.GREEN + "A1Z26 Encode [+]" + Style.RESET_ALL)
- print(Style.BRIGHT + "04. " + Fore.YELLOW + "A1Z26 Decode [-]" + Style.RESET_ALL)
- print(Style.BRIGHT + "05. " + Fore.GREEN + "Atbash Encode [+]" + Style.RESET_ALL)
- print(Style.BRIGHT + "06. " + Fore.YELLOW + "Atbash Decode [-]" + Style.RESET_ALL)
- print(Style.BRIGHT + "07. " + Fore.GREEN + "Rail Fence Encode [+]" + Style.RESET_ALL)
- print(Style.BRIGHT + "08. " + Fore.YELLOW + "Rail Fence Decode [-]" + Style.RESET_ALL)
- print(Style.BRIGHT + "09. " + Fore.GREEN + "Vigenère Encode [+]" + Style.RESET_ALL)
- print(Style.BRIGHT + "10. " + Fore.YELLOW + "Vigenère Decode [-]" + Style.RESET_ALL)
- print(Style.BRIGHT + "11. " + Fore.GREEN + "Fernet Encode [+]" + Style.RESET_ALL)
- print(Style.BRIGHT + "12. " + Fore.YELLOW + "Fernet Decode [-]" + Style.RESET_ALL)
- print(Style.BRIGHT + " 0. " + Fore.RED + "Exit [!]" + Style.RESET_ALL)
- choice = input(Style.BRIGHT +"\nEnter your choice (1-12)\nOr... type '0' to EXIT: " + Style.RESET_ALL)
- if choice == '1' or choice == '2':
- mode = choice
- text = input("\nEnter the text: ")
- shift = int(input("\nEnter the shift value: "))
- print("\nEncoded/Decoded text:", caesar_cipher(text, shift, mode))
- elif choice == '3' or choice == '4':
- mode = choice
- if mode == '3':
- text = input("\nEnter the plaintext to encode: ")
- else:
- text = input("\nEnter the ciphertext to decode: ")
- print("\nEncoded/Decoded text:", a1z26_cipher(text, mode))
- elif choice == '5' or choice == '6':
- mode = choice
- if mode == '5':
- text = input("\nEnter the plaintext to encode: ")
- else:
- text = input("\nEnter the ciphertext to decode: ")
- print("\nEncoded/Decoded text:", atbash_cipher(text, mode))
- elif choice == '7':
- text = input("\nEnter the plaintext to encode: ")
- key = int(input("\nEnter the number of rails: "))
- print("\nEncoded text:", encode_RailFence(text, key))
- elif choice == '8':
- text = input("\nEnter the ciphertext to decode: ")
- key = int(input("\nEnter the number of rails: "))
- print("\nDecoded text:", decode_RailFence(text, key))
- elif choice == '9' or choice == '10':
- mode = choice
- if mode == '9':
- text = input("\nEnter the plaintext to encode: ")
- else:
- text = input("\nEnter the ciphertext to decode: ")
- key = input("\nEnter the key: ")
- print("\nEncoded/Decoded text:", vigenere_cipher(text, key, mode))
- elif choice == '11':
- key = fernet_encode()
- if key is not None:
- text = input("\nEnter the text to encode using Fernet: ")
- cipher_suite = Fernet(key)
- encoded_text = cipher_suite.encrypt(text.encode())
- print("\nEncoded text:\n", encoded_text.decode())
- break # Exit the loop after encoding
- elif choice == '12':
- key = fernet_decode()
- if key is not None:
- encoded_text = input("\nEnter the text to decode using Fernet: ")
- cipher_suite = Fernet(key)
- decoded_text = cipher_suite.decrypt(encoded_text.encode()).decode()
- print("\nDecoded text:\n", decoded_text)
- break # Exit the loop after decoding
- elif choice == '0':
- print()
- print("-" * 28)
- print("Exiting Program... Goodbye!")
- print("-" * 28)
- break
- else:
- print("\nInvalid choice! Please enter a number between 1 and 12.\n")
- input("\nPress [ENTER] to continue...\n\n")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement