I would like to have a file encryption function for my iphone application. For desktop applications, I used the following function to encrypt relatively small files:
- (NSData *)aesEncrypt:(NSString *)key {
char keyPtr[kCCKeySizeAES256+1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL ,
[self bytes], dataLength,
buffer, bufferSize,
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer);
return nil;
}
But I do not think that this code can be used on iphone. If I try to encrypt a 5 MB file, it will occupy at least 10 MB in memory, as it will be loaded into NSData and returned as such. Is there a method that would eccrypt a file by reading small blocks and writing output to another file? Or am I mistaken in this, taking so m
source
share