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
    As Ignacio implies, MIME base64 is not (quite) a real numbering system. It is not actually converting a value from, eg, ascii decimal to what might be considered a mathematically valid base 64 representation. The conversion is on sequences of bytes, which is why = is used as padding (notice that is a 65th "digit"), and there are other side effects.
    – goldilocks
    Aug 9 '13 at 12:43

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')
>>> '\x00185'.encode('base64')
>>> '\x00\x00185'.encode('base64')
>>> '\x00\x00\x00185'.encode('base64')
  • Why there are 3 possible encodings for any given plaintext?
    – Martin
    Aug 9 '13 at 12:57
  • 1
    Because 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 '13 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.