Archived Forum Post

Index of archived forum posts

Question:

Minimize Encryption Memory Usage on iOS?

Dec 06 '12 at 14:46

We purchased a developer license for ChilKat and we're using it in an iOS app. We frequently have to encrypt large files that are first opened for viewing in memory.

If a file has 24 MB, when we encrypt it using

CkoCrypt2* encryptCrypt = [self newCkoCrypt2];

// Set the key. [encryptCrypt SetEncodedKey:key encoding: @"base64"];

// Set the IV [encryptCrypt SetEncodedIV:iv encoding: @"base64"];

NSFileHandle* file = [NSFileHandle fileHandleForWritingAtPath:toPath]; [file writeData:[encryptCrypt EncryptBytes:data]];

we end up using 24 + 24 x 2 = 24 + 48 MB.. which is a lot of memory for an iPad 1.

It would be very nice to encrypt from memory (NSData) to a file (Path) and this way we would only use memory for the crypto's buffer and the original data.

Can we somehow achieve this?


Answer

I checked the memory usage for the C++ case, and if 24MB is encrypted, then the max memory usage is 48MB. The Chilkat Objective-C implementation is a thin wrapper around the C++ libs. Here's what the EncryptBytes wrapping looks like:

// method: EncryptBytes
- (NSData *)EncryptBytes: (NSData *)bData
{
    CkByteData arg1;
    arg1.append2([bData bytes],[bData length]);
    CkByteData bd;
    bool b = ((CkCrypt2 *)m_obj)->EncryptBytes(arg1,bd);
    if (!b) return nil;
    return [NSData dataWithBytes:bd.getData() length:bd.getSize()];
}

I don't know of anything that can be done to reduce the amount of memory. One option might be to use the Chilkat C++ libs for iOS in your app to avoid the extra copy to NSData. The CkByteData object has a method for saving the contents directly to a file.