5

I understand that there are similar questions, but I do not understand them so here goes: I have bytes (encoded from a string) in a string form (read from a file). When I try to decode the bytes, I get an error saying that it is a string, not bytes. I understand it is in the wrong form, but do I not have the correct information? How can I convert bytes in a string back to bytes? I will also note I know this is not a secure password method and will not be used as one. (I am using python 3)

I've done some research into how I can fix this, but I am very new and either did not understand it or could not apply it. I though this would work but it does not:

password=bytes(password, 'cp037')

Oh well. Here is a short version of the code I have:

#writing to the file
password="example" 
password=password.encode('cp037') 
password=str(password)
f=open("passwordFile.txt", "w+")
f.write(password)
f.close

#reading from the file
f=open("passwordFile.txt","r")
password=f.read()
#this is where I need to turn password back into bytes
password=password.decode('cp037') 
print(password)

I expected to get example as output, but have a error: AttributeError: 'str' object has no attribute 'decode'

9
  • 1
    which version of python are you using? Commented Nov 6, 2019 at 14:25
  • 1
    what is y when you try to decode it Commented Nov 6, 2019 at 14:28
  • 2
    What's the purpose of password=str(password)? That doesn't decode the string; it just gives you a string representation of the bytes. Commented Nov 6, 2019 at 14:29
  • 2
    You probably just want to open your file using cp037 as the encoding, and write the original password as-is, letting the file handle do the encoding for you. password = 'example'; with open("passwordFile.txt", "w+", encoding='cp037') as f: f.write(password). Commented Nov 6, 2019 at 14:30
  • 1
    @thinkCode You have a string. I'm asking why you are trying to round-trip it in the first place. Commented Nov 6, 2019 at 15:16

4 Answers 4

3

A quick possibility to convert from a string representation of bytes literals

"b'\x85\xa7\x81\x94\x97\x93\x85'"

to the actual bytes would be

bytes_as_bytes = eval(bytes_literals_as_str)

i.e.

bytes_as_bytes = eval("b'\\x85\\xa7\\x81\\x94\\x97\\x93\\x85'")
Sign up to request clarification or add additional context in comments.

1 Comment

you would almost certainly want to use ast.literal_eval instead of just eval.
2

The simplest solution is to write the password as bytes object without converting it to str.

But anyway, the problem here is you've wrote a byte codded string, so the file will contains this b'\x85\xa7\x81\x94\x97\x93\x85'.

And when you read the line, it is actually an str, if you want to decode it you need to convert the encoded byte array to an actual bytes object, ~~I don't know if there is a ready-to-use function in python that allows to do that,~~ but one possible solution is to convert the line to a Hex coded line (by removing the b' from the beginning, and all the \xs, and the last quote ').

#writing to the file
password="example" 
password=password.encode('cp037') 
password=str(password)
f=open("passwordFile.txt", "w+")
f.write(password)
f.close()

#reading from the file
f=open("passwordFile.txt","r")
password=f.read()
#this is where I need to turn password back into bytes
# -------
# we converts the string 
# from "b'\x85\xa7\x81\x94\x97\x93\x85'" 
# to "85a78194979385"
# then pass it to the bytes.fromhex to get the bytes objet.
password=bytes.fromhex(password[2:-1].replace('\\x', '')).decode('cp037')
# -------
print(password)

Comments

1

write byte, read byte and convert into string using decode

#writing to the file
password="example"
password=password.encode('cp037')
#password=str(password) (remove this line)
f=open("passwordFile.txt", "wb")
f.write(password)
f.close()

#reading from the file
f=open("passwordFile.txt","rb")
password=f.read()
#this is where I need to turn password back into bytes
y=password.decode('cp037')
print(y)

Comments

-2

Use hex() and fromhex() methods. Even if you convert to string (to simulate writing to a file, it works.)

Btw, this is just a password hashing example.

from hashlib import scrypt
from os import urandom

secret = "password"
salt = urandom(16).hex()

def hashing(secret, salt):
    return scrypt(secret.encode(), salt=salt.encode(), n=16384, r=8, p=1)

def checking(attempt, real_secret, salt):
    return real_secret == hashing(attempt, salt)

hashed = hashing(secret, salt)

# convert to hex, save it (convert to string) and convert it back to hex and then to bytes
new = hashed.hex() # convert it to hex
new = str(new) # simulate writing to a file
new = bytes.fromhex(new) # convert it back to bytes from hex/str type
print(checking(secret, new, salt)) # just testing if the conversion worked properly by checking if the passwords match

1 Comment

This is a good answer, but completely incorrect for this Q. You are encoding strings to hex strings and back, which are different from byte strings - a data type like str, int.

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.