@staticmethod defb64decode_wrap(cipher_text): """ # 解决问题:python-ignore-incorrect-padding-error-when-base64-decoding Wrapper for base64.b64decode - Expects Base64 encoded ASCII data as input argument cipher_text - Returns decoded data as bytes object - Expects cipher_text uses standard Base64 place-values: A-Z; a-z; 0-9; +; /. - Robust to missing Base64 encoded characters - Will not return original data in this case - Not necessarily robust to corrupted characters in cipher_text - Throws MISSING_B64_NOT_ASCII exception if cipher_text contains non-ASCII characters; implemented via bytes.decode and/or str.encode """ try: # Ensure argument is either bytes ASCII or str ASCII ifisinstance(cipher_text, bytes): arg_string = cipher_text.decode('ASCII') elifisinstance(cipher_text, str): arg_string = cipher_text.encode().decode('ASCII') else: assertFalse except Exception: raise exceptions.MISSING_B64_NOT_ASCII( 'Method missing_b64.b64decode Base64 encoded argument is neither ASCII bytes nor ASCII str')
try: # Remove any = chars, and try padding with two =s # - This will work with (len(cipher_text') % 4) of 0, 2 or 3 # - cipher_text' is cipher_text with all non-Base64-standard-place-values removed decode_ret = base64.b64decode(arg_string.replace('=', '') + '==')
except base64.binascii.Error as e: # If that failed, padd with A== # - This will work with (len(cipher_text') % 4) of 1 decode_ret = base64.b64decode(arg_string.replace('=', '') + 'A==')
except Exception: # Execution should never get here raise
ifisinstance(decode_ret, bytes): # for py3 return decode_ret.encode() else: return decode_ret