AES256加密/解密错误+;IOS SDK 7
我使用AES256进行安全保护,并以加密形式存储数据,这在IOS 6及以下版本中运行良好,但当我在IOS 7中测试我的应用程序时,我没有得到以前存储的数据。调试后,我发现解密不起作用的是IOS 7,返回空白 我的代码如下:AES256加密/解密错误+;IOS SDK 7,ios,security,encryption,ios7,Ios,Security,Encryption,Ios7,我使用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[kCCK
- (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作为值。我在这里得到了类似的结果。加密总是有效的。解密在iOS 8和iOS 9中工作,但在iOS 7上失败。执行后,
CCCrypt()
中的最后一个参数为0。缓冲区保持为0。我使用的不是NSString*键
,而是NSData*键
。我试图在函数调用之前打印密钥,它看起来与加密时完全相同。有人有线索吗?@edwardtoday,我也是。我建议把这当作一个新问题来问。“我也想知道为什么。”爱德华今天承认。我将在线程周围徘徊,并查看在这一端可以找到什么。
- (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;
}