当我们为AES加密/解密生成密钥时,出现以下错误。
malloc: *** error for object 0x1c421b840: Invalid pointer dequeued from free list
*** set a breakpoint in malloc_error_break to debug
下面为“生成密钥”添加了代码:
- (NSData*)generateKey:(NSString*)childKey {
unsigned char saltChar[16];
for (int i=0; i<16; i++) {
saltChar[i] = 0;
}
NSData *salt = [NSData dataWithBytes:saltChar length:16];
size_t bufferSize = kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
int result = CCKeyDerivationPBKDF(kCCPBKDF2, // algorithm
childKey.UTF8String, // password
[childKey lengthOfBytesUsingEncoding:NSUTF8StringEncoding], // passwordLength
salt.bytes, // salt
salt.length, // saltLen
kCCPRFHmacAlgSHA1, // PRF
65, // rounds
buffer, // derivedKey
bufferSize*8); // derivedKeyLen
NSLog(@"Unable to create AES key for password: %d", result);
NSData *data = nil;
if (result == kCCSuccess) {
data = [[NSData alloc] initWithData:[NSData dataWithBytesNoCopy:buffer length:16]];
}
free(buffer);
return data;
}
上面的代码出了什么问题,我对此错误感到非常沮丧。请帮助任何人。
谢谢。