def setUp(self): self.test_word = 'testy' self.pad_word = 'loose' self.cipher = Cipher() self.affine = Affine(5, 9) self.atbash = Atbash() self.caesar = Caesar() self.keyword_cipher = Keyword(self.pad_word)
class CipherTests(unittest.TestCase): def setUp(self): self.test_word = 'testy' self.pad_word = 'loose' self.cipher = Cipher() self.affine = Affine(5, 9) self.atbash = Atbash() self.caesar = Caesar() self.keyword_cipher = Keyword(self.pad_word) def test_char_blocks(self): assert self.cipher.char_blocks('testytestytest') == 'testy testy test' assert self.cipher.char_blocks('exactlyten') == 'exact lyten' def test_use_pad(self): assert self.cipher.use_pad(self.test_word, self.pad_word) == 'ESGLC' def test_affine_encrypt(self): """Affine examples from http://crypto.interactive-maths.com/affine-cipher.html.""" assert self.affine.encrypt(self.test_word) == 'ADVAZ' def test_affine_decrypt(self): """Affine examples from http://crypto.interactive-maths.com/affine-cipher.html.""" assert self.affine.decrypt('ADVAZ') == self.test_word.upper() def test_atbash_encrypt(self): """Atbash examples from http://crypto.interactive-maths.com/atbash-cipher.html.""" assert self.atbash.encrypt(self.test_word) == 'GVHGB' def test_atbash_decrypt(self): """Atbash examples from http://crypto.interactive-maths.com/atbash-cipher.html.""" assert self.atbash.decrypt('GVHGB') == self.test_word.upper() def test_caesar_encrypt(self): """Caesar shifts by 3 every time. Caesar cipher examples from http://crypto.interactive-maths.com/caesar-cipher.html.""" assert self.caesar.encrypt(self.test_word) == 'WHVWB' def test_caesar_decrypt(self): """Caesar shifts by 3 every time. Caesar cipher examples from http://crypto.interactive-maths.com/caesar-cipher.html.""" assert self.caesar.decrypt('WHVWB') == self.test_word.upper() def test_keyword_encrypt(self): assert self.keyword_cipher.encrypt(self.test_word) == 'TARTY' def test_keyword_decrypt(self): assert self.keyword_cipher.decrypt('TARTY') == self.test_word.upper()
def produce_cipher(command, selection, message): """takes user message, encryption or decryption preference, and cipher option to create a new cipher object and store it in the global cipher database if it does not currently exist""" if command == "encrypt": if selection == "Polybius": newcipher = Polybius(message, "encrypt") elif selection == "Bifid": newcipher = Bifid(message, "encrypt") elif selection == "Keyword": newcipher = Keyword(message, "encrypt") else: raise ValueError("The cipher type selection was not valid") newcipher.encrypt() CIPHERBANK.append(newcipher) alert = "Your message has been placed into our cipher " \ "database; Press ENTER to view the encryption" return blocker(newcipher.encrypted, newcipher), alert elif command == "decrypt": if selection == "Polybius": newcipher = Polybius(message, "decrypt") elif selection == "Bifid": newcipher = Bifid(message, "decrypt") elif selection == "Keyword": newcipher = Keyword(message, "decrypt") else: raise ValueError( "The cipher type selection was not valid (line 126)") newcipher.decrypt() result = True for e in CIPHERBANK: if e.message.upper() == newcipher.decrypted: result = e.pad_check() break if result: alert = "Your message has been decrypted; Press ENTER to view it" return newcipher.decrypted, alert else: alert = "You do not have the correct PIN - " return "Your message was not decrypted", alert
def encrypt(code, string): ''' Encrypts the given string depending on whether code is equal to 'atbash', 'polybius', or 'keyword'. ''' if code == 'atbash': atbash = Atbash() return atbash.encrypt(string) elif code == 'polybius': poly = Polybius() return poly.encrypt(string) elif code == 'keyword': key = Keyword(keyword) return key.encrypt(string)
def keyword_encrypt(): """Will encrypt message using the keyword cipher""" user_message = input("What would you like to encrypt? ") encrypted_bifid_message = Keyword(user_message).encrypt() print("Great! Your encrypted message is: {}".format(encrypted_bifid_message))
def crypter(self): """ encrypts or decrypts a string using the chosen cipher """ # if self.functionality is set to "1" if self.functionality == "1": # if self.cipher is set to "1" if self.cipher == "1": # use the affine cipher to encrypt self.message Affine(self.message).encrypt() # if self.cipher is set to "2" elif self.cipher == "2": # use the atbash cipher to encrypt self.message Atbash(self.message).encrypt() # if self.cipher is set to "3" elif self.cipher == "3": # use the keyword cipher to encrypt self.message Keyword(self.message).encrypt() # if self.functionality is set to "2" elif self.functionality == "2": # if self.cipher is set to "1" if self.cipher == "1": # use the affine cipher to decrypt self.message Affine(self.message).decrypt() # if cipher is set to "2" elif self.cipher == "2": # use the atbash cipher to decrypt self.message Atbash(self.message).decrypt() # if cipher is set to "3" elif self.cipher == "3": # use the keyword cipher to decrypt self.message Keyword(self.message).decrypt() # prompt the user to continue using the app or exit self.loop()
def keyword_decrypt(): """decrypts a message using the keyword cipher""" user_encrypted_message = input("What would you like to decrypt? ") user_decrypted_message = Keyword(user_encrypted_message).decrypted(user_encrypted_message) print("Your message reads: {}".format(user_decrypted_message)) another_one = input("If you would like to use another cipher? Type 'Y/N' Or type 'Start Over' to restart. Or 'q' to quit. ").lower() if another_one == 'y': my_cipher.another_cipher() elif another_one == 'start over': my_cipher.start_over() elif another_one == 'q': print("Great Thanks for using the keyword Cipher!")
def secret(): while True: cipher = '' print("\nBelow are the available ciphers. Please pick one.") ciphers = ['Affine', 'Atbash', 'Keyword'] for cipher in ciphers: print("-{}".format(cipher)) cipher_choice = input("\nWhich cipher would you like to use? ") clear_screen() if cipher_choice.lower() == 'affine': cipher = Affine() elif cipher_choice.lower() == 'atbash': cipher = Atbash() elif cipher_choice.lower() == 'keyword': keyword = input("What would you like your keyword to be? ") cipher = Keyword(keyword) elif cipher_choice.lower() == 'q': quit_program() else: print("I'm sorry. Please choose a cipher from the list.") secret() message = input("What would you like your message to be? ") choice = input("Would you like to encrypt or decrypt a message?") if choice.lower() == 'encrypt': cipher.encrypt(message) elif choice.lower() == 'decrypt': cipher.decrypt(message) elif choice.lower() == 'q': quit_program() elif choice.lower() != 'encrypt' or 'decrypt': print("Please choose either encryption or decryption.") secret()
def run_cipher(encrypt=True): """Sub menu with a list of implemented ciphers.""" clear() prompt = "Choose a cipher to use:\n\n" prompt += "1) (Af)fine\n" prompt += "2) (At)bash\n" prompt += "3) (C)aesar\n" prompt += "4) (K)eyword\n\n" prompt += "Type (q) to quit.\n" user_input = input(prompt) affine_input = [1, '1', 'af'] atbash_input = [2, '2', 'at'] caesar_input = [3, '4', 'c'] keyword_cipher_input = [4, '3', 'k'] valid_input = affine_input + atbash_input + keyword_cipher_input + caesar_input if user_input.lower() == "q": return "q" while user_input not in valid_input: user_input = str(input(prompt)) def ask_for_value(): val_input = input("Enter value:\n") if not encrypt: val_input = Cipher.remove_char_blocks(val_input) return val_input text = ask_for_value() while text.lower().replace(" ", "").isalpha() is False: print("Value must contain letters only.\n") text = ask_for_value() # Affine inputs if user_input in affine_input: aff_first_number = input("Please enter a beginning number for the Affine cipher (must be odd):\n") aff_second_number = input("Please enter an ending number for the Affine cipher:\n") while aff_first_number.isnumeric() is False \ or int(aff_first_number) % 2 == 0 \ or aff_second_number.isnumeric() is False: print("Value must contain numbers. First number must be odd.\n") aff_first_number = input("Please enter a beginning number for the Affine Cipher (must be odd):\n") aff_second_number = input("Please enter an ending number for the Affine cipher:\n") cipher = Affine(aff_first_number, aff_second_number) # Atbash inputs if user_input in atbash_input: cipher = Atbash() # Keyword inputs if user_input in keyword_cipher_input: user_keyword = input("Please enter your keyword for the Keyword Cipher:\n") while text.lower().isalpha() is False: print("Value must contain letters only.\n") user_keyword = input("Please enter keyword for the Keyword Cipher:\n") cipher = Keyword(user_keyword) if user_input in caesar_input: cipher = Caesar() if encrypt: text = cipher.encrypt(text) if input("Do you want to add a secret pad? (Y/n)\n").lower() == "y": text = pad_option(text, cipher) val = cipher.char_blocks(text) else: if input("Was a secret pad used? (Y/n)\n").lower() == "y": text = pad_option(text, cipher, encrypt=False) val = cipher.decrypt(text) return val
while message == '': if ((cipher == 'Keyword' or cipher == 'Polybius') and encrypt_decrypt == 'Decrypt'): print("If the message you wish to decrypt was formatted into" " blocks of\nfive you may omit the spaces and dashes when" " entering the message." ) message = input("Enter the message that you would like" " to {}: ".format(encrypt_decrypt.lower()) ) if message == '': print("I'm sorry, I didn't get that. ") # Implement cipher selection. if cipher == 'Keyword': if encrypt_decrypt == 'Encrypt': new_instance = Keyword(keyword, message) output = new_instance.encrypt() if pad_option != 'Not in use.': output = pad_encrypt(output) if block_option.upper() == 'Y': output = five_keyword(output) else: if pad_option != 'Not in use.': output = pad_decrypt(message) new_instance = Keyword(keyword, output) else: new_instance = Keyword(keyword, message) output = new_instance.decrypt() if cipher == 'Polybius': if encrypt_decrypt == 'Encrypt': if pad_option != 'Not in use.':
def encrypt_decrypt(): """Encrypt or decrypt text.""" playing = True while playing: #User input cipher = int( input("Enter the number associated with cipher you wish to use: ")) text = input("What is the message?: ") choice = input( "Are you going to encrypt or decrypt? Enter E or D: ").lower() print("\n") if choice == "e": #Generates affine cipher if cipher == 1: affine = Affine() print(affine.encrypt(text)) playing = False #Generates atbash cipher elif cipher == 2: atbash = Atbash() print(atbash.encrypt(text)) playing = False #Generates keyword cipher elif cipher == 3: #Ask user keyword secret_key = input("Enter your keyword: ") keyword = Keyword() print("\n") print(keyword.encrypt(text, secret_key)) playing = False if choice == "d": #Decrypts affine cipher if cipher == 1: affine = Affine() print(affine.decrypt(text)) playing = False #Decrypts atbash cipher elif cipher == 2: atbash = Atbash() print(atbash.decrypt(text)) playing = False #Decrypts keyword cipher elif cipher == 3: secret_key = input("Enter your keyword: ") keyword = Keyword() print("\n") print(keyword.decrypt(text, secret_key)) playing = False #Asks user to play again or not else: if input("\nDo you want to contine? Y/N: ").lower() == "y": welcome() encrypt_decrypt() else: print("See you next time!")
def run_cipher(encrypt=True): """Sub menu with a list of implemented ciphers.""" global key_val clear() prompt = "Choose a cipher to use:\n\n" prompt += "1) (Af)fine\n" prompt += "2) (At)bash\n" prompt += "3) (K)eyword\n\n" prompt += "Type (q) to quit.\n" user_input = input(prompt) affine_input = [1, '1', 'af'] atbash_input = [2, '2', 'at'] keyword_cipher_input = [3, '3', 'k'] valid_input = affine_input + atbash_input + keyword_cipher_input if user_input.lower() == "q": return "q" while user_input not in valid_input: user_input = str(input(prompt)) def ask_for_message(): val_input = input("Enter message:\n") return val_input text = ask_for_message() while text.lower().isalpha() is False: print("Message must contain letters only.\n") text = ask_for_message() # Affine inputs if user_input in affine_input: aff_first_number = input("Please enter a beginning number for the Affine cipher (must be odd):\n") aff_second_number = input("Please enter an ending number for the Affine cipher:\n") while aff_first_number.isnumeric() is False \ or int(aff_first_number) % 2 == 0 \ or aff_second_number.isnumeric() is False: print("Value must contain numbers. First number must be odd.\n") aff_first_number = input("Please enter a beginning number for the Affine Cipher (must be odd):\n") aff_second_number = input("Please enter an ending number for the Affine cipher:\n") cipher = Affine(aff_first_number, aff_second_number) # Atbash inputs if user_input in atbash_input: cipher = Atbash() # Keyword inputs if user_input in keyword_cipher_input: user_keyword = input("Please enter your keyword for the Keyword Cipher:\n") while text.lower().isalpha() is False: print("Message must contain letters only.\n") user_keyword = input("Please enter keyword for the Keyword Cipher:\n") cipher = Keyword(user_keyword) if encrypt: key_val = cipher.encrypt(text) else: key_val = cipher.decrypt(text) return key_val
def welcome(): """Enables the user to select a cipher and either encrypt or decrypt a message. Once complete the encrypted/decrypted message will be returned, and the user will be prompted to restart or quit. """ clear() cipher_choice = None encrypt_or_decrypt = None cipher_dict = { '1': 'Caesar', '2': 'Keyword', '3': 'Affine', '4': 'Transposition' } print("""Welcome to the Secret Messages project for treehouse techdegree. Select a cipher from the list below or press 'q' to quit:\n 1 - Caesar 2 - Keyword 3 - Affine 4 - Transposition""") # Select cipher type. while cipher_choice is None: cipher_choice = input("Enter 1, 2, 3, or 4 to begin.\n>> ").lower() if cipher_choice not in ['1', '2', '3', '4']: if cipher_choice == 'q': print("Good Bye.") sys.exit() else: print("Invalid selection, Try again.") cipher_choice = None print("You have selected {}.".format(cipher_dict[cipher_choice])) # Select Encryption or decryption. while encrypt_or_decrypt is None: encrypt_or_decrypt = input("Would you like to encrypt or decrypt?\n>>\ ").lower() if encrypt_or_decrypt not in ['encrypt', 'decrypt']: if encrypt_or_decrypt == 'q': print("Good Bye.") sys.exit() else: print("Invalid selection, Try again.") encrypt_or_decrypt = None # Prints Caesar encryption/decrytion. clear() if cipher_choice == '1': if encrypt_or_decrypt == 'encrypt': print("The {}ed message is: {}".format(encrypt_or_decrypt, Caesar().encrypt())) else: print("The {}ed message is: {}".format(encrypt_or_decrypt, Caesar().decrypt())) # Prints Keyword encryption/decrytion. if cipher_choice == '2': if encrypt_or_decrypt == 'encrypt': print("The {}ed message is: {}".format(encrypt_or_decrypt, Keyword().encrypt())) else: print("The {}ed message is: {}".format(encrypt_or_decrypt, Keyword().decrypt())) # Prints Affine encryption/decrytion. if cipher_choice == '3': if encrypt_or_decrypt == 'encrypt': print("The {}ed message is: {}".format(encrypt_or_decrypt, Affine().encrypt())) else: print("The {}ed message is: {}".format(encrypt_or_decrypt, Affine().decrypt())) # Prints Transposition encryption/decrytion. if cipher_choice == '4': if encrypt_or_decrypt == 'encrypt': print("The {}ed message is: {}".format(encrypt_or_decrypt, Transposition().encrypt())) else: print("The {}ed message is: {}".format(encrypt_or_decrypt, Transposition().decrypt())) # Ask user to restart or quit. restart = input("Would you like to encrypt/decrypt another message? Y/n\n\ >> ").upper() if restart == 'Y': welcome() else: print("Good Bye.") sys.exit()