Search code examples
phpobjective-cbase64uuencode

objective-c to PHP websafe encoding; uuencode?


I am sending strings from my objective-c app to a PHP script over HTTP. I need to websafe these strings.

I am currently encoding with Google Toolbox for Mac GTMStringEncoding rfc4648Base64WebsafeStringEncoding and decoding with base64_decode() on the PHP end. Works great 99% of the time.

Unfortunately, this encoding is not entirely websafe as it includes some web-interpreted characters ("/" and "-"). The regular GTMStringEncoding rfc4648Base64StringEncoding also includes web-interpreted characters.

Is uuencoding the data the way to go? I see that PHP already has uudecode support, will I have top roll my own on the objective-c side?

If not uuencode, then what?


Solution

  • OK, it seems that PHP did not default support Section 5 of RFC 4648, "Base 64 Encoding with URL and Filename Safe Alphabet." This function allows PHP to handle the 4 out-lier chars before base64_decode:

    function base64url_decode($base64url) {
        $base64 = strtr($base64url, '-_', '+/');
        $plainText = base64_decode($base64);
        return ($plainText);
    }
    

    My thanks to the anonymous "Tom" who posted it on PHP.net 6 years ago.