Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode and decode Files as Base64 in Cocoa / Objective-C

I am currently trying to get a small soap client to work, which includes to send a certificate file within the xml of the request.

I have no trouble getting the file into an NSData object - but then I have to convert it to some Base64 String. Environment is Mac OSX, Xcode 4.3.

I have found a lot of older posting dealing with that - but the best I found was some code that made use of OpenSSL libs and where containing loads of deprecated methods.

So, my question is as follows: Is there a better way than to use the OpenSSL libs? If yes, do you perchance have some URL or more recent code scraps?

If no, I guess there is some project out there which deals with Base64 that can be recommended. After all Base64 is not that uncommon.

Thanks for your help!

like image 619
Kai Mattern Avatar asked Jul 08 '12 21:07

Kai Mattern


People also ask

How do I decode a file with Base64?

To decode a file with contents that are base64 encoded, you simply provide the path of the file with the --decode flag. As with encoding files, the output will be a very long string of the original file. You may want to output stdout directly to a file.

Can you Base64 encode a file?

Base64 encoding uses printable characters to encode binary data. This enables another interesting use case: You can encode multiline files into Base64 strings.

Is BTOA a Base64?

The btoa() method creates a Base64-encoded ASCII string from a binary string (i.e., a string in which each character in the string is treated as a byte of binary data).


2 Answers

Here is a base64 encoding done with CommonCrypto:

it is very easy code, it would not be difficult to put it in a category

if you add this to your project you need also to add the Security.framework

#include <CoreFoundation/CoreFoundation.h>
#include <Security/Security.h>

static NSData *base64helper(NSData *input, SecTransformRef transform)
{
    NSData *output = nil;

    if (!transform)
        return nil;

    if (SecTransformSetAttribute(transform, kSecTransformInputAttributeName, input, NULL))
        output = (NSData *)SecTransformExecute(transform, NULL);

    CFRelease(transform);

    return [output autorelease];
}

NSString *base64enc(NSData *input)
{
    SecTransformRef transform = SecEncodeTransformCreate(kSecBase64Encoding, NULL);

    return [[[NSString alloc] initWithData:base64helper(input, transform) encoding:NSASCIIStringEncoding] autorelease];
}

NSData *base64dec(NSString *input)
{
    SecTransformRef transform = SecDecodeTransformCreate(kSecBase64Encoding, NULL);

    return base64helper([input dataUsingEncoding:NSASCIIStringEncoding], transform);
}
like image 139
denis2342 Avatar answered Nov 15 '22 23:11

denis2342


If you are using the iOS 7 or OS X 10.9 SDK, you can use the new base64 capabilities of NSData.

If you are using an older SDK, just add this declaration to get NSData base64 encoding and decoding. This will work on iOS 4+ and OS X 10.7+.

#ifndef __IPHONE_7_0
@interface NSData (NSDeprecated)
- (id)initWithBase64Encoding:(NSString *)base64String NS_DEPRECATED(10_6, 10_9, 4_0, 7_0);
- (NSString *)base64Encoding NS_DEPRECATED(10_6, 10_9, 4_0, 7_0);
@end
#endif
like image 40
0xced Avatar answered Nov 15 '22 23:11

0xced