How do you reject non-alphabet characters in a Base64 sample payload?
b64decode with validate=True rejects characters outside the alphabet, exposing damaged or contaminated input during decoding.
Python example and interpretation
YWk= decodes to ai, while a leading question mark fails strict decoding. Successful decoding alone says nothing about provenance.
import base64,binascii
data='?YWk='
ok=base64.b64decode('YWk=',validate=True).decode()
try: base64.b64decode(data,validate=True)
except binascii.Error as exc: result=[ok,type(exc).__name__]
resultResult ['ai', 'Error']
中文
怎样严格检查样本载荷的 Base64 字符集?
b64decode 的 validate=True 会对字母表以外的字符报错,便于在解码时发现输入污染。
Python 算例与适用条件
标准文本 YWk= 解出 ai;在开头加问号会失败。Base64 只是编码,成功解码本身无法证明来源。
import base64,binascii
data='?YWk='
ok=base64.b64decode('YWk=',validate=True).decode()
try: base64.b64decode(data,validate=True)
except binascii.Error as exc: result=[ok,type(exc).__name__]
result结果 ['ai', 'Error']