如何连接code和德code文件为Base64可可/ Objective-C的 [英] How to encode and decode Files as Base64 in Cocoa / Objective-C

查看:178
本文介绍了如何连接code和德code文件为Base64可可/ Objective-C的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在试图让一个小香皂客户端的工作,其中包括对请求的XML内发送证书文件。

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.

我没有麻烦的文件到NSData对象 - 但我不得不把它转换为Base64编码的一些字符串。环境是Mac OSX上,X code 4.3。

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.

我发现了很多旧的张贴处理的 - 但我找到了最好的是一些code,使得使用OpenSSL库和含德precated方法负载其中

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库?
如果是的话,你或许有一些网址或更近code下脚料?

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?

如果没有,我想有一些项目在那里它与可推荐的Base64交易。毕竟Base64是不是少见。

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.

感谢您的帮助!

推荐答案

下面是CommonCrypto做了base64编码:

Here is a base64 encoding done with CommonCrypto:

这是很容易code,它不会是很难把它在一个类别

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

如果您添加到你的项目,你也需要添加Security.framework

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);
}

这篇关于如何连接code和德code文件为Base64可可/ Objective-C的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆