UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0x89 in position 1: invalid start byte

Encoding and decoding in Python

Teri Radichel
Bugs That Bite

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

⚙️ Check out my series on Automating Cybersecurity Metrics | Code.

🔒 Related Stories: Bugs | Application Security | Secure Code

💻 Free Content on Jobs in Cybersecurity | ✉️ Sign up for the Email List

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Encoding and decoding values can get confusing.

This sample code might help you out. I named the variables to show whether they were encoded (bytes) or decoded (a string) or base64 encoded or decoded.

import base64value="Decoded string value"value_encode = value.encode('utf-8')
value_base64encode = base64.b64encode(value_encode)
value_decode = value_base64encode.decode('utf-8')
print(value_decode)value_encode = value_decode.encode('utf-8')
value_base64decode = base64.b64decode(value_encode)
value_decode = value_base64decode.decode('utf-8')
print(value_decode)

Sometimes you encode values so they don’t accidentally get processed incorrectly. For example if you have code in a value and you don’t want it to mess up parsing and processing, you might encode it to prevent…

--

--

Teri Radichel
Bugs That Bite

CEO 2nd Sight Lab | Penetration Testing & Assessments | AWS Hero | Masters of Infosec & Software Engineering | GSE 240 etc | IANS | SANS Difference Makers Award