so i have function for encryption and decryption using AES CTR. i was using python 3.x and the idea for this aes ctr from tweaksp
when i try to decrypt to get the plaintext, i got error message:
'bytes' object has no attribute 'encode'
after i check another stackoverflow, i found if .encode('hex')
not works on python 3.x base this
here's my code for encrypt:
key_bytes = 16
def encrypt(key, pt):
plaintext = read_file(pt)
if isinstance(plaintext, str):
pt= plaintext.encode("utf-8")
if len(key) <= key_bytes:
for x in range(len(key),key_bytes):
key = key + "0"
assert len(key) == key_bytes
# Choose a random, 16-byte IV.
iv = Random.new().read(AES.block_size)
# Convert the IV to a Python integer.
iv_int = int(binascii.hexlify(iv), 16)
# Create a new Counter object with IV = iv_int.
ctr = Counter.new(AES.block_size * 8, initial_value=iv_int)
# Create AES-CTR cipher.
aes = AES.new(key.encode('utf8'), AES.MODE_CTR, counter=ctr)
# Encrypt and return IV and ciphertext.
ciphertext = aes.encrypt(pt)
return (iv, ciphertext)
what i input for decrypt
key : maru000000000000
iv : b'he\xf5\xba\x9bf\xf4\xacfA\xa6\xc7\xce\xd0\x90j'
ciphertext : 01101000011001011111010110111010100110110110011011110100101011000110011001000001101001101100011111001110110100001001000001101010
and my decrypt code:
def decrypt(key, iv, ciphertext):
assert len(key) == key_bytes
print(type(iv))
# Initialize counter for decryption. iv should be the same as the output of
# encrypt().
iv_int = int(iv.encode('hex'), 16)
ctr = Counter.new(AES.block_size * 8, initial_value=iv_int)
# Create AES-CTR cipher.
aes = AES.new(key, AES.MODE_CTR, counter=ctr)
# Decrypt and return the plaintext.
plaintext = aes.decrypt(ciphertext)
return plaintext