Exemplo n.º 1
0
 def test_cs_crack(self):
     cipher = Caesar()
     cracker = TriangleAttack()
     open_text = normalize_text(
         "Ja, Syn Poklopu, rozzuren do silenstvi, vodarnu srovnam se zemi, jmenem mistru kanvodstvi. Ja, matku vlastni, vsak kdo vi zda, prokouknutou mel, nikoliv jak pribuzni. Tak jako davno pred casem jsme svatou chatru topily, stejny konec pripravime podvodnikum z vodarny. Nelze verit nikomu, kdo ma tlamu plnou cistoty. Vsechny tyhle bestie ceka voda ze stoky."
     )
     key = "q"
     cipher_text = cipher.encrypt(open_text, key)
     langmodel = LangModel(get_lang_path('cs'))
     cracked_key = cracker.crack(cipher_text, LanguageStats(langmodel))
     self.assertEqual(cracked_key, key)
 def test_cs_crack(self):
     cipher = Caesar()
     cracker = TriangleAttack()
     open_text = normalize_text(
         "Ja, Syn Poklopu, rozzuren do silenstvi, vodarnu srovnam se zemi, jmenem mistru kanvodstvi. Ja, matku vlastni, vsak kdo vi zda, prokouknutou mel, nikoliv jak pribuzni. Tak jako davno pred casem jsme svatou chatru topily, stejny konec pripravime podvodnikum z vodarny. Nelze verit nikomu, kdo ma tlamu plnou cistoty. Vsechny tyhle bestie ceka voda ze stoky."
     )
     key = "q"
     cipher_text = cipher.encrypt(open_text, key)
     langmodel = LangModel(get_lang_path("cs"))
     cracked_key = cracker.crack(cipher_text, LanguageStats(langmodel))
     self.assertEqual(cracked_key, key)
class VigenereBruteForce(object):
	"""Cryptanalysis method for Vigenere cipher"""
	def __init__(self):
		super(VigenereBruteForce, self).__init__()
		self.max_key_len = 10
		self.caesar_cracker = TriangleAttack()
		self.vigenere = Vigenere()
	
	def crack(self, cipher_text, langstats):
		key_text = self._get_possible_keys(cipher_text, langstats)
		return langstats.most_meaningful(key_text)

	def _get_possible_keys(self, cipher_text, langstats):
		for i in range(2, self.max_key_len + 1):
			partition = self._partition_text(cipher_text, i)
			key = "".join(self.caesar_cracker.crack(v, langstats) for v in partition)
			yield key, self.vigenere.decrypt(cipher_text, key)

	def _partition_text(self, text, number):
		classes = {x:[] for x in range(number)}
		for i, char in enumerate(text):
			classes[i % number].append(char)
		return ("".join(v) for k, v in classes.iteritems())
Exemplo n.º 4
0
class VigenereBruteForce(object):
    """Cryptanalysis method for Vigenere cipher"""
    def __init__(self):
        super(VigenereBruteForce, self).__init__()
        self.max_key_len = 10
        self.caesar_cracker = TriangleAttack()
        self.vigenere = Vigenere()

    def crack(self, cipher_text, langstats):
        key_text = self._get_possible_keys(cipher_text, langstats)
        return langstats.most_meaningful(key_text)

    def _get_possible_keys(self, cipher_text, langstats):
        for i in range(2, self.max_key_len + 1):
            partition = self._partition_text(cipher_text, i)
            key = "".join(
                self.caesar_cracker.crack(v, langstats) for v in partition)
            yield key, self.vigenere.decrypt(cipher_text, key)

    def _partition_text(self, text, number):
        classes = {x: [] for x in range(number)}
        for i, char in enumerate(text):
            classes[i % number].append(char)
        return ("".join(v) for k, v in classes.iteritems())