Safe Base64 URL in Objective-C

I looked through numerous stack overflow messages on how to decode Base64 encoded strings in Objective-C, but I still have problems with it.

Essentially, I'm trying to port a Python script to Objective C. A line of Python code:

zlib.decompress(base64.urlsafe_b64decode(string)) 

When I run the "string" through the base64.urlsafe_b64decode version in Python, it comes out correctly and can be correctly generated by Zlib. When I run a β€œstring” on any variant of the Objective-C Base64 decoder, it performs several kinds of work, but the results do not match, and the Zlib decompression fails.

Is there a difference between Base 64-safe decoding and code that is widely available here on SO? If someone has experienced such difficulties before, then any understanding of what to do is appreciated.

Thank you SO!

Edit 1: I used the Base 64 code found here . The source data (still Base64 encoded) can be found here , the generated Objective-C can be found here , and the Python created can be found here . Ideally, I want Objective-C code to decode with the same text as the Python script.

+4
source share
2 answers

Special thanks to Graham for pointing out differences in RFC, I was able to solve this problem. If someone comes across this in the future, here's how to solve it:

  • Download NSData + Base64 code from here .
  • In NSData+Base64.m you need to change the lookup tables to the following:
 // // Mapping from 6 bit pattern to ASCII character. // static unsigned char base64EncodeLookup[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; // // Definition for "masked-out" areas of the base64DecodeLookup mapping // #define xx 65 // // Mapping from ASCII character to 6 bit pattern. // static unsigned char base64DecodeLookup[256] = { xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 62, xx, xx, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, xx, xx, xx, xx, xx, xx, xx, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, xx, xx, xx, xx, 63, xx, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, }; 

This will make Objective-C decoding equivalent to Python.

+12
source

iOS 7.0 and later have the "base64EncodedStringWithOptions" method in NSData, which will do the above search. But this method does not return a string with safe URL encoding. Therefore, you need to manually perform the replacement to get a string with safe URL encoding. See below..

 NSData *originalData = [originalString dataUsingEncoding:NSUTF8StringEncoding]; NSString *base64String = [originalData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; base64String = [base64String stringByReplacingOccurrencesOfString:@"/" withString:@"_"]; base64String = [base64String stringByReplacingOccurrencesOfString:@"+" withString:@"-"]; 

The result of base64String is now URL safe.

+6
source

All Articles