Reputation: 107
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.
Upvotes: 0
Views: 70
Reputation: 107
I found the problem, just had to remove the b'' from the string and everything worked. Thank you very much everyone.
Upvotes: 0
Reputation: 15310
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'
Upvotes: 1