def _decode_16(self, key: str, cyphertext: str):
        """decodes a 16 byte string"""

        key_array = Converter.make_array(key)
        cyphertext_array = Converter.make_array(cyphertext)

        # generate round keys
        round_keys = self._generate_round_keys(
            Converter.byte_to_hex(key_array))
        # Add key0 to plaintext to start and first round
        state_array = round_keys[10] ^ cyphertext_array
        state_array = self._AES_decode_first_round(state_array)
        # main 9 round loop

        for i in range(1, 10):
            round = 10 - i
            state_array = self._AES_decode_round(state_array,
                                                 round_keys[round])

        # final round
        state_array = Converter.hex_to_byte(state_array)

        final_array = state_array ^ key_array

        return Converter.make_string(final_array)
    def _AES_encode_last_round(self, state_array, round_key):
        """performs the last round of encoding"""
        hex_array = Converter.byte_to_hex(state_array)

        step1 = self.sbox.matrix_sbox_substitution(hex_array)
        step2 = self._shift_rows(step1)
        step2 = Converter.hex_to_byte(step2)
        step3 = step2 ^ round_key

        return step3
    def _AES_decode_round(self, state_array, round_key):
        """reverses the steps of a signle encoding round"""
        step1 = Converter.hex_to_byte(state_array)
        step1 = step1 ^ round_key
        step2 = self._inverse_mix_columns(step1)
        step2 = Converter.byte_to_hex(step2)
        step3 = self._inverse_shift_rows(step2)
        step4 = self.inverse_sbox.matrix_sbox_substitution(step3)

        return step4
    def _AES_decode_first_round(self, state_array):
        """reverses the steps of the last encoding round"""
        step1 = Converter.byte_to_hex(state_array)
        step1 = self._inverse_shift_rows(step1)
        step2 = self.inverse_sbox.matrix_sbox_substitution(step1)

        return step2
    def _encode_16(self, key: str, plaintext: str) -> str:
        """encodes a 16 byte string """

        key_array = Converter.make_array(key)
        plaintext_array = Converter.make_array(plaintext)

        # generate round keys
        round_keys = self._generate_round_keys(
            Converter.byte_to_hex(key_array))

        # Add key0 to plaintext to start
        state_array = key_array ^ plaintext_array
        # main 9 round loop

        for round in range(1, 10):
            state_array = self._AES_encode_round(state_array,
                                                 round_keys[round])

        # final round

        final_array = self._AES_encode_last_round(state_array, round_keys[10])
        return Converter.make_string(final_array)
Beispiel #6
0
 def test_make_string(self):
     self.assertEqual(Converter.make_string(self.test_array),
                      self.test_string)
Beispiel #7
0
 def test_make_array(self):
     self.assertTrue(
         np.array_equal(Converter.make_array(self.test_string),
                        self.test_array))
Beispiel #8
0
 def test_hex_to_byte(self):
     self.assertTrue(
         np.array_equal(Converter.hex_to_byte(self.test_hex),
                        self.test_bytes))
Beispiel #9
0
 def test_byte_to_hex(self):
     self.assertTrue(
         np.array_equal(Converter.byte_to_hex(self.test_bytes),
                        self.test_hex))