data
is a normal base64 encoded string, containing only characters from the base64 character set. The problem is indeed the /
on the end, because the length of a base64 string should be dividable by 4 without remainder. So there should be padding on the end if necessary to achieve this. With the /
on the end data
is 45 characters long, which means 44 base64 characters that could be decoded to 33 bytes and then the last character which encodes only 6 bits.
Just adding padding would not solve it, because you can only add two padding characters (=
) but you need one more for the missing two bits.
So you can either cut it off like this:
lenmax = len(data) - len(data)%4
print(base64.b64decode(data[0:lenmax]).decode())
or add something like 0==
to fill it up to 48 characters. But then you would get an error in decode()
, and I'm not a friend of inventing extra data.
Or ask/check the code of the sender to find out, why there is this lonely /
on the end.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…