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

后端 未结 4 1948
庸人自扰
庸人自扰 2020-12-30 11:26

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

4条回答
  •  清歌不尽
    2020-12-30 12:33

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

提交回复
热议问题