示例#1
0
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'))
 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()]))
示例#3
0
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'))
示例#4
0
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)
 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()]))
 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()]))
 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()]))
 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()]))
示例#9
0
    def crack(self, encrypted_message):
        combinations = list(
            product(range(len(GERMAN_ALPHABET_CHARACTERS)), repeat=4))
        shuffle(combinations)
        print(combinations[:10])

        kb_divergences = {}
        i = 0
        for combination in combinations:
            rotor_1 = Rotor(combination[0], HARDCODED_PERIOD)
            rotor_2 = Rotor(combination[1], HARDCODED_PERIOD)
            rotor_3 = Rotor(combination[2], HARDCODED_PERIOD)
            rotor_4 = Rotor(combination[3], HARDCODED_PERIOD)
            enigma_machine = Enigma(plugboard,
                                    [rotor_1, rotor_2, rotor_3, rotor_4])
            decrypted_message = enigma_machine.decrypt(encrypted_message)
            kb_divergences[combination] = (
                self.language_model.fitness(decrypted_message),
                decrypted_message)
            #print(i)
            i += 1
        sorted_divergences = sorted(kb_divergences.items(), key=lambda x: x[1])
        return sorted_divergences[:1000]
 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()]))
示例#11
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))
示例#12
0
 def setUp(self):
     self.enigma = Enigma()
示例#13
0
 def setUp(self):
     self.enigma = Enigma(reflector='YRUHQSLDPXNGOKMIEBFZCWVJAT')
示例#14
0
文件: main.py 项目: adam-good/Enigma
from enigma.enigma import Enigma
from enigma.enigma import Rotor
import sys

e = Enigma(0, 0, 0)

rotorNums = []
rotorOffsets = []
rotors = []

print("[*] Which Rotors Would You Like To Use? (1-8)")
for i in range(1, 4):
    num = int(raw_input("Position " + str(i) + ") "))

    while num < 1 or num > 8:
        num = int(raw_input("Invalid Input! Try again: "))

    rotorNums.append(num)

print('')

print("[*] Set Starting Positions For Each Rotor: (0-25)")
for i in range(1, 4):
    num = int(raw_input("Rotor Position " + str(i) + ") "))

    while num < 0 or num > 25:
        num = int(raw_input("Invalid Input! Try again: "))

    rotorOffsets.append(num)

for i in range(3):
 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")