Score:1

'str' object has no attribute 'decode' when decrypt aes

st flag

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'

fgrieu avatar
ng flag
Not only are programming question off-topic, but the extracts do not cover the lines with `get_response` and `password.decode`which are these where the error is reported to happen.
PyhonLovers avatar
st flag
so how to fix it? @fgrieu
DannyNiu avatar
vu flag
Strings encodes into bytes, and bytes decodes into strings. Python 3 now makes such distinction - strings are to be human-readable, bytes are raw data.
PyhonLovers avatar
st flag
so i must change the encode to decode at decrypt func when want to make new aes? @DannyNiu
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.