0

I am using the following class to encode and decode, it works without issues when pass the encrypted string directly from the encrypt function to the decrypt function. But if I try to decrypt the encrypted string using online decryption it throws this error and the same happens if I try to decrypt a encrypted string from an external source.

class AESCipher(object):
    def __init__(self, key):
        self.bs = AES.block_size
        self.key = bytes.fromhex(key)

    def encrypt(self, raw):
        raw = pad(raw.encode('utf-8'), AES.block_size)
        cipher = AES.new(self.key, AES.MODE_ECB)
        encrypted_data = cipher.encrypt(raw)
        return encrypted_data.hex()

    def decrypt(self, enc):
        enc = bytes.fromhex(enc)
        cipher = AES.new(self.key, AES.MODE_ECB)
        decrypted_data = cipher.decrypt(enc)
        return decrypted_data

I tried using different types of .hex and encoding and decoding, but all had the same result.

1 Answers1

0

Replacing bytes.fromhex(key) with bytes(key, "utf-8") solved the issue.

  • That means 1. that the key wasn't derived from a string correctly by the online too and 2. that you were exacerbating the problem by inputting hexadecimals (max 4 bits of randomness per character instead of 8). There is no such thing as "the online tool", they all handle things differently, most of these are not well described w.r.t. encoding / decoding, and many of them I would consider broken. – Maarten Bodewes Aug 11 '23 at 22:46