0

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.

3
  • You can use eval(): bytes_data = eval(byte_string) Commented Jan 15, 2021 at 12:00
  • 1
    @בנימיןכהן Never recommend eval, as it can run any arbitrary code. Commented Jan 15, 2021 at 12:02
  • How did you end up with the string representation of a bytes object, rather than the bytes themselves? You should fix that Commented Jan 15, 2021 at 12:02

1 Answer 1

2

"b'gAAAAABgAG4-D7Wda8gIQ6mLJNQegbfgKtnQ9nxcwJzokWGsiZMNYN2igbuUVcwMHxqZNQ-Yvd8tkTo-vWGEuOK7jgfKlGSq5A=='"

This is originally a byte value returned by the python cryptography.fernet encrypt() function.

No, that's a representation of the repr() of a bytestring returned by fernet 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 Fernet 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 Fernet to consume.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. I managed to use ast.literal_eval() to convert "b'gAAAAABgAG4-D7Wda8gIQ6mLJNQegbfgKtnQ9nxcwJzokWGsiZMNYN2igbuUVcwMHxqZNQ-Yvd8tkTo-vWGEuOK7jgfKlGSq5A=='" back into a regular text string and using that I was able to decrypt the value. If you have any advice about how to properly save and load a pandas dataframe with an encrypted column of values, along with other unencrypted columns of values, to an Excel file and back, I'd love to hear. I simply did a to_excel() because it seems most logical to me but not sure if it is good practice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.