Exemple #1
0
def test_repeated_character_keys_are_detected():
    TEST_CHARSET = "123"
    WRONG_KEY = "122"
    with pytest.raises(substitution.WrongSubstitutionKey) as e:
        _ = substitution.cipher("", WRONG_KEY, TEST_CHARSET)
    assert e.value.get_cause(
    )[0] == substitution.WrongSubstitutionKeyCauses.repeated_characters
Exemple #2
0
def test_wrong_length_key_are_detected():
    TEST_CHARSET = "123"
    WRONG_KEY = "1234"
    with pytest.raises(substitution.WrongSubstitutionKey) as e:
        _ = substitution.cipher("", WRONG_KEY, TEST_CHARSET)
    assert e.value.get_cause(
    )[0] == substitution.WrongSubstitutionKeyCauses.wrong_key_length
Exemple #3
0
def test_attack_substitution(temp_dir, loaded_dictionaries: LoadedDictionaries):
    with tempfile.NamedTemporaryFile(mode="w") as message_file, \
            open(os.path.join(os.getcwd(), "cifra", "tests", "resources/english_book_c1.txt")) as english_book:
        original_message = english_book.read()
        ciphered_text = substitution.cipher(original_message, substitution_TEST_KEY, substitution_TEST_CHARSET)
        message_file.write(ciphered_text)
        message_file.flush()
        output_file_pathname = os.path.join(temp_dir, "recovered_message.txt")
        provided_args = f"attack substitution {message_file.name} --deciphered_file {output_file_pathname} --charset {substitution_TEST_CHARSET}".split()
        cifra_launcher.main(provided_args, loaded_dictionaries.temp_dir)
        with open(output_file_pathname, mode="r") as output_file:
            recovered_content = output_file.read()
            assert original_message == recovered_content
def test_hack_substitution_mp(loaded_dictionaries: LoadedDictionaries,
                              text_file: str, language: str, key: str,
                              charset: str):
    elapsed_time = []
    with timeit(elapsed_time):
        text_file_pathname = os.path.join(os.getcwd(), "cifra", "tests",
                                          text_file)
        with open(text_file_pathname) as text_file:
            clear_text = text_file.read()
            ciphered_text = substitution.cipher(clear_text, key, charset)
        found_key = attack_substitution.hack_substitution_mp(
            ciphered_text,
            charset,
            _database_path=loaded_dictionaries.temp_dir)
        assert key == found_key[0]
    print(
        f"\n\nElapsed time with test_hack_substitution: {elapsed_time[0]} seconds."
    )
Exemple #5
0
def test_cipher():
    ciphered_text = substitution.cipher(ORIGINAL_MESSAGE,
                                        TEST_KEY,
                                        charset=TEST_CHARSET)
    assert ciphered_text == CIPHERED_MESSAGE