为什么Python的base64.b64decode()会忽略字符串末尾的乱码?

时间:2016-05-02 07:39:21

标签: python python-2.7 encoding base64

我有一个很长的标记,我使用python的base64.b64decode()方法解码。

有效。但正如您在下面看到的,即使我在最后插入乱码字符,它也会返回相同的结果。为什么?这两个字符串不应该产生两种不同的解码结果吗?

>>> import base64
>>> token = "Ti6VXtqWYb8WVuR6m/bnDKyVqS96pvRGH9SqTsC7w1E4ZlcjCK8SDQFWRa2b0q96pAflgZTmao+CeEk9cJFVUq0MgBCBoPMUEdTLwT7AhyAa1xOQf8b9C63+DH3v2L+PqJMPSTPfWRXL5WeOPR+gFJBrAm/658phg6vzhBMNS6wgyiiLqfWUOpWyAlcMRrKu5Yq7mXaloxxFQm6HEVcvrjDVGSdsCHRB0Osby8PttEel5oqFkYq85LfNobE9VaR6Onzowru1lHnTdfEqUT5qabXaw9j9rapT4+in2N1WQt1t+XzBn1xxGLT903FOZQxkf2X7R9sGrhLXzSnBAW5q18T8ZJBsxsq3OryCgKfPEJ3x+uj0LCnoogX/gucVcZDp19HIdvelOQsD5de85U800LCDQFKatd/+VBhh4oRrnefD+6l4WRzjg1h5J2ZNgjUhCtIu6r63zFq5ef7nG60JxdTYPOT1njGfEUNAuNuBW97i98ZfhmiPOZMaINPoEFHJQRG1nMwAYCwcytn053n+7D5Dz6MZxrWwAX3/VS9fT6SduFVQ6X4HJA/+FIH8epcqAkU6M6UVm7sfQwHV/vflVAkGNQFevNwA2+u6erInPTWqL9usz4IU47ekp68xk1BBAYEqE0AKeXaZZVpYJ8CJmbAcdxvMD9+Pchi9lk6ZomzxxLKWEPGcPjFobM8bRDEVbmfP+vYfWwovy/tOo9tqkqc0sAvS5RGp9Q0SBAfBQ9c8TXuwqrDBc0OPG5TTEQQ42Cd9Ky9K2ZHldQkXOc/H0vIWBo2m5aJABvVWambd0oEzGmQHrNYzQxNSKgWSLoh7w8HrUzn9skJQGzU/igt6EOdp617ToBD5G936ByF7Rft+FGKB3jiFeEvke0Fbh3wrsr0xqP9JxL/tr8P2x29hRQauigY2MYwrt0nilET/x88="
>>> base64.b64decode("%sXXXXXXBlahBlahBlah" % (token)) == base64.b64decode(token)
True

如果我把胡言乱语的字符放在开头,那就失败了:

>>> base64.b64decode("%sXXXXXXBlahBlahBlah" % (token)) == base64.b64decode(token)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/base64.py", line 76, in b64decode
    raise TypeError(msg)
TypeError: Incorrect padding

1 个答案:

答案 0 :(得分:6)

The CPython implementation停止解释是否看到了打击垫(=)。

    if (this_ch == BASE64_PAD) {
        if ( (quad_pos < 2) ||
             ((quad_pos == 2) &&
              (binascii_find_valid(ascii_data, ascii_len, 1)
               != BASE64_PAD)) )
        {
            continue;
        }
        else {
            /* A pad sequence means no more input.
            ** We've already interpreted the data
            ** from the quad at this point.
            */
            leftbits = 0;
            break;
        }
    }

使用base64编码的字符串进行无填充的实验:

>>> base64.decodestring('YWJj')  # without a padding
'abc'
>>> base64.decodestring('YWJj' + 'XXX')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/base64.py", line 328, in decodestring
    return binascii.a2b_base64(s)
binascii.Error: Incorrect padding

>>> base64.decodestring('YWI=')  # with a padding
'ab'
>>> base64.decodestring('YWI=XXX')
'ab'