如何在Cocoa / Objective-C中将文件编码和解码为Base64

时间:2022-12-02 23:30:06

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.

我目前正在尝试让一个小型肥皂客户端工作,其中包括在请求的xml中发送证书文件。

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.

我将文件放入NSData对象没有问题 - 但我必须将其转换为某些Base64字符串。环境是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.

我发现很多旧的帖子处理这个 - 但我发现最好的是一些使用OpenSSL库的代码,其中包含大量已弃用的方法。

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?

所以,我的问题如下:有没有比使用OpenSSL库更好的方法?如果是,您是否有一些URL或更新的代码片段?

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.

如果不是,我想有一些项目可以推荐使用Base64。毕竟Base64并不罕见。

Thanks for your help!

谢谢你的帮助!

4 个解决方案

#1


11  

Here is a base64 encoding done with CommonCrypto:

这是使用CommonCrypto完成的base64编码:

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

如果将其添加到项目中,还需要添加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);
}

#2


5  

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

如果您使用的是iOS 7或OS X 10.9 SDK,则可以使用NSData的新base64功能。

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+.

如果您使用的是较旧的SDK,只需添加此声明即可获得NSData base64编码和解码。这适用于iOS 4+和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

#3


3  

Here is a simple NSData Base64 category I wrote. It uses the plist serialization/deserialization mechanism under the hood. Also, duping radar #9896929 would be nice.

这是我写的一个简单的NSData Base64类别。它使用了引擎盖下的plist序列化/反序列化机制。此外,重复雷达#9896929会很好。

#4


2  

This works for OSX and it's ok using this with SDK's starting from 10.6 to 10.8. For 10.9 the methods have changed a bit (although at the time of writing, they work), but it's all documented on

这适用于OSX,可以使用SDK,从10.6到10.8开始。对于10.9,这些方法有所改变(虽然在撰写本文时,它们都有效),但它们都记录在案

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instm/NSData/base64Encoding

+ (NSString *) base64StringFromFileAtPath: (NSString*) filePath {
    NSData * dataFromFile = [NSData dataWithContentsOfFile:filePath];
    return [dataFromFile base64Encoding];
}


+ (NSData*) dataFrom64String : (NSString*) stringEncodedWithBase64
{
    NSData *dataFromBase64 = [[NSData alloc] initWithBase64Encoding:stringEncodedWithBase64];
    return dataFromBase64;
}

#1


11  

Here is a base64 encoding done with CommonCrypto:

这是使用CommonCrypto完成的base64编码:

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

如果将其添加到项目中,还需要添加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);
}

#2


5  

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

如果您使用的是iOS 7或OS X 10.9 SDK,则可以使用NSData的新base64功能。

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+.

如果您使用的是较旧的SDK,只需添加此声明即可获得NSData base64编码和解码。这适用于iOS 4+和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

#3


3  

Here is a simple NSData Base64 category I wrote. It uses the plist serialization/deserialization mechanism under the hood. Also, duping radar #9896929 would be nice.

这是我写的一个简单的NSData Base64类别。它使用了引擎盖下的plist序列化/反序列化机制。此外,重复雷达#9896929会很好。

#4


2  

This works for OSX and it's ok using this with SDK's starting from 10.6 to 10.8. For 10.9 the methods have changed a bit (although at the time of writing, they work), but it's all documented on

这适用于OSX,可以使用SDK,从10.6到10.8开始。对于10.9,这些方法有所改变(虽然在撰写本文时,它们都有效),但它们都记录在案

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instm/NSData/base64Encoding

+ (NSString *) base64StringFromFileAtPath: (NSString*) filePath {
    NSData * dataFromFile = [NSData dataWithContentsOfFile:filePath];
    return [dataFromFile base64Encoding];
}


+ (NSData*) dataFrom64String : (NSString*) stringEncodedWithBase64
{
    NSData *dataFromBase64 = [[NSData alloc] initWithBase64Encoding:stringEncodedWithBase64];
    return dataFromBase64;
}