iOS 5:数据加密AES-256 EncryptWithKey:未找到

13

问题涉及iOS5应用程序。我有一个视图控制器,其中包含一些UITextFields。

我想使用AES-256加密数据。

事实上,我不知道做加密和解密需要添加哪些必备的包。我已经阅读过其他帖子,但是过多的解释让我感到困惑。

请告诉我需要包括哪些包、头文件才能使用AES-256加密数据。

Chandra


请查看此链接:https://dev59.com/xHM_5IYBdhLWcg3wZSE6 - Adam
@Adam:未列出要包含的软件包和头文件。 - Chandu
这个答案提供了完整的解决方案:https://dev59.com/xHM_5IYBdhLWcg3wZSE6#5078432 - Adam
1个回答

39

请参考以下类别。

常见问题:什么是类别?

简而言之,Cocoa API用于添加方法, 简要扩展类。

更多信息,请查看以下链接:

自定义现有类

类别

文件 - 新建 - Cocoa Touch - Objective-C类别

如果您想使用一个类别,您的类需要添加 #import "NSData+Encryption.h"。

输入图像描述

输入图像描述

//NSData+Encryption.h

@interface NSData (Encryption)
- (NSData *)AES256EncryptWithKey:(NSString *)key;
- (NSData *)AES256DecryptWithKey:(NSString *)key;
@end

//NSData+Encryption.m

#import "NSData+Encryption.h"
#import <CommonCrypto/CommonCryptor.h>

@implementation NSData (Encryption)
- (NSData *)AES256EncryptWithKey:(NSString *)key {
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
    
    // fetch key data
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    
    NSUInteger dataLength = [self length];
    
    //See the doc: For block ciphers, the output size will always be less than or 
    //equal to the input size plus the size of one block.
    //That's why we need to add the size of one block here
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                          keyPtr, kCCKeySizeAES256,
                                          NULL /* initialization vector (optional) */,
                                          [self bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        //the returned NSData takes ownership of the buffer and will free it on deallocation
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }
    
    free(buffer); //free the buffer;
    return nil;
}

- (NSData *)AES256DecryptWithKey:(NSString *)key {
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
    
    // fetch key data
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    
    NSUInteger dataLength = [self length];
    
    //See the doc: For block ciphers, the output size will always be less than or 
    //equal to the input size plus the size of one block.
    //That's why we need to add the size of one block here
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    
    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                          keyPtr, kCCKeySizeAES256,
                                          NULL /* initialization vector (optional) */,
                                          [self bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesDecrypted);
    
    if (cryptStatus == kCCSuccess) {
        //the returned NSData takes ownership of the buffer and will free it on deallocation
        return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
    }
    
    free(buffer); //free the buffer;
    return nil;
}
@end

这很聪明,或者有没有一种直接使用NSStrings的方法,而不涉及NSData? - Esqarrouth
我无法表达我对这个解决方案的印象有多深刻。 - Brandon A

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接