コード例 #1
0
ファイル: mengenlehreuhr.py プロジェクト: mproffitt/PyKryptos
 def _get_decipher(self, args):
     return Decipher(args)
コード例 #2
0
ファイル: mengenlehreuhr.py プロジェクト: mproffitt/PyKryptos
class Mengenlehreuhr():
    """
    The Mengenlehreur clock tells the time by utilising Set-Theory

    This version of the clock is used to encipher/decipher messages
    by encoding them using a sum of visible characters on the clock face,
    adding to, or subtracting from (depending on the method used) this
    value to the alphabetical index value of the current  message index.

    Equation:
        c = cipher
        m = message
        a = alphabet

        Add:
        ci = (mi + (E[ai == 1] % 26)) % 26

        Subtract
        ci = (mi - (E[ai == 1] % 26)) > 0 ^ 26 + (mi - (E[ai == 1] % 26))
    """
    def __init__(self):
        self.args = Parser().parse_args()
        self.time = datetime.now()
        self.decipher = Decipher(self.args)

    def run(self):
        """Run the clock until a keyboard interrupt."""
        self.time = (datetime.now() if self.args.time == 'now' else
                     datetime.strptime(self.args.time, '%H:%M:%S'))

        self.clock = self._get_clock(self._get_time_item(self.time))
        try:
            while True:
                self._update(self.time)
                if self.args.increment == 'minute':
                    if self.time.second & 0x1:
                        self.time += timedelta(seconds=1)
                    self.time += timedelta(minutes=1)
                else:
                    self.time += timedelta(seconds=1)

                sleep(self.args.speed)
        except KeyboardInterrupt:
            pass
        finally:
            self._stop()

    def _stop(self):
        """ Cleanup and return control to the console """
        self.clock.stop()

    def _update(self, time):
        time_item = self._get_time_item(time)
        if time.second % 2 == 0:
            time_item.character = self.decipher.get_next_cipher()
        self.clock.update(time_item)
        character = (self.decipher.add(time_item.character, self.clock.visible)
                     if self.args.method == 'add' else self.decipher.subtract(
                         time_item.character, self.clock.visible))
        if time.second % 2 == 0:
            self.clock.write(
                LogItem(time, character, self.decipher.cipher_index))

    def _get_clock(self, time):
        return Clock(time, self.decipher)

    def _get_decipher(self, args):
        return Decipher(args)

    def _get_time_item(self, time):
        return TimeItem(time=time)
コード例 #3
0
ファイル: kryptostest.py プロジェクト: mproffitt/PyKryptos
 def setUp(self):
     self.decipher = Decipher(TestArgs())
コード例 #4
0
ファイル: mengenlehreuhr.py プロジェクト: mproffitt/PyKryptos
 def __init__(self):
     self.args = Parser().parse_args()
     self.time = datetime.now()
     self.decipher = Decipher(self.args)
コード例 #5
0
ファイル: kryptostest.py プロジェクト: mproffitt/PyKryptos
class DecipherTest(unittest.TestCase):
    def setUp(self):
        self.decipher = Decipher(TestArgs())

    def tearDown(self):
        pass

    @data(('A', 1), ('G', 7), ('S', 19), ('Y', 25))
    @unpack
    def test_get_index(self, char, expected):
        assert self.decipher.get_index(char) == expected

    @data([[
        'W', 'X', 'S', 'T', 'F', 'G', 'H', 'I', 'J', 'K', 'M', 'N', 'P', 'Q',
        'R', 'A', 'B'
    ], 10], [[
        'W', 'X', 'Y', 'S', 'T', 'U', 'F', 'G', 'H', 'I', 'J', 'K', 'M', 'N',
        'P', 'Q', 'R', 'A', 'B'
    ], 4], [[
        'W', 'S', 'F', 'G', 'H', 'I', 'J', 'K', 'M', 'N', 'A', 'B', 'C', 'D'
    ], 26])
    @unpack
    def test_get_index_of_visible_chars(self, visible, expected):
        assert self.decipher.get_index_from_visible_chars(visible) == expected

    @data([
        'H',
        [
            'W', 'X', 'S', 'T', 'F', 'G', 'H', 'I', 'J', 'K', 'M', 'N', 'P',
            'Q', 'R', 'A', 'B'
        ], 'R'
    ], [
        'E',
        [
            'W', 'X', 'S', 'T', 'F', 'G', 'H', 'I', 'J', 'K', 'M', 'N', 'P',
            'Q', 'R', 'A', 'B', 'C'
        ], 'R'
    ], [
        'L',
        [
            'W', 'X', 'S', 'T', 'F', 'G', 'H', 'I', 'J', 'K', 'M', 'N', 'P',
            'Q', 'R', 'A', 'B', 'C', 'D'
        ], 'C'
    ], ['L', ['W', 'X', 'S', 'T', 'U'], 'O'],
          ['O', ['W', 'X', 'S', 'T', 'U', 'A'], 'S'],
          ['W', ['W', 'X', 'S', 'T', 'U', 'A', 'B'], 'C'],
          ['O', ['W', 'X', 'S', 'T', 'U', 'A', 'B', 'C'], 'X'],
          ['R', ['W', 'X', 'S', 'T', 'U', 'A', 'B', 'C', 'D'], 'E'],
          ['L', ['W', 'X', 'S', 'T', 'U', 'F'], 'U'],
          ['D', ['W', 'X', 'S', 'T', 'U', 'F', 'A'], 'N'])
    @unpack
    def test_add_character(self, character, visible, expected):
        assert self.decipher.add(character, visible) == expected

    @data([
        'R',
        [
            'W', 'X', 'S', 'T', 'F', 'G', 'H', 'I', 'J', 'K', 'M', 'N', 'P',
            'Q', 'R', 'A', 'B'
        ], 'H'
    ], [
        'R',
        [
            'W', 'X', 'S', 'T', 'F', 'G', 'H', 'I', 'J', 'K', 'M', 'N', 'P',
            'Q', 'R', 'A', 'B', 'C'
        ], 'E'
    ], [
        'C',
        [
            'W', 'X', 'S', 'T', 'F', 'G', 'H', 'I', 'J', 'K', 'M', 'N', 'P',
            'Q', 'R', 'A', 'B', 'C', 'D'
        ], 'L'
    ], ['O', ['W', 'X', 'S', 'T', 'U'], 'L'],
          ['S', ['W', 'X', 'S', 'T', 'U', 'A'], 'O'],
          ['C', ['W', 'X', 'S', 'T', 'U', 'A', 'B'], 'W'],
          ['X', ['W', 'X', 'S', 'T', 'U', 'A', 'B', 'C'], 'O'],
          ['E', ['W', 'X', 'S', 'T', 'U', 'A', 'B', 'C', 'D'], 'R'],
          ['U', ['W', 'X', 'S', 'T', 'U', 'F'], 'L'],
          ['N', ['W', 'X', 'S', 'T', 'U', 'F', 'A'], 'D'])
    @unpack
    def test_subtract_character(self, character, visible, expected):
        assert self.decipher.subtract(character, visible) == expected

    def test_get_next_cipher(self):
        for character in [c for c in TestArgs.ciphertext]:
            assert self.decipher.get_next_cipher() == character
コード例 #6
0
ファイル: mengenlehreuhr.py プロジェクト: mproffitt/PyKryptos
class Mengenlehreuhr():
    """
    The Mengenlehreur clock tells the time by utilising Set-Theory

    This version of the clock is used to encipher/decipher messages
    by encoding them using a sum of visible characters on the clock face,
    adding to, or subtracting from (depending on the method used) this
    value to the alphabetical index value of the current  message index.

    Equation:
        c = cipher
        m = message
        a = alphabet

        Add:
        ci = (mi + (E[ai == 1] % 26)) % 26

        Subtract
        ci = (mi - (E[ai == 1] % 26)) > 0 ^ 26 + (mi - (E[ai == 1] % 26))
    """

    def __init__(self):
        self.args     = Parser().parse_args()
        self.time     = datetime.now()
        self.decipher = Decipher(self.args)

    def run(self):
        """Run the clock until a keyboard interrupt."""
        self.time = (
            datetime.now() if self.args.time == 'now' else datetime.strptime(self.args.time, '%H:%M:%S')
        )

        self.clock = self._get_clock(
            self._get_time_item(self.time)
        )
        try:
            while True:
                self._update(self.time)
                if self.args.increment == 'minute':
                    if self.time.second & 0x1:
                        self.time += timedelta(seconds=1)
                    self.time += timedelta(minutes=1)
                else:
                    self.time += timedelta(seconds=1)

                sleep(self.args.speed)
        except KeyboardInterrupt:
            pass
        finally:
            self._stop()

    def _stop(self):
        """ Cleanup and return control to the console """
        self.clock.stop()

    def _update(self, time):
        time_item = self._get_time_item(time)
        if time.second % 2 == 0:
            time_item.character = self.decipher.get_next_cipher()
        self.clock.update(time_item)
        character = (
            self.decipher.add(time_item.character, self.clock.visible)
            if self.args.method == 'add' else
                self.decipher.subtract(time_item.character, self.clock.visible)
        )
        if time.second % 2 == 0:
            self.clock.write(
                LogItem(
                    time,
                    character,
                    self.decipher.cipher_index
                )
            )

    def _get_clock(self, time):
        return Clock(time, self.decipher)

    def _get_decipher(self, args):
        return Decipher(args)

    def _get_time_item(self, time):
        return TimeItem(time=time)
コード例 #7
0
ファイル: mengenlehreuhr.py プロジェクト: mproffitt/PyKryptos
 def __init__(self):
     self.args     = Parser().parse_args()
     self.time     = datetime.now()
     self.decipher = Decipher(self.args)
コード例 #8
0
ファイル: kryptostest.py プロジェクト: mproffitt/PyKryptos
 def setUp(self):
     self.decipher = Decipher(TestArgs())
コード例 #9
0
ファイル: kryptostest.py プロジェクト: mproffitt/PyKryptos
class DecipherTest(unittest.TestCase):

    def setUp(self):
        self.decipher = Decipher(TestArgs())

    def tearDown(self):
        pass

    @data(
        ('A', 1),
        ('G', 7),
        ('S', 19),
        ('Y', 25)
    )
    @unpack
    def test_get_index(self, char, expected):
        assert self.decipher.get_index(char) == expected

    @data(
        [['W', 'X', 'S', 'T', 'F','G','H','I','J','K','M','N','P','Q','R','A','B'], 10],
        [['W', 'X','Y', 'S', 'T', 'U', 'F','G','H','I','J','K','M','N','P','Q','R','A','B'],  4],
        [['W', 'S', 'F', 'G', 'H', 'I', 'J', 'K', 'M', 'N', 'A', 'B', 'C', 'D'], 26]
    )
    @unpack
    def test_get_index_of_visible_chars(self, visible, expected):
        assert self.decipher.get_index_from_visible_chars(visible) == expected

    @data(
        ['H', ['W', 'X', 'S', 'T', 'F','G','H','I','J','K','M','N','P','Q','R','A','B'],         'R'],
        ['E', ['W', 'X', 'S', 'T', 'F','G','H','I','J','K','M','N','P','Q','R','A','B','C'],     'R'],
        ['L', ['W', 'X', 'S', 'T', 'F','G','H','I','J','K','M','N','P','Q','R','A','B','C','D'], 'C'],
        ['L', ['W', 'X', 'S', 'T', 'U'],                                                         'O'],
        ['O', ['W', 'X', 'S', 'T', 'U','A'],                                                     'S'],
        ['W', ['W', 'X', 'S', 'T', 'U','A','B'],                                                 'C'],
        ['O', ['W', 'X', 'S', 'T', 'U','A','B','C'],                                             'X'],
        ['R', ['W', 'X', 'S', 'T', 'U','A','B','C','D'],                                         'E'],
        ['L', ['W', 'X', 'S', 'T', 'U','F'],                                                     'U'],
        ['D', ['W', 'X', 'S', 'T', 'U','F','A'],                                                 'N']
    )
    @unpack
    def test_add_character(self, character, visible, expected):
        assert self.decipher.add(character, visible) == expected

    @data(
        ['R', ['W', 'X', 'S', 'T', 'F','G','H','I','J','K','M','N','P','Q','R','A','B'],         'H'],
        ['R', ['W', 'X', 'S', 'T', 'F','G','H','I','J','K','M','N','P','Q','R','A','B','C'],     'E'],
        ['C', ['W', 'X', 'S', 'T', 'F','G','H','I','J','K','M','N','P','Q','R','A','B','C','D'], 'L'],
        ['O', ['W', 'X', 'S', 'T', 'U'],                                                         'L'],
        ['S', ['W', 'X', 'S', 'T', 'U','A'],                                                     'O'],
        ['C', ['W', 'X', 'S', 'T', 'U','A','B'],                                                 'W'],
        ['X', ['W', 'X', 'S', 'T', 'U','A','B','C'],                                             'O'],
        ['E', ['W', 'X', 'S', 'T', 'U','A','B','C','D'],                                         'R'],
        ['U', ['W', 'X', 'S', 'T', 'U','F'],                                                     'L'],
        ['N', ['W', 'X', 'S', 'T', 'U','F','A'],                                                 'D']
    )
    @unpack
    def test_subtract_character(self, character, visible, expected):
        assert self.decipher.subtract(character, visible) == expected

    def test_get_next_cipher(self):
        for character in [c for c in TestArgs.ciphertext]:
            assert self.decipher.get_next_cipher() == character