Reposting this question. I am not asking about the 'b' in a string literal or how to get rid of it.
I have a string like this:
"b'gAAAAABgAG4-D7Wda8gIQ6mLJNQegbfgKtnQ9nxcwJzokWGsiZMNYN2igbuUVcwMHxqZNQ-Yvd8tkTo-vWGEuOK7jgfKlGSq5A=='"
This is originally a byte value returned by the python cryptography.fernet encrypt() function.
I need to pass this value to the decrypt() function that takes in a byte value. How do I convert this string back to a type of byte?
I have read all the similar decoding posts on string to base64 on stackoverflow but they don't address my question specifically.
I have tried various methods like string.encode("ascii") and bytes(string, 'utf-8') but I just end up with something like this:
b"b'gAAAAABgAG3XMnAaKN3H2y1bC-j08i8ONFwzG0SKeRyiM9dnfEo4ojegCxrY3DQB0Hf9kyM3fUId8ZZk_eQkX3GwAdboIMtk2A=='"
I have also tried the following
import base64
byt = base64.decodebytes("b'gAAAAABgAG4-D7Wda8gIQ6mLJNQegbfgKtnQ9nxcwJzokWGsiZMNYN2igbuUVcwMHxqZNQ-Yvd8tkTo-vWGEuOK7jgfKlGSq5A=='")
But I ended up with an error "TypeError: expected bytes-like object, not str".
I have this problem because I want to encrypt a column of data in pandas, then write it to an Excel file along with other unencrypted columns. When I read it back later, the encrypted column ends up as a string and I get the error "TypeError: token must be bytes" when I decrypt the values.
bytes_data = eval(byte_string)eval, as it can run any arbitrary code.