so i have function to decrypt aes ctr. but when i tried to decrypt, the result i got from plaintext is type bytes. i want to get the result of plaintext using decode, because when i encrypt it, i was encode it first.
here's my encrypt func:
def encrypt(key, pt):
plaintext = read_file(pt)
if isinstance(plaintext, str):
pt= plaintext.encode("utf-8")
print(pt)
print(type(pt))
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)
print(iv_int)
# 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)
and my decrypt func :
def decrypt(key, iv, ciphertext):
assert len(key) == key_bytes
print(key)
print(iv)
print(ciphertext)
print(type(ciphertext))
# Initialize counter for decryption. iv should be the same as the output of
# encrypt().
iv_int = int(binascii.hexlify(iv), 16)
print(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)
# Decrypt and return the plaintext.
plaintext = aes.decrypt(ciphertext)
if isinstance(plaintext, bytes):
pt= plaintext.decode("ascii")
print(pt)
print(type(pt))
return pt
the result from plaintext : b'\xf6\x18n:\x8a\xb6\x8e\xb1\xb1'
what i want to get from plaintext / pt : SandI2021
and my traceback:
Traceback (most recent call last): File
"C:\Users\Capoo\lib\site-packages\django\core\handlers\exception.py",
line 55, in inner
response = get_response(request) File "C:\Users\Capoo\lib\site-packages\django\core\handlers\base.py", line
197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) File "F:\KULIAH\SEMESTER8\SKRIPSI\MusicLockApp\MusicLockApp\views.py", line
171, in decode
final = password.decode() AttributeError: 'str' object has no attribute 'decode'