예제 #1
0
def brute_force(ciphered_text: str,
                charset: str = DEFAULT_CHARSET,
                _database_path: Optional[str] = None,
                _testing=False) -> str:
    """ Get Vigenere ciphered text key.
    Uses a brute force technique trying the entire dictionary space until finding a text
    that can be identified with any of our languages.
    **You should not use this function. Use *brute_force_mp* instead.** This
    function is slower than *mp* one because is sequential while the other uses a
    multiprocessing approach. This function only stay here to allow comparisons
    between sequential and multiprocessing approaches.
    :param ciphered_text: Text to be deciphered.
    :param charset: Charset used for Vigenere method substitution. Both ends, ciphering
        and deciphering, should use the same charset or original text won't be properly
        recovered.
    :param _database_path: Absolute pathname to database file. Usually you don't
        set this parameter, but it is useful for tests.
    :param _testing: Vigenere takes to long time to be tested against real dictionaries. So,
        to keep tests short if _testing is set to True a mocked key generator is used so only
        a controlled handful of words are tested to find a valid key. In simple terms: don't
        mess with this parameter and keep it to False, it is only used for testing.
    :return: Most probable Vigenere key found.
    """
    key_generator_function = dictionary_word_key_generator(
        _database_path
    ) if not _testing else mocked_dictionary_word_key_generator()
    return simple_brute_force(
        key_generator=key_generator_function,
        assess_function=_assess_vigenere_key,
        # key_space_length=key_space_length,
        ciphered_text=ciphered_text,
        charset=charset,
        _database_path=_database_path)
예제 #2
0
def brute_force(ciphered_text: str,
                charset: str = DEFAULT_CHARSET,
                _database_path: Optional[str] = None) -> int:
    """ Get Caesar ciphered text key.

    Uses a brute force technique trying the entire key space until finding a text
    that can be identified with any of our languages.

    **You should not use this function. Use *brute_force_mp* instead.** This
    function is slower than *mp* one because is sequential while the other uses a
    multiprocessing approach. This function only stay here to allow comparisons
    between sequential and multiprocessing approaches.

    :param ciphered_text: Text to be deciphered.
    :param charset: Charset used for Caesar method substitution. Both ends, ciphering
     and deciphering, should use the same charset or original text won't be properly
     recovered.
    :param _database_path: Absolute pathname to database file. Usually you don't
     set this parameter, but it is useful for tests.
    :return: Caesar key found.
    """
    key_space_length = len(charset)
    return simple_brute_force(
        key_generator=integer_key_generator(key_space_length),
        assess_function=_assess_caesar_key,
        # key_space_length=key_space_length,
        ciphered_text=ciphered_text,
        charset=charset,
        _database_path=_database_path)
예제 #3
0
def brute_force(ciphered_text: str,
                _database_path: Optional[str] = None) -> int:
    """ Get Transposition ciphered text key.

    Uses a brute force technique trying the entire key space until finding a text
    that can be identified with any of our languages.

    **You should not use this function. Use *brute_force_mp* instead.** This
    function is slower than *mp* one because is sequential while the other uses a
    multiprocessing approach. This function only stay here to allow comparisons
    between sequential and multiprocessing approaches.

    :param ciphered_text: Text to be deciphered.
    :param _database_path: Absolute pathname to database file. Usually you don't
     set this parameter, but it is useful for tests.
    :return: Transposition key found.
    """
    key_space_length = len(ciphered_text)
    return simple_brute_force(
        key_generator=_integer_key_generator(key_space_length),
        assess_function=_assess_transposition_key,
        # key_space_length=key_space_length,
        ciphered_text=ciphered_text,
        _database_path=_database_path)