def test_decipher(self):
        test_msg = "Klein testje, kijken of dit werkt."

        test_cipher = translator.create_cipher(
            string.ascii_lowercase,
            string.ascii_lowercase[1:] + string.ascii_lowercase[0])

        encrypted_msg = translator.decipher_text(
            test_msg, {v: k
                       for k, v in test_cipher.items()})
        decrypted_msg = translator.decipher_text(text=encrypted_msg,
                                                 cipher=test_cipher)

        self.assertEqual(test_msg, decrypted_msg)
Example #2
0
    def _generic_decryption_test(self, test_lang_msgs, target_cipher):
        """
        Generic test whether decryption works as expected. Tests whether deciphering the
        encrypted message for a language results in the expected target_msg and target_cipher.

        :param target_cipher: Cipher to use on messages and try to find
        :return: Fails if deciphering doesn't work
        """
        # Run through all languages and test messages
        for language, original_msg in test_lang_msgs.items():
            # Encrypt the message
            msg_enc = encipher_text(original_msg, target_cipher)

            # Calculate the cipher and decipher the message with it
            if language == 'en':
                solver = self.solver_english
            elif language == 'nl':
                solver = self.solver_dutch
            elif language == 'de':
                solver = self.solver_german
            else:
                self.assertTrue(False)

            found_cipher, perc_correct = solver.solve(msg_enc=msg_enc)
            found_msg = decipher_text(text=msg_enc, cipher=found_cipher)
            print(found_msg)

            # Check against targets
            self.assertEqual(original_msg, found_msg)
Example #3
0
    async def get(self):
        """
        Assumes one argument (text).
        Deciphers this text and returns the result.

        :return: json with result and original text.
        """
        # Parse the arguments
        input_text = self.get_argument(name="text")
        language = self.get_argument(name="language")
        return_cipher = self.get_argument(name="cipher", default=False) == 'on'
        return_original = self.get_argument(name="original",
                                            default=False) == 'on'

        print("Received input text: {0}".format(input_text))

        if language == 'detect':
            cipher = None
            perc_correct = -1

            for l in ('en', 'nl', 'de'):
                cipher_new, perc_correct_new = self.application.solvers[
                    l].solve(msg_enc=input_text)
                if perc_correct_new > perc_correct:
                    perc_correct = perc_correct_new
                    cipher = cipher_new
        else:
            # Using the decipher class, decipher the text received
            try:
                solver = self.application.solvers[language]
            except KeyError:
                solver = Solver(language=language)
            cipher, perc_correct = solver.solve(msg_enc=input_text)

        output_text = translator.decipher_text(text=input_text, cipher=cipher)

        result_dict = {'output_text': output_text}
        if return_cipher:
            result_dict['cipher'] = cipher
        if return_original:
            result_dict['input_text'] = input_text

        self.finish(json.dumps(result_dict))
    def get_possible_words(self, word_enc, cipher={}):
        """
        Based on an already existing cipher, retrieve the possible decodings for a given word.
        
        :param word_enc: Encoded word
        :param cipher: Cipher found so far
        :return: Possible decodings
        """
        processed_word = process_word(word_enc, self._character_set)
        try:
            possible_words = self._word_dictionary[self._get_key_for_word(
                processed_word)]
            cipher_copy = cipher.copy()

            # Determine what to fill cipher with
            # Replace this cipher with a negation of the already known values
            # Those values cannot become the result of the cipher anymore
            if len(cipher.values()) <= 0:
                cipher_fill = '.'
            else:
                cipher_fill = '[^' + ''.join(cipher.values()) + ']'

            # Fill cipher with missing keys
            for c in list(self._character_set):
                if c not in cipher_copy.keys():
                    cipher_copy[c] = cipher_fill

            regex_string = decipher_text(processed_word, cipher_copy)

            regex = re.compile(regex_string)

            # Filter out possibilities with current cipher keys
            possible_words = list(filter(regex.search, possible_words))
            return possible_words
        except KeyError:
            logging.error("No possible words found for: {}".format(word_enc))
            return []