コード例 #1
0
ファイル: test_enigma.py プロジェクト: kentjas1/enigma
class SimpleMessageCipherTestCase(TestCase):
    def setUp(self):
        self.enigma = Enigma()

    def test_enigma_encrypts_BC_as_JM(self):
        self.assertEqual('JM', self.enigma.encrypt('BC'))

    def test_enigma_encrypts_ABC_as_EJM(self):
        self.assertEqual('EJM', self.enigma.encrypt('ABC'))
コード例 #2
0
ファイル: test_enigma.py プロジェクト: kentjas1/enigma
class SingleLetterCipherTestCase(TestCase):
    def setUp(self):
        self.enigma = Enigma()

    def test_enigma_encrypts_blank_as_blank(self):
        self.assertEqual('', self.enigma.encrypt(''))

    def test_enigma_encrypts_A_as_E(self):
        self.assertEqual('E', self.enigma.encrypt('A'))

    def test_enigma_encrypts_B_as_J(self):
        self.assertEqual('J', self.enigma.encrypt('B'))

    def test_enigma_encrypts_C_as_M(self):
        self.assertEqual('M', self.enigma.encrypt('C'))
コード例 #3
0
ファイル: test_enigma.py プロジェクト: kentjas1/enigma
class RotorUKWBTestCase(TestCase):
    def setUp(self):
        self.enigma = Enigma(reflector='YRUHQSLDPXNGOKMIEBFZCWVJAT')

    def test_enigma_stores_reflector_as_instance_variable(self):
        enigma = Enigma(reflector='foo')
        self.assertEqual('foo', enigma.reflector)

    def test_enigma_reflector_translates_A_as_Y(self):
        self.assertEqual('Y', self.enigma.reflect('A'))

    @patch('enigma.enigma.Enigma.reflect')
    def test_enigma_reflector_used_during_encryption_when_present(self, mock_reflect):
        self.enigma.encrypt('')
        self.assertEqual(1, mock_reflect.call_count)
コード例 #4
0
 def test_encryption_w_m3_reflectorC(self):
     enigma = Enigma(reflector=ReflectorC(),
                     plugboard=Plugboard(),
                     rotors=[RotorIII(), RotorII(),
                             RotorI()])
     self.assertEqual(enigma.decrypt(enigma.encrypt("A")), "A")
     self.assertEqual(enigma.encrypt("AAAAA"), "PJBUZ")
     self.assertEqual(
         enigma.encrypt(REDUCED_LOREM_IPSUM),
         "KQZIFEZXSBHUFEFRRNSHTHEDTISEBMIKIURGQERSLBAVMMINXGWLKRCLKQWKBFUKEMSISOUNNBFGATWWKDP"
     )
     cyphertext = enigma.encrypt(LOREM_IPSUM)
     self.assertEqual(
         enigma.decrypt(cyphertext),
         "".join([c for c in LOREM_IPSUM.upper() if c.isalpha()]))
コード例 #5
0
 def test_encryption_w_m3_reflectorB(self):
     enigma = Enigma(reflector=ReflectorB(),
                     plugboard=Plugboard(),
                     rotors=[RotorIII(), RotorII(),
                             RotorI()])
     self.assertEqual(enigma.decrypt(enigma.encrypt("A")), "A")
     self.assertEqual(enigma.encrypt("AAAAA"), "BDZGO")
     self.assertEqual(enigma.encrypt("PROBANDO"), "LCNCOEFQ")
     self.assertEqual(
         enigma.encrypt(REDUCED_LOREM_IPSUM),
         "PIXWHLIFPVFHECGDYIPYYXWASFXPPOALEJGRHCZXDTNKGZAFAZXFGOSGIYMRMEEZHQBOWSKIZJAVWFDATOH"
     )
     cyphertext = enigma.encrypt(LOREM_IPSUM)
     self.assertEqual(
         enigma.decrypt(cyphertext),
         "".join([c for c in LOREM_IPSUM.upper() if c.isalpha()]))
コード例 #6
0
 def test_encryption_double_notch_rotors(self):
     enigma = Enigma(reflector=ReflectorB(),
                     plugboard=Plugboard(),
                     rotors=[
                         RotorVIII(ring_setting=1),
                         RotorVII(ring_setting=1),
                         RotorVI(ring_setting=1)
                     ])
     self.assertEqual(
         enigma.encrypt(REDUCED_LOREM_IPSUM),
         "QPEVWGFMEFUKAMFQQOHBMHYMEQNTFCOWYMKZBTGEEMWZEPLDYYNTLPXAVEYCZBJWYOQMLCJHITIYZRDZTPX"
     )
     cyphertext = enigma.encrypt(LOREM_IPSUM)
     self.assertEqual(
         enigma.decrypt(cyphertext),
         "".join([c for c in LOREM_IPSUM.upper() if c.isalpha()]))
コード例 #7
0
 def test_encryption_m4(self):
     enigma = Enigma(reflector=ReflectorBThin(),
                     plugboard=Plugboard(),
                     rotors=[
                         RotorIII(ring_setting=1),
                         RotorII(),
                         RotorI(),
                         RotorBeta(ring_setting=3)
                     ])
     self.assertEqual(
         enigma.encrypt(REDUCED_LOREM_IPSUM),
         "NXVMWHIMNFCIDSJXDNQTIBJNYMAWWWVNCPQZJREWOUCNDKSHKQRBMETDSWEJDVJUIEGETVQUAORAYMKMDFA"
     )
     cyphertext = enigma.encrypt(LOREM_IPSUM)
     self.assertEqual(
         enigma.decrypt(cyphertext),
         "".join([c for c in LOREM_IPSUM.upper() if c.isalpha()]))
コード例 #8
0
 def test_encryption_w_ringstellung(self):
     enigma = Enigma(reflector=ReflectorB(),
                     plugboard=Plugboard(),
                     rotors=[
                         RotorIII(ring_setting=1),
                         RotorII(ring_setting=1),
                         RotorI(ring_setting=1)
                     ])
     self.assertEqual(enigma.encrypt("AAAAA"), "EWTYX")
     self.assertEqual(
         enigma.encrypt(REDUCED_LOREM_IPSUM),
         "HEPCLDHGHAMAXFGUGXCKOFIHDQLOMXROJEYSVJWPUBSCQOAMNMNZBKPPPNLFJOILBXIBKXTGEKEGQNSQZPK"
     )
     cyphertext = enigma.encrypt(LOREM_IPSUM)
     self.assertEqual(
         enigma.decrypt(cyphertext),
         "".join([c for c in LOREM_IPSUM.upper() if c.isalpha()]))
コード例 #9
0
 def test_encryption_decryption_w_ukw_c(self):
     enigma = Enigma(reflector=ReflectorC(),
                     plugboard=Plugboard(PLUGBOARD_TUPLES),
                     rotors=[
                         Rotor(offset=10, period=1),
                         Rotor(offset=10, period=2),
                         Rotor(offset=10, period=3),
                         Rotor(offset=10, period=7),
                         Rotor(offset=10, period=26)
                     ])
     cyphertext = enigma.encrypt(LOREM_IPSUM)
     self.assertEqual(
         enigma.decrypt(cyphertext),
         "".join([c for c in LOREM_IPSUM.upper() if c.isalpha()]))
コード例 #10
0
                decrypted_message)
            #print(i)
            i += 1
        sorted_divergences = sorted(kb_divergences.items(), key=lambda x: x[1])
        return sorted_divergences[:1000]


plugboard_pairs = []
# TODO: Discuss if plugboard pairs are random.
characters = [a.upper() for a in GERMAN_ALPHABET_CHARACTERS]
shuffle(characters)
for i in range(10):
    plugboard_pairs.append((characters[i], characters[-i - 1]))

plugboard = Plugboard(plugboard_pairs)

language_model2 = CharacterFrequencyKLDLanguageModel(
    "probarunanuevamaneradeescribir".upper())
cracker = Cracker(language_model2, 10)

enigma = Enigma(plugboard, [
    Rotor(1, HARDCODED_PERIOD),
    Rotor(10, HARDCODED_PERIOD),
    Rotor(12, HARDCODED_PERIOD),
    Rotor(15, HARDCODED_PERIOD)
])

encrypted = enigma.encrypt("probarunanuevamaneradeescribir".upper())

print(cracker.crack(encrypted))
コード例 #11
0
 def test_encryption_decryption_one_rotor(self):
     enigma = Enigma(reflector=Reflector(),
                     plugboard=Plugboard(PLUGBOARD_TUPLES),
                     rotors=[Rotor(offset=10, period=1)])
     cyphertext = enigma.encrypt("Probando")
     self.assertEqual(enigma.decrypt(cyphertext), "PROBANDO")