Two Base64 encodings yield same decoding

base64, decoding, encoding, objective-c

Solution

It's clear that the encoded strings have patterns similar where they correspond to alphanumeric characters and different where they corresponds to line breaks. So the difference is because somewhere along the "Encode"->"Decode" way the software processes line breaks (CR (\r), LF(\n) or CRLF(\r\n)) differently and that's why you have such results.

Other than that there're no two different ways to encode a given string into Base64 and no two different ways to decode a valid Base64-encoded data.

Problem

Is it expected behavior that two encodings can map to the same decoding? I'm trying to troubleshoot a digital signature issue by doing sanity checks on base64-encoded intermediate strings. For example, the following base64 encoding: ``` R0VUDQoNCg0KRnJpLCAwNCBTZXAgMjAwOSAxMTowNTo0OSBHTVQrMDA6MDANCi8= ``` and: ``` R0VUCgoKRnJpLCAwNCBTZXAgMjAwOSAxMDozMzoyOCBHTVQrMDA6MDAKLw== ``` both decode to: ``` GET Fri, 04 Sep 2009 11:05:49 GMT+00:00 / ``` (With the characters escaped, this is: `GET\n\n\n Fri, 04 Sep 2009 11:05:49 GMT+00:00\n/`) The first encoding comes from testing two online base64 encoders. The second encoding comes from an Objective-C base64 encoder available here. Is there something wrong with the result I'm generating with the Obj-C encoder?

Original source