I have a file that have this format:
username password (base64 encoded) id
I have to read this password (base64 encoded) and decode it to pass as a paramater in the password to authenticate. The problem is, when I read this password it is being readed as string and I get an error when I try to decode because this is expecting to be bytes.
def getSecret(self):
home = expanduser("~/.user/credentials")
with open(home,"r") as file:
self.password = list(file)[1]
self.password = base64.b64decode(self.password)
return self.password
conn = User()
decode = base64.b64decode(conn.getSecret())
print(decode)
But this is returning a string and should be bytes, when I try to decode this i got this error
return binascii.a2b_base64(s)
binascii.Error: Incorrect padding
How can I read and decode this?
Thank you.
You have a Python string
that you want to decode:
>>> password_b64='c2VjcmV0\n'
The binascii.a2b_base64
function will do that ( NOTE: a2b
):
>>> binascii.a2b_base64(password_b64)
b'secret'
But it returns a bytes
object, not a string
object. So you have to decode the bytes somehow. The obvious way is to presume they are UTF-8, and invoke the .decode(encoding)
method on the resulting bytes
:
>>> binascii.a2b_base64(password_b64).decode("utf-8")
'secret'
I found the problem, just had to remove the b'' from the string and everything worked. Thank you very much everyone.
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.