# source: https://stackoverflow.com/questions/44164829/base64-decode-specific-string-incorrect-padding-with-correct-padding # We use hexlify for debugging. import binascii # We use the Base64 library. import base64 # Base64 works on multiples of 4 characters.. # ..Sometimes we get 3/2/1 characters and it might be midway through another. def relaxed_decode_base64(data): # If there is already padding we strim it as we calculate padding ourselves. if '=' in data: data = data[:data.index('=')] # We need to add padding, how many bytes are missing. missing_padding = len(data) % 4 # We would be mid-way through a byte. if missing_padding == 1: data += 'A==' # Jut add on the correct length of padding. elif missing_padding == 2: data += '==' elif missing_padding == 3: data += '=' # Actually perform the Base64 decode. return base64.b64decode(data) # Debugging print(str(relaxed_decode_base64('46oWrWpy2gTEGwNnN6Ayy')) + '\n')
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question