#๐Ÿ”’ Decryption not working

24 messages ยท Page 1 of 1 (latest)

wary wolf
#

I made these two encryption & decryption functions:

    def encrypt(self, data, password, password2):
        salt = get_salt()
        key = scrypt(password, salt, 32, N=2 ** 14, r=8, p=1)
        cipher = AES.new(key, AES.MODE_CBC)
        ct_bytes = cipher.encrypt(pad(data.encode(), AES.block_size))

        salt2 = get_salt()
        key2 = scrypt(password2, salt2, 32, N=2 ** 14, r=8, p=1)
        cipher2 = AES.new(key2, AES.MODE_CBC)
        ct2_bytes = cipher2.encrypt(pad(ct_bytes, AES.block_size))

        iv = base64.b64encode(cipher.iv).decode()
        ct = base64.b64encode(ct2_bytes).decode()
        salt = base64.b64encode(salt.encode()).decode()
        salt2 = base64.b64encode(salt2.encode()).decode()

        return f"{iv}:{ct}:{salt}:{salt2}"

    def decrypt(self, data, password, password2):
        iv, ct, salt, salt2 = data.split(":")
        iv = base64.b64decode(iv)
        ct = base64.b64decode(ct)
        salt = base64.b64decode(salt).decode()
        salt2 = base64.b64decode(salt2).decode()

        key2 = scrypt(password2, salt2, 32, N=2 ** 14, r=8, p=1)
        cipher2 = AES.new(key2, AES.MODE_CBC)
        pt2 = unpad(cipher2.decrypt(ct), AES.block_size).decode()

        key = scrypt(password, salt, 32, N=2 ** 14, r=8, p=1)
        cipher = AES.new(key, AES.MODE_CBC, iv=iv)
        pt = unpad(cipher.decrypt(pt2.encode()), AES.block_size).decode()

        return pt

Encryption is working fine for me, it's decryption that's the issue. Getting this error from line pt2 = unpad(cipher2.decrypt(ct), AES.block_size).decode()

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 9: invalid start byte

I don't really understand encoding and decoding (conversion from chars to bytes??) so I appreciate any help with this.

formal pikeBOT
#

@wary wolf

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wary wolf
#

I'm not sure if it's neccessary to send my other functions, as I suspect these are the root of the problem but let me know if I should

sonic creek
#

mid process python uses unicode so idk but it could be that they are unicode bytes trying to be decoded to utf-8 by any chance idk

wary wolf
#

So should I specify and different encoding to .decode()?

#

I thought utf-8 is based off unicode?

sonic creek
#

idk i dont do this kind of stuff either its just a guess

narrow grail
#

Why are you trying to decode there? It's bytes and it should be bytes at this point, shouldn't it? The decode tries to make it into a string

wary wolf
#

You're right, and then I go and encode it again for no reason

#

Let me try it

narrow grail
#

In the encrypt you don't have .encode() for that part, so you don't need decode

wary wolf
#

Now I can get past that line, but still having another issue

narrow grail
#

Treat the functions as opposites and reverse each action from the end - if needed, check the types.
You could also print stuff at each step in encoding and then in decoding - and compare

wary wolf
#

I'll try that and come back if I can't figure it out

hidden quiver
#

Encryption is working fine for me, it's decryption that's the issue.

I'm probably not helping here, but you don't know if the encryption is working if the decryption isn't working ๐Ÿคฃ

wary wolf
#

Yeah I guess so... haha

#

But getting no errors at least

#

I found an inconsistency but I'm not sure what it means:

#

During the first encryption it gives out b'\xf8\xe9\xabfL\x99VEa\xd0!\xb0g\xc6\x1bk',
and on the first decryption I get b"n\xb4h\x9d4\xce\xb17?\xf3\xd1\xfa\xc5\x7f'\x8f"

#

I think it's an issue with salt encoding/decoding

#

Perhaps when i put the salt in the f string

#

is it converting bytes to a string somehow?

formal pikeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.