thinkCode
thinkCode

Reputation: 71

How to convert bytes in a string back to bytes

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'

Upvotes: 5

Views: 4022

Answers (4)

Victor Gulart
Victor Gulart

Reputation: 1

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

Upvotes: -2

jotelha
jotelha

Reputation: 39

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'")

Upvotes: 3

Abdelhak Bougouffa
Abdelhak Bougouffa

Reputation: 86

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)

Upvotes: 2

Venkatesh Nandigama
Venkatesh Nandigama

Reputation: 518

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)

Upvotes: 1

Related Questions