Exemplo n.º 1
0
 def _get_keywords(self, args):
     arg_names = self.arguments.names
     if 'name' in arg_names:
         name_index = arg_names.index('name')
         return [Keyword(args[name_index], args[name_index + 1:])]
     elif self.arguments.varargs == 'names':
         return [Keyword(name, []) for name in args[len(arg_names):]]
     return []
Exemplo n.º 2
0
    def process_links(cls, link):
        """ checks if present in list or/and updates pagerank"""

        new_link = Link(link)

        if new_link.link not in cls.links_dict:
            cls.links_dict[new_link.link] = new_link
            new_link.keywords()
            return True
        else:
            cls.links_dict[new_link.link].update_pagerank()
            Keyword.update(link)
            return False
    def checkMatch(self, query):
        keyword = Keyword()

        if len(query.split()) == 0:
            return True

        key = query.split()[0].upper()
        queryTemplate = keyword.getKeyword(key)
        queryTemplate.parse(query)

        if key not in self.template:
            print("OOPS key not found")
            return False
        print("result ", queryTemplate == self.template[key])
        return queryTemplate == self.template[key]
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!")
Exemplo n.º 5
0
    def keywords(self):
        """ returns keywords from page """

        url = self.link
        rep = r.get(url)

        text = ""

        if rep.status_code == 200:
            soup = bs(rep.text, 'html.parser')
            text = soup.get_text()

        word_set = stemming(text)

        for word in word_set:

            Keyword.add_keyword(word, self)
Exemplo n.º 6
0
 def replace_variables(self, variables, errors):
     if self.name:
         try:
             self.name = variables.replace_string(self.name)
         except DataError, err:
             errors.append('Replacing variables from %s failed: %s' %
                           (self.__class__.__name__, unicode(err)))
         self._keyword = Keyword(self.name,
                                 self.args,
                                 type=type(self).__name__.lower())
Exemplo n.º 7
0
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)
Exemplo n.º 8
0
def test():
    client = MongoClient('127.0.0.1', 27017)
    # stub
    from keywords import Keyword
    keyword_db = Keyword(client)
    keyword_db.insert("ニコニコ大百科")
    object_id = keyword_db.find_object("ニコニコ大百科")

    content_db = Content(client)
    content_db.insert(object_id, ["概要"], {"type": None, "content": ["ここは概要"]})
    content_db.insert(object_id, ["概要", "プロフィール"], {
        "type": None,
        "content": ["ここは概要の中のプロフィール"]
    })
    content_db.insert(object_id, ["プロフィール"], {
                      "type": None, "content": ["ここはプロフィール"]})
    content_db.insert(object_id, ["関連項目"], {
                      "type": None, "content": ["ここは関連項目"]})

    print('search: ', ["概要"])
    pprint(content_db.find_objects(object_id, ["概要"]))

    print('search: ', ["プロフィール"])
    pprint(content_db.find_objects(object_id, ["プロフィール"]))

    print('search: ', ["概要", "プロフィール"])
    pprint(content_db.find_objects(object_id, ["概要", "プロフィール"]))

    print('search: ', ["関連項目"])
    pprint(content_db.find_objects(object_id, ["関連項目"]))

    print('search: ', ["関連商品"])
    pprint(content_db.find_objects(object_id, ["関連商品"]))

    print('show all')
    pprint(content_db.all)
    keyword_db.reset()
    content_db.reset()
Exemplo n.º 9
0
 def _get_default_run_kw_keywords(self, given_args):
     index = self.arguments.names.index('name')
     return [Keyword(given_args[index], given_args[index + 1:])]
Exemplo n.º 10
0
 def _get_run_kws_keywords(self, given_args):
     return [Keyword(name, []) for name in given_args]
Exemplo n.º 11
0
 def _get_run_kw_if_keywords(self, given_args):
     for kw_call in self._get_run_kw_if_calls(given_args):
         if kw_call:
             yield Keyword(kw_call[0], kw_call[1:])
Exemplo n.º 12
0
 def main_menu(self):
     # Method to display the main menu.
     self.clear_scrn()
     print("______Main Menu______\n\n"
           "Please choose a Cipher:\n"
           "1) Keyword\n"
           "2) Affine\n"
           "3) Atbash\n")
     menu_choice = self.get_user_input(
         "Please enter a number 1-3 or a Cipher name:\n>>")
     if menu_choice == '1' or menu_choice.upper() == 'KEYWORD':
         from keywords import Keyword
         keyword = Keyword()
         self.clear_scrn()
         print("___Keyword Menu:___\n"
               "Please choose an option:\n"
               "1) ENCRYPT a message\n"
               "2) ENCRYPT a message using a PAD\n"
               "3) DECRYPT a message\n"
               "4) DECRYPT a message using a PAD\n")
         keyword_choice = self.get_user_input(
             "Please enter a number 1-4 or 'Encrypt', 'EncryptPAD', 'Decrypt' or 'DecryptPAD': \n>>"
         )
         if keyword_choice == '1' or keyword_choice.upper() == 'ENCRYPT':
             keyword_messgae = keyword.get_message()
             keyword_keyword = keyword.get_keyword()
             keyword_encrypted_message = keyword.encrypt_message(
                 keyword_messgae, keyword_keyword)
             print("Your encrypted message is:\n{}.\n".format(
                 keyword_encrypted_message))
             keyword_block = input(
                 "Do you want to display your message in 5 letter block format? (Y/N)"
             )
             if keyword_block.upper() == 'Y':
                 self.five_block(keyword_encrypted_message)
             nothing = input("\n\nHit enter to return to the Main Menu.")
             self.main_menu()
         elif keyword_choice == '2' or keyword_choice.upper(
         ) == 'ENCRYPTPAD':
             keyword_messgae = keyword.get_message()
             keyword_keyword = keyword.get_keyword()
             keyword_encrypted_message = keyword.encrypt_message(
                 keyword_messgae, keyword_keyword)
             keyword_pad = keyword.get_pad(keyword_encrypted_message)
             keyword_pad_message = keyword.encrypt_pad(
                 keyword_encrypted_message, keyword_pad)
             print("Your encrypted message with a PAD is:\n{}".format(
                 keyword_pad_message))
             keyword_block = input(
                 "Do you want to display your message in 5 letter block format? (Y/N)"
             )
             if keyword_block.upper() == 'Y':
                 self.five_block(keyword_pad_message)
             nothing = input("\n\nHit enter to return to the Main Menu.")
             self.main_menu()
         elif keyword_choice == '3' or keyword_choice.upper() == 'DECRYPT':
             keyword_encrypted_message = keyword.get_encrypted_message()
             keyword_keyword = keyword.get_keyword()
             keyword_decrypted_message = keyword.decrypt_message(
                 keyword_encrypted_message, keyword_keyword)
             print("Your Decrypted message is:\n{}.\n".format(
                 keyword_decrypted_message))
             nothing = input("\n\nHit enter to return to the Main Menu.")
             self.main_menu()
         elif keyword_choice == '4' or keyword_choice.upper(
         ) == 'DECRYPTPAD':
             keyword_encrypted_message_pad = keyword.get_encrypted_message()
             keyword_keyword = keyword.get_keyword()
             keyword_pad = keyword.get_pad(keyword_encrypted_message_pad)
             keyword_encrypted_message = keyword.decrypt_pad(
                 keyword_encrypted_message_pad, keyword_pad)
             keyword_decrypted_message = keyword.decrypt_message(
                 keyword_encrypted_message, keyword_keyword)
             print("Your Decrypted message is:\n{}.\n".format(
                 keyword_decrypted_message))
             nothing = input("\n\nHit enter to return to the Main Menu.")
             self.main_menu()
         else:
             nothing = input(
                 "Not a valid entry. Returning to Main Menu. Hit enter to continue."
             )
             self.main_menu()
     elif menu_choice == '2' or menu_choice.upper() == 'AFFINE':
         from affines import Affine
         affine = Affine()
         self.clear_scrn()
         print("___Affine Menu:___\n"
               "Please choose an option:\n"
               "1) ENCRYPT a message\n"
               "2) ENCRYPT a message using a PAD\n"
               "3) DECRYPT a message\n"
               "4) DECRYPT a message using a PAD\n")
         affine_choice = self.get_user_input(
             "Please enter a nubmer 1-4 or 'Encrypt', 'EncryptPAD', 'Decrypt' or 'DecryptPAD': \n>>"
         )
         if affine_choice == '1' or affine_choice.upper() == 'ENCRYPT':
             affine_message = affine.get_message()
             affine_keycode = affine.get_key_code()
             affine_encrypted_message = affine.encrypt_message(
                 affine_message, affine_keycode)
             print("Your encyrpted message is:\n{}".format(
                 affine_encrypted_message))
             keyword_block = input(
                 "Do you want to display your message in 5 letter block format? (Y/N)"
             )
             if keyword_block.upper() == 'Y':
                 self.five_block(affine_encrypted_message)
             nothing = input("\n\nHit enter to return to the Main Menu.")
             self.main_menu()
         elif affine_choice == '2' or affine_choice.upper() == 'ENCRYPTPAD':
             affine_message = affine.get_message()
             affine_keycode = affine.get_key_code()
             affine_encrypted_message = affine.encrypt_message(
                 affine_message, affine_keycode)
             affine_pad = affine.get_pad(affine_encrypted_message)
             affine_pad_message = affine.encrypt_pad(
                 affine_encrypted_message, affine_pad)
             print("Your encrypted message with a PAD is:\n{}".format(
                 affine_pad_message))
             keyword_block = input(
                 "Do you want to display your message in 5 letter block format? (Y/N)"
             )
             if keyword_block.upper() == 'Y':
                 self.five_block(affine_pad_message)
             nothing = input("\n\nHit enter to return to the Main Menu.")
             self.main_menu()
         elif affine_choice == '3' or affine_choice.upper() == 'DECRYPT':
             affine_encrypted_message = affine.get_encrypted_message()
             affine_keycode = affine.get_key_code()
             affine_decrypted_message = affine.decrypt_message(
                 affine_encrypted_message, affine_keycode)
             print("Your decrypted message is:\n{}.".format(
                 affine_decrypted_message))
             nothing = input("\n\nHit enter to return to the Main Menu.")
             self.main_menu()
         elif affine_choice == '4' or affine_choice.upper() == 'DECRYPTPAD':
             affine_encrypted_message_pad = affine.get_encrypted_message()
             affine_keycode = affine.get_key_code()
             affine_pad = affine.get_pad(affine_encrypted_message_pad)
             affine_encrypted_message = affine.decrypt_pad(
                 affine_encrypted_message_pad, affine_pad)
             affine_decrypted_message = affine.decrypt_message(
                 affine_encrypted_message, affine_keycode)
             print("Your decrypted message is:\n{}.".format(
                 affine_decrypted_message))
             nothing = input("\n\nHit enter to return to the Main Menu.")
             self.main_menu()
         else:
             nothing = input(
                 "Not a valid entry. Returning to Main Menu. Hit enter to continue."
             )
             self.main_menu()
     elif menu_choice == '3' or menu_choice.upper() == 'ATBASH':
         from atbashs import Atbash
         atbash = Atbash()
         self.clear_scrn()
         print("___Atbash Menu:___\n"
               "Please choose an option:\n"
               "1) ENCRYPT a message\n"
               "2) ENCRYPT a message using a PAD\n"
               "3) DECRYPT a message\n"
               "4) DECRYPT a message using a PAD\n")
         atbash_choice = self.get_user_input(
             "Please enter a number 1-4 or 'Encrypt', 'EncryptPAD', 'Decrypt' or 'DecryptPAD': \n>>"
         )
         if atbash_choice == '1' or atbash_choice.upper() == 'ENCRYPT':
             atbash_message = atbash.get_message()
             atbash_encrypted_message = atbash.encrypt_message(
                 atbash_message)
             print("Your encrypted message is:\n{}.\n".format(
                 atbash_encrypted_message))
             keyword_block = input(
                 "Do you want to display your message in 5 letter block format? (Y/N)"
             )
             if keyword_block.upper() == 'Y':
                 self.five_block(atbash_encrypted_message)
             nothing = input("\n\nHit enter to return to the Main Menu.")
             self.main_menu()
         elif atbash_choice == '2' or atbash_choice.upper() == 'ENCRYPTPAD':
             atbash_message = atbash.get_message()
             atbash_encrypted_message = atbash.encrypt_message(
                 atbash_message)
             atbash_pad = atbash.get_pad(atbash_encrypted_message)
             atbash_pad_message = atbash.encrypt_pad(
                 atbash_encrypted_message, atbash_pad)
             print("Your encrypted message is:\n{}.\n".format(
                 atbash_pad_message))
             keyword_block = input(
                 "Do you want to display your message in 5 letter block format? (Y/N)"
             )
             if keyword_block.upper() == 'Y':
                 self.five_block(atbash_pad_message)
             nothing = input("\n\nHit enter to return to the Main Menu.")
             self.main_menu()
         elif atbash_choice == '3' or atbash_choice.upper() == 'DECYPT':
             atbash_encrypted_message = atbash.get_encrypted_message()
             atbash_decrypted_message = atbash.decrypt_message(
                 atbash_encrypted_message)
             print("Your decrypted message is:\n{}.\n".format(
                 atbash_decrypted_message))
             nothing = input("\n\nHit enter to return to the Main Menu.")
             self.main_menu()
         elif atbash_choice == '4' or atbash_choice.upper() == 'DECYPTPAD':
             atbash_encrypted_message_pad = atbash.get_encrypted_message()
             atbash_pad = atbash.get_pad(atbash_encrypted_message_pad)
             atbash_encrypted_message = atbash.decrypt_pad(
                 atbash_encrypted_message_pad, atbash_pad)
             atbash_decrypted_message = atbash.decrypt_message(
                 atbash_encrypted_message)
             print("Your decrypted message is:\n{}.\n".format(
                 atbash_decrypted_message))
             nothing = input("\n\nHit enter to return to the Main Menu.")
             self.main_menu()
         else:
             nothing = input(
                 "Not a valid entry. Returning to Main Menu. Hit enter to continue."
             )
             self.main_menu()
     else:
         nothing = input(
             "Not a valid entry. Returning to Main Menu. Hit enter to continue."
         )
         self.main_menu()
Exemplo n.º 13
0
def __keyword_cipher(encrypt_or_decrypt, working_text):
    if encrypt_or_decrypt == "encryption":
        __clear()

        # Define your own keyword?
        keyword_decision = input(
            "Would you like to define your own keyword? [y/n] ").upper()

        if keyword_decision == "Y":
            __clear()
            # Grab user's word
            usr_keyword = input(
                "What would you like your keyword to be? \nBe sure your keyword does not contain repeated letters. "
            )
            if len(usr_keyword) == len(set(usr_keyword)):
                keyword_encryption = Keyword(working_text, usr_keyword)

            else:
                # user had a repeated letter
                __clear()
                input(
                    'Keyword argument contained repeated letters. \nPlease Try Again.'
                )
                __keyword_cipher(encrypt_or_decrypt, working_text)

        # No user defined keyword
        elif keyword_decision == "N":
            keyword_encryption = Keyword(working_text)

        else:
            # Whoops user mistyped
            __clear()
            input(
                'The option specified by the user was not recognized. \nPlease try again'
            )
            __keyword_cipher(encrypt_or_decrypt, working_text)

        return keyword_encryption.encrypt()

    elif encrypt_or_decrypt == "decryption":
        __clear()

        # Did user define keyword?
        keyword_decision = input("Did you define a keyword? [y/n] ").upper()

        if keyword_decision == "Y":
            __clear()
            # Grab user keyword
            usr_keyword = input("What was your keyword? ")
            __clear()
            keyword_decryption = Keyword(working_text, usr_keyword)

        # No user defined keyword
        elif keyword_decision == "N":
            __clear()
            keyword_decryption = Keyword(working_text)

        else:
            # Whoops user mistyped
            __clear()
            input(
                'The option specified by the user was not recognized. \nPlease try again'
            )
            __keyword_cipher(encrypt_or_decrypt, working_text)

        return keyword_decryption.decrypt()
Exemplo n.º 14
0
def play():
    """
    Asks the user what cipher he wants to use,
    in order to encrypt or decrypt a text message.
    Gives user the option to return the cipher text in 5 characters blocks.
    Asks user for different input based on cipher selection.
    Adds a onetime pad as an additional security layer.
    If encrypting:
    Returns cipher text encrypted with the chosen cipher and onetime pad.
    If decrypting:
    Returns text decrypted with the chosen cipher and onetime pad.
    """
    working = True
    cipher_choice = True
    enc_dec = True
    letters = string.ascii_uppercase
    while working:
        clear_screen()
        print(
            "This is the Secret Messages project for the Treehouse Techdegree. \n"
            "These are the current available ciphers: \n"
            "- Affine \n"
            "- Atbash \n"
            "- Caesar \n"
            "- Keyword \n"
            "- Type (Q) to quit. \n")
        while cipher_choice:
            choice = input(
                "Type the name of the cipher would you like to use? \n")
            if choice.upper() == 'Q':
                exit()
            elif choice.upper() == 'AFFINE':
                cipher = Affine()
                break
            elif choice.upper() == 'ATBASH':
                cipher = Atbash()
                break
            elif choice.upper() == 'CAESAR':
                cipher = Caesar()
                break
            elif choice.upper() == 'KEYWORD':
                cipher = Keyword()
                break
            else:
                print('Type the name of any available cipher. \n')

        user_text = input('What is your message?(Letters only) \n')

        while enc_dec:
            e_or_d = input('Are we going to encrypt or decrypt? \n')
            if e_or_d.upper() == 'ENCRYPT' and isinstance(cipher, Affine):
                alpha, beta = get_keys()
                ot_pad = input('Type your one time pad. \n')
                ot_val = cipher.one_time_pad(user_text, ot_pad)
                value = cipher.encrypt(ot_val, alpha, beta)
                block_choice = yes_or_no()
                if block_choice.upper() == 'Y':
                    value = cipher.add_padding(value)
                    print(value + '\n')
                    repeat()
                    break
                else:
                    print(value + '\n')
                    repeat()
                    break
            elif e_or_d.upper() == 'DECRYPT' and isinstance(cipher, Affine):
                alpha, beta = get_keys()
                ot_pad = input(
                    'Type your one time pad, must be the same used for encrypting. \n'
                )
                block_choice = y_o_n()
                if block_choice.upper() == 'Y':
                    no_block = cipher.remove_padding(user_text)
                    value = cipher.decrypt(no_block, alpha, beta)
                    ot_val = cipher.one_time_pad(value, ot_pad, encrypt=False)
                    print(ot_val + '\n')
                    repeat()
                    break
                else:
                    value = cipher.decrypt(user_text, alpha, beta)
                    ot_val = cipher.one_time_pad(value, ot_pad, encrypt=False)
                    print(ot_val + '\n')
                    repeat()
                    break
            elif e_or_d.upper() == 'ENCRYPT' and isinstance(cipher, Atbash):
                ot_pad = input('Type your one time pad. \n')
                ot_val = cipher.one_time_pad(user_text, ot_pad)
                value = cipher.encrypt(ot_val)
                block_choice = yes_or_no()
                if block_choice.upper() == 'Y':
                    value = cipher.add_padding(value)
                    print(value + '\n')
                    repeat()
                    break
                else:
                    print(value + '\n')
                    repeat()
                    break
            elif e_or_d.upper() == 'DECRYPT' and isinstance(cipher, Atbash):
                ot_pad = input(
                    'Type your one time pad, must be the same used for encrypting. \n'
                )
                block_choice = y_o_n()
                if block_choice.upper() == 'Y':
                    no_block = cipher.remove_padding(user_text)
                    value = cipher.decrypt(no_block)
                    ot_val = cipher.one_time_pad(value, ot_pad, encrypt=False)
                    print(ot_val + '\n')
                    repeat()
                    break
                else:
                    value = cipher.decrypt(user_text)
                    ot_val = cipher.one_time_pad(value, ot_pad, encrypt=False)
                    print(ot_val + '\n')
                    repeat()
                    break
            elif e_or_d.upper() == 'ENCRYPT' and isinstance(cipher, Caesar):
                ot_pad = input('Type your one time pad. \n')
                ot_val = cipher.one_time_pad(user_text, ot_pad)
                value = cipher.encrypt(ot_val)
                block_choice = yes_or_no()
                if block_choice.upper() == 'Y':
                    value = cipher.add_padding(value)
                    print(value + '\n')
                    repeat()
                    break
                else:
                    print(value + '\n')
                    repeat()
                    break
            elif e_or_d.upper() == 'DECRYPT' and isinstance(cipher, Caesar):
                ot_pad = input(
                    'Type your one time pad, must be the same used for encrypting. \n'
                )
                block_choice = y_o_n()
                if block_choice.upper() == 'Y':
                    no_block = cipher.remove_padding(user_text)
                    value = cipher.decrypt(no_block)
                    ot_val = cipher.one_time_pad(value, ot_pad, encrypt=False)
                    print(ot_val + '\n')
                    repeat()
                    break
                else:
                    value = cipher.decrypt(user_text)
                    ot_val = cipher.one_time_pad(value, ot_pad, encrypt=False)
                    print(ot_val + '\n')
                    repeat()
                    break
            elif e_or_d.upper() == 'ENCRYPT' and isinstance(cipher, Keyword):
                ot_pad = input('Type your one time pad. \n')
                keyword_choice = get_keyword()
                ot_val = cipher.one_time_pad(user_text, ot_pad)
                value = cipher.encrypt(ot_val, keyword_choice)
                block_choice = yes_or_no()
                if block_choice.upper() == 'Y':
                    value = cipher.add_padding(value)
                    print(value + '\n')
                    repeat()
                    break
                else:
                    print(value + '\n')
                    repeat()
                    break
            elif e_or_d.upper() == 'DECRYPT' and isinstance(cipher, Keyword):
                ot_pad = input(
                    'Type your one time pad, must be the same used for encrypting. \n'
                )
                block_choice = y_o_n()
                keyword_choice = get_keyword()
                if block_choice.upper() == 'Y':
                    no_block = cipher.remove_padding(user_text)
                    value = cipher.decrypt(no_block, keyword_choice)
                    ot_val = cipher.one_time_pad(value, ot_pad, encrypt=False)
                    print(ot_val + '\n')
                    repeat()
                    break
                else:
                    value = cipher.decrypt(user_text, keyword_choice)
                    ot_val = cipher.one_time_pad(value, ot_pad, encrypt=False)
                    print(ot_val + '\n')
                    repeat()
                    break