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.fe.net 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.
"b'gAAAAABgAG4-D7Wda8gIQ6mLJNQegbfgKtnQ9nxcwJzokWGsiZMNYN2igbuUVcwMHxqZNQ-Yvd8tkTo-vWGEuOK7jgfKlGSq5A=='"
This is originally a byte value returned by the python cryptography.fe.net encrypt() function.
No, that's a representation of the repr()
of a bytestring returned by fe.net encrypt()
. It will be doubly tough to decode that (though ast.literal_eval()
would do it).
You'll need to change that encryption code to decode the bytestring back into a regular text string; since the Fe.net token is ASCII safe, you can simply do that with .decode()
.
Then, when trying to decrypt the value, you would similarly .encode('ascii')
the text string for Fe.net to consume.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.