AES256加密/解密错误+IOS SDK 7

6

我正在使用AES256进行安全加密,并以加密形式存储数据,这在IOS 6及以下版本中运作良好,但当我在IOS 7中测试我的应用程序时,我无法获得之前存储的数据。经过调试,我发现解密在IOS 7中不起作用并返回空值。

我的代码如下:

- (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;

你能帮我在IOS 7中重新获取我的数据吗?

谢谢


经过大量的调试,我发现当CCCryptorStatus cryptStatus = CCCrypt(...)函数被调用时,缓冲区将获得NSThreadWillExitNotification作为值。 - Hindu
1个回答

7
我在苹果开发者论坛上找到了解决此问题的方法。
- (NSData *)encrypt:(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)

 BOOL patchNeeded = ([key length] > kCCKeySizeAES256);
 if (patchNeeded) {
      key = [key substringToIndex:kCCKeySizeAES256]; // Ensure that the key isn't longer than what's needed (kCCKeySizeAES256)
 }

 // fetch key data
 [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

 if (patchNeeded) {
      keyPtr[0] = '\0';  // Previous iOS version than iOS7 set the first char to '\0' if the key was longer than kCCKeySizeAES256
 }

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

当然,复制粘贴相同的补丁到解密方法中。

1
我这里也有类似的结果。加密总是有效的。在iOS 8和9上解密有效,但在iOS 7上失败了。CCCrypt()中的最后一个参数在执行后为0。缓冲区仍然全部为0。我没有使用NSString *key,而是使用NSData *key。我尝试在函数调用之前打印密钥,它看起来与加密时相同。有人知道原因吗? - edwardtoday
@edwardtoday 我也一样。我建议您将其作为一个新问题提出。我也想知道为什么。 - Drakes
@edwardtoday,收到。我会在这个话题里徘徊,并尽力找到相关资料。 - Drakes

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