class Bifid(Cipher): """Implements word Bifid cipher which is a combination of Transposition and Polybius ciphers.""" def __init__(self, keyword): """Initiates class that takes a single string as a keyword argument. Creates an instance of WordCipher passing in the keyword as an argument. Creates an instance of Polybius. """ self.keyword = keyword self.word_cipher = WordCipher(keyword) self.poly = Polybius() def encrypt(self, text): """Takes a single argument of text which is the message to be encrypted Passes text to the instance of WordCipher encrypt method. The returned value is passed to the instance of Polybius encrypt method. Returns that value as the encryption. """ step_one = self.word_cipher.encrypt(text) step_two = self.poly.encrypt(step_one) return step_two def decrypt(self, enc_list): """Takes a single argument of enc_list which is the encrypted message. Passes enc_list to the instance of Polybius decrypt method. The returned value is passed to the instance of WordCipher decrypt method. Returns that value as the decrypted message. """ step_one = self.poly.decrypt(enc_list) step_two = self.word_cipher.decrypt(step_one) return step_two
def __init__(self, keyword): """Initiates class that takes a single string as a keyword argument. Creates an instance of WordCipher passing in the keyword as an argument. Creates an instance of Polybius. """ self.keyword = keyword self.word_cipher = WordCipher(keyword) self.poly = Polybius()
def main(): main_menu() # Main Menu myPolybius = Polybius() try: main_choice = input("[+] Enter your choice: ").lower().strip() except: main() if main_choice == "i": # Instructions instructions() input("Press <Enter> to return to the main menu... ") main() elif main_choice == "e": # Encipher clear() print("\v\tEnter your plaintext to encipher: ") try: sentence = input("--> ") except KeyboardInterrupt: main() print("\v\tYour ciphertext is: \n") print(myPolybius.encipher(sentence)) input("\n\vPress <Enter> to return to the main menu... ") main() elif main_choice == "d": # Decipher clear() print("\v\tEnter your ciphertext to decipher: ") try: sentence = input("--> ") except KeyboardInterrupt: main() print("\v\tYour message decrypted is: ") print(myPolybius.decipher(sentence)) input("\n\nPress <Enter> to return to the main menu... ") main() elif main_choice == "q": # Quit quit() input( " ----------------------------------------------------------------" ) exit(0) else: error("I didn't understand your choice !\n") input("Press <Enter> to try again... ") main()
def main(): main_menu() # Main Menu myPolybius = Polybius() try: main_choice = input('[+] Enter your choice: ').lower().strip() except: main() if main_choice == 'i': # Instructions instructions() input('Press <Enter> to return to the main menu... ') main() elif main_choice == 'e': # Encipher clear() print("\v\tEnter your plaintext to encipher: ") try: sentence = input("--> ") except KeyboardInterrupt: main() print("\v\tYour ciphertext is: \n") myPolybius.encipher(sentence) input('\n\vPress <Enter> to return to the main menu... ') main() elif main_choice == 'd': # Decipher clear() print("\v\tEnter your ciphertext to decipher: ") try: sentence = input("--> ") except KeyboardInterrupt: main() print("\v\tYour message decrypted is: ") myPolybius.decipher(sentence) input('\n\nPress <Enter> to return to the main menu... ') main() elif main_choice == 'q': # Quit quit(); input(" ----------------------------------------------------------------") exit(0) else: error("I didn't understand your choice !\n") input('Press <Enter> to try again... ') main()
def encrypt(string, cipher, cipher_pad): if cipher == 'atbash': atbash = AtBash() return atbash.encrypt(string, cipher_pad) elif cipher == 'caesar': caesar = Caesar() return caesar.encrypt(string, cipher_pad) elif cipher == 'polybius': polybius = Polybius() return polybius.encrypt(string, cipher_pad) elif cipher == 'keyword': keyword = Keyword() return keyword.encrypt(string, cipher_pad)
def select_cipher(): helpers.clear_screen() print("Please pick one of the following ciphers:") print() for cipher in ciphers: print(f"- {cipher}") print() while True: try: cipher = input("Which cipher would you like to use? ") if cipher.upper() == "ATBASH": cipher = Atbash() elif cipher.upper() == "CAESAR": cipher = Caesar() elif cipher.upper() == "POLYBIUS": cipher = Polybius() elif cipher.upper() == "KEYWORD": keyword = input( "Which word would you like to use as a keyword? ") cipher = Keyword(keyword) else: raise ValueError("That cipher doesn't exist " + "or has not yet been implemented.") except ValueError as error: print(error) print() else: break return cipher
def run_cipher(cipher_choice, message, encode_choice): '''Executes the chosen cipher''' if cipher_choice == 'caesar': if encode_choice == 'encrypt': return Caesar().encrypt(message) elif encode_choice == 'decrypt': return Caesar().decrypt(message) elif cipher_choice == 'keyword': if encode_choice == 'encrypt': return Keyword().encrypt(message) elif encode_choice == 'decrypt': return Keyword().decrypt(message) elif cipher_choice == 'affine': if encode_choice == 'encrypt': return Affine().encrypt(message) elif encode_choice == 'decrypt': return Affine().decrypt(message) elif cipher_choice == 'polybius': if encode_choice == 'encrypt': return Polybius().encrypt(message) elif encode_choice == 'decrypt': return Polybius().decrypt(message)
def script(): """Runs the script for the command line menu, asking the user for the message to encrypt/decrypt""" atb = Atbash() key = Keyword() poly = Polybius() choose_cipher = input("Which one do you want to use? ") if choose_cipher.upper() == "ATBASH": action = user_input() if action.upper() == "ENCRYPT": print(encrypt_it(atb)) elif action.upper() == "DECRYPT": print(decrypt_it(atb)) else: print("Sorry I didn't understand that...\n") elif choose_cipher.upper() == "KEYWORD": our_keyword = input("What keyword would you like to use? ") key.get_keyword(our_keyword) action = user_input() if action.upper() == "ENCRYPT": print(encrypt_it(key)) elif action.upper() == "DECRYPT": print(decrypt_it(key)) else: print("Sorry I didn't understand that...\n") elif choose_cipher.upper() == "POLYBIUS": action = user_input() if action.upper() == "ENCRYPT": print(encrypt_it(poly)) elif action.upper() == "DECRYPT": print(decrypt_it(poly)) else: print("Sorry I didn't understand that...\n") else: print("I didn't understand that, please try again!\n") # ask the user if he wants to encrypt/decrypt something else encrypt_more = input("Encrypt or decrypt something else? Y/N ") if encrypt_more.upper() != "N": script() else: print("Thank you, bye!")
def cipher_selection(): """The first screen shows all the cipher options to encrypt and decrypt.""" selection_ed = (input("To encrypt a message, press E.\n" "To decrypt a message, press D.\n" "To quit, press Q.\n")).upper() if selection_ed == 'E': cipher_option = (input("Please select one of these cipher options:\n" "Affine: Press AFE\n" "Atbash: Press ABE\n" "Polybius: Press POE\n" "To quit, press Q.\n")).upper() if cipher_option == "AFE": a = int( input( 'Enter an alpha. Available values: 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25\n' )) if a not in [3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]: print('Alpha value is not accepted. Please try again.\n') a = int( input( 'Enter an alpha. Available values: 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25\n' )) b = int(input('Enter a beta value. Integer numbers only.\n')) text = (input('What is your message?\n')).upper() x = Affine(a, b) x.encrypt(text) menu_back() if cipher_option == 'ABE': text = (input('What is your message?\n')).upper() x = Atbash() x.encrypt(text) menu_back() if cipher_option == 'POE': text = (input('What is your message?\n')).upper() x = Polybius() x.encrypt(text) menu_back() if cipher_option == 'Q': print('You quit.\n') os.system('clear') else: print('Not valid. Please try again.') menu_back() if selection_ed == 'D': cipher_option = (input("Please select one of these cipher options:\n" "Affine: Press AFD\n" "Atbash: Press ABD\n" "Polybius: Press POD\n" "To quit, enter Q.\n")).upper() if cipher_option == "AFD": a = int( input( 'Enter an alpha. Available values: 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25\n' )) b = int(input('Enter a beta value.\n')) text = (input('What is your message?\n')).upper() x = Affine(a, b) x.decrypt(text) menu_back() if cipher_option == 'ABD': text = (input('What is your message?\n')).upper() x = Atbash() x.decrypt(text) menu_back() if cipher_option == 'POD': text = (input('What is your message?\n')).upper() x = Polybius() x.decrypt(text) menu_back() else: print('Not valid. Please try again.\n') menu_back() if selection_ed == 'Q': print('You quit.\n') os.system('clear') else: print('Not valid.\n') menu_back()