AES in CTR mode

Asked

Viewed 23 times

0

I’m developing AES in CTR mode for my work, but I’m not getting it to return the right result, both at Encrypt and at Decrypt,.

Follow the code:

class AESCipher(object):
    def __init__(self, key):

       

    def encrypt(self, plain_text):
        print("Plaint_text com pad: ",plain_text)
        bytes = PBKDF2(plain_text.encode("utf-8"), "salt".encode("utf-8"), 48, 128)
        iv = bytes[0:16]
        self.key = bytes[16:48]
        print("key: ", self.key)
        #iv = int(self.block_size)
        print("iv:", iv)
        #iv_int = int(hex(iv), 16)
        iv_int = int.from_bytes(iv, "little")
        #print("iv_int:", iv_int)
        ctr = Counter.new(AES.block_size*8, initial_value=iv_int)
        print("ctr:", ctr)
        cipher = AES.new(self.key, AES.MODE_CTR, counter=ctr)
        print("cipher:", cipher)
        encrypted_text = cipher.encrypt(plain_text.encode())
        print("encrypted_text:", encrypted_text)
        aux = b16encode(encrypted_text).decode("utf-8")
        return (aux)

    def decrypt(self, encrypted_text):
        encrypted_text = b16decode(encrypted_text)
        bytes = PBKDF2(str(encrypted_text).encode("utf-8"), "salt".encode("utf-8"), 48, 128)
        iv = bytes[0:16]
        #iv = encrypted_text[:self.block_size]
        #print(hex(iv))
        #iv_int = int(str(iv).encode('hex'), 16)
        iv_int = int.from_bytes(iv, "little")
        ctr = Counter.new(128, initial_value=iv_int)
        cipher = AES.new(self.key, AES.MODE_CTR, counter=ctr)
        plain_text = cipher.decrypt(encrypted_text[self.block_size:])
        result = b16encode(plain_text).decode("utf-8")
        return result

Entrances:

obj = Aescipher("000102030405060708090A0B0C0D0E0F")

aux = obj.Encrypt("00112233445566778899ABBCCDEEFF")

Decrypted text does not match clear text. Neither does encrypted text. this was to be the result of Encrypt: 69C4E0D86A7B0430D8CDB78070B4C55A

  • 'Cause you’re having one key as a parameter if in the __init__ You don’t do anything to her?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.