Reputation: 171
I am using AES 256 for Encryption Decryption, I have bigger files about 1 - 2 GB it should go through Encryption and Decryption Process , while doing encryption of downloaded files I receives the memory warning and app crashes. So I want to know how do I do encryption decryption chunk by chunk to reduce the memory usage.
Following is the code snip which I am using:-
- (NSData *)AES256EncryptWithKey:(NSString *)key
{
char keyPtr[kCCKeySizeAES256 + 1];
bzero( keyPtr, sizeof( keyPtr ) );
// fetch key data
[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;
}
- (NSData *)AES256DecryptWithKey:(NSString *)key
{
char keyPtr[kCCKeySizeAES256+1];
bzero( keyPtr, sizeof( keyPtr ) );
// fetch key data
[key getCString:keyPtr maxLength:sizeof( keyPtr ) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc( bufferSize );
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt( kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL ,
[self bytes], dataLength,
buffer, bufferSize,
&numBytesDecrypted );
if( cryptStatus == kCCSuccess )
{
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free( buffer );
return nil;
}
Thanking You, Rohit Jankar
Upvotes: 2
Views: 3694
Reputation: 3653
Actual reason of crash is you are loading large amount of data in memory. You are doing one shot encryption i.e CCCrypt
is a oneshot encryption function. Instead of this you have to use stream or some other way like divide your data into small chunk and encrypt data chunk by chunk.
Here You can find more about implementation : Encrypt big file
Upvotes: 1
Reputation: 1208
I too came across this situation in one of my project.
Approach I have followed:
I encrypt only few bytes from the file may be only the header/first 100 bytes etc.While decrypting I decrypt only the specific number of bytes. Thus avoiding to encrypt & decrypt complete file. If the file is theft by any means it cannot be viewed as header/begin part of the file is encrypted.Though the remaining range of the file can be decrypted, it is useless & incomplete.
Moreover it depends on the type of file & client as well. I also heard one of my colleague saying that we can load bytes in chunks and encrypt them. I am not sure how exactly in terms of coding.
This is just to give you an idea of what i have done which may match your scenario as well.
TNQ
Upvotes: 1