close

[Solved] AttributeError:’bytes’ object has no attribute ‘encode’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error AttributeError:’bytes’ object has no attribute ‘encode’ in Python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How AttributeError:’bytes’ object has no attribute ‘encode’ Error Occurs?

Today I get the following error AttributeError:’bytes’ object has no attribute ‘encode’ in Python.

How To Solve AttributeError:’bytes’ object has no attribute ‘encode’ Error ?

  1. How To Solve AttributeError:'bytes' object has no attribute 'encode' Error?

    To Solve AttributeError:'bytes' object has no attribute 'encode' Error  (pad * chr(pad))is bytes while problems lies with aesEncrypt(text, secKey). It has been called twice with text as str for the first time while as bytes for the second time.

Solution 1

 (pad * chr(pad))is bytes while problems lies with aesEncrypt(text, secKey). It has been called twice with text as str for the first time while as bytes for the second time.

The solution is to make sure that the input text is of str type.

Solution 2

Since the first parameter of AES.new is bytes/bytearray/memoryview, and I assume that text is already of type bytes, then we just have to convert the pad part from unicode to bytes.

text = text + (pad * chr(pad)).encode("utf-8")

To be extra safe, you may encode text conditionally before concatenating with pad.

if not isinstance(text, bytes):
    text = text.encode('utf-8')

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment