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
Beispiel #4
0
 def test_hex_to_byte(self):
     self.assertTrue(
         np.array_equal(Converter.hex_to_byte(self.test_hex),
                        self.test_bytes))