Python 3.5 base64 decoding seems to be incorrect?

Viewed 836

In Python 3.5 the base64 module has a method, standard_b64decode() for decoding strings from base64, which returns a bytes object.

When I run base64.standard_b64decode("wc==") the output is b\xc1. When you base64 encode "\xc1", you get "wQ==". It looks like there is an error in the decoding function. Actually, I think "wc==" is an invalid base64 encoded string, by this reasoning:

  1. wc== ends with ==, which means that it was produced from a single input byte.

  2. The corresponding values of 'w' and 'c' in the regular base64 alphabet are, respectively, 48 and 28, meaning their 6-bit representations are, respectively, 110000 and 011100.

  3. Concatenating these, the first 8 bits are 11000001, which is \xc1, but the remaining bits (1100) are non-zero, so couldn't have been produced by the padding process performed during base64 encoding, as that only appends bits with value 0, which means these extra 1 bits can't have been produced through valid base64 encoding -> the string is not a valid base64 encoded string.

I think this is true for any 4 character chunk of base64 encoding ending in == when any of the last 4 bits of the second character are 1.

I'm pretty convinced that this is right, but I'm rather less experienced than the Python developers.

Can anyone confirm the above, or explain why it's wrong, if indeed it is?

1 Answers
Related