I sent an e-mail which included almost 2500 characters of text and one attached 60KiB PNG file. This text included number 185 eight times. However, after the e-mail was encoded to base64 and sent, I'm able to find only three "MTg1"("MTg1" is "185" in base64 encoding) strings from e-mail source. How to explain this? According to e-mail header, content type is "text/plain; charset=UTF-8" and content transfer encoding is "base64".
1 Answer
Base64 encoding turns 6 bytes into 8. There are 3 possible encodings for any given plaintext, shifted by several bits each time (and mixed with other encoded text, hence not trivial to find).
>>> '185'.encode('base64')
'MTg1\n'
>>> '\x00185'.encode('base64')
'ADE4NQ==\n'
>>> '\x00\x00185'.encode('base64')
'AAAxODU=\n'
>>> '\x00\x00\x00185'.encode('base64')
'AAAAMTg1\n'
-
-
1Because of how Base64 works. The first 8 bits in the plaintext become a 6-bit character plus 2 more bits in the next character. The next 8 bits become 4 bits for the current character, and 4 for the next. The third 8 bits become 2 and 6. Rinse, lather, repeat, pad with
=
when done. Aug 9, 2013 at 13:00
=
is used as padding (notice that is a 65th "digit"), and there are other side effects.