def get_messages(): cipher = "" print("This is the Secret Messages Project for the Treehouse Techdegree.") user_prompt = "These are the current available ciphers:\n" user_prompt += "-(At)bash-\n" user_prompt += "-(K)eyword-\n" user_prompt += "-(P)olybius Square-\n" user_prompt += "-(Q)uit\n" while cipher not in ['at', 'k', 'p', 'q']: print(user_prompt) cipher = input("Which cipher would you like to use?\n>>>").lower() clear_screen() if cipher == 'at': cipher = Atbash() elif cipher == 'k': cipher = KeywordCipher() elif cipher == 'p': cipher = PolybiusSquare() elif cipher == 'q': break else: print( "Sorry, I didn't get that. Please choose from the list again." ) print(user_prompt) message = input("What is your message?").lower() choice = input( "Would you like to (e)ncrypt or (d)ecrypt the message?").lower() if choice == 'e': cipher.encrypt(message) else: cipher.decrypt(message)
def __atbash_cipher(encrypt_or_decrypt, working_text): # Run Atbash encryption if encrypt_or_decrypt == "encryption": atbash_encryption = Atbash() __clear() return atbash_encryption.encrypt(working_text) # Run Atbash decryption elif encrypt_or_decrypt == "decryption": atbash_decryption = Atbash() __clear() return atbash_decryption.encrypt(working_text)
def run_atbash_cipher(): """this method prompts the user if they would like to encrypt or decrypt and runs the encrypt or decrypt method of this cipher """ atbash = Atbash() while True: encrypt_decrypt = input("\nEnter 1 to encrypt. Enter 2 to decrypt: ") if encrypt_decrypt == '1': atbash.encrypt() break elif encrypt_decrypt == '2': atbash.decrypt() break else: print("\nTry again! That is not a valid choice!")
def app_menu(): """Application menu""" enc_dec_choice = input(encrypt_decrypt_prompt).upper() if enc_dec_choice != encrypt and enc_dec_choice != decrypt: sys.exit() algorithm_choice = input(cipher_prompt).upper() if algorithm_choice == cipher_keyword: keyword_obj = KeywordCipher(input("PROVIDE KEYWORD\n")) if enc_dec_choice == encrypt: encryption_res = keyword_obj.encrypt(input(user_input_encrypt__prompt)) else: decryption_result = keyword_obj.decrypt(input(user_input_decrypt__prompt)) elif algorithm_choice == cipher_atbash: atbash_obj = Atbash() if enc_dec_choice == encrypt: encryption_res = atbash_obj.encrypt(input(user_input_encrypt__prompt)) else: decryption_result = atbash_obj.decrypt(input(user_input_decrypt__prompt)) elif algorithm_choice == cipher_affine: affine_obj = Affine() print("\nValid values for a factor: {0}".format(a_possible_values)) a = check_a_factor(int(input(a_factor_prompt))) b = int(input(b_factor_prompt)) if enc_dec_choice == encrypt: encryption_res = affine_obj.encrypt(input(user_input_encrypt__prompt), a, b) else: decryption_result = affine_obj.decrypt(input(user_input_decrypt__prompt), a, b) else: sys.exit() (print("ENCRYPTED STRING: {0}".format(encryption_res)) if enc_dec_choice == encrypt else print("DECRYPTED STRING: {0}".format(decryption_result)))
def main(): print() print("This is the Secret Messages project for Treehouse \n") cipher_text = None print("These are the current available ciphers:\n") ciphers = ["Caesar", "Atbash", "Keyword", "Affine"] for cipher in ciphers: print("- {}".format(cipher)) while True: answer = input("Which cipher would you like to use? ") print() if answer.lower() == "atbash": cipher_text = Atbash() break elif answer.lower() == "keyword": keyword = input("what key word would you like to use ") if keyword is None or keyword == "": cipher_text = KeyWords() break else: cipher_text = KeyWords(keyword) break elif answer.lower() == "affine": cipher_text = Affine() break elif answer.lower() == "caesar": key_value = input("Shift Key Value:") if isinstance(int(key_value), int): cipher_text = Caesar(int(key_value)) break else: cipher_text = Caesar() break else: print("not a valid cipher") message = input("{} Cipher,type in your message ".format(answer)) while True: what_to_do = input("Do you want to encrypt ot decrypt? ") if what_to_do == "encrypt": encrypted_message = cipher_text.encrypt(message) break elif what_to_do == "decrypt": encrypted_message = cipher_text.decrypt(message) break else: print("not valid command") print("Your {}ed message is {} ".format(what_to_do, encrypted_message)) regame = input("Do you want to encrypt/decrypt again y/n ") clear() if regame.lower() == "y": main() else: print("Good bye hope you had fun {}ing ".format(what_to_do))
def test_atbash_encrypt(): plaintext = ciphertexts['plaintext'] desired_ciphertext = ciphertexts['Atbash'] cipher = Atbash() encrypted_text = cipher.encrypt(plaintext) assert encrypted_text == desired_ciphertext
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 test_atbash_encryption(self): return self.assertEqual(Atbash.encrypt("Encryption with Atbash"), "vMXIBKGRLM DRGS zGYZHS")
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(): print( "This is the Secret Message project for the Treehouse Techdegree. \n" ) user_choice = input( "Would you like to encrypt or decrypt a message? \n \n") if user_choice.lower() == "encrypt": text = input("What message would you like to encrypt? \n \n") cipher_type = input( "Which cipher would you like to use to encrypt? \n" "\n - Keyword" "\n - Atbash" "\n - Affine" "\n - Caesar \n" "\n") #Keyword Cipher if cipher_type.lower() == "keyword": secret_keyword = input( "Great Choice! What is your keyword? \n \n") keyword = Keyword() print('\n') print(keyword.encrypt(text, secret_keyword)) #Atbash Cipher elif cipher_type.lower() == "atbash": atbash = Atbash() print('\n') print(atbash.encrypt(text)) #Affine Cipher elif cipher_type.lower() == "affine": affine = Affine() print('\n') print(affine.encrypt(text)) #Caesar Cipher elif cipher_type.lower() == "caesar": caesar = Caesar() print('\n') print(caesar.encrypt(text)) elif ValueError: print(" \nSorry, {} is not a choice!".format(cipher_type)) run_cipher() elif user_choice.lower() == "decrypt": text = input("What message would you like to decrypt? \n \n") cipher_type = input( "Which cipher would you like to use to decrypt? \n" "\n - Keyword" "\n - Atbash" "\n - Affine" "\n - Caesar \n" "\n") #Keyword Cipher if cipher_type.lower() == "keyword": secret_keyword = input( "Great Choice! What is your keyword? \n \n") keyword = Keyword() print('\n') print(keyword.decrypt(text, secret_keyword)) #Atbash Cipher elif cipher_type.lower() == "atbash": atbash = Atbash() print(atbash.decrypt(text)) #Affine Cipher elif cipher_type.lower() == "affine": affine = Affine() print('\n') print(affine.decrypt(text)) #Caesar Cipher elif cipher_type.lower() == "caesar": caesar = Caesar() print('\n') print(caesar.decrypt(text)) #If the cipher type is not a choice elif ValueError: print(" \nSorry, {} is not a choice!".format(cipher_type)) run_cipher() elif ValueError: print("{} wasn't an option. \n".format(user_choice)) run_cipher()