加密iphone-sdk上的文件

Mar*_*ius 5 iphone encryption cocoa objective-c

我想为我的iphone应用程序提供文件加密功能.对于基于桌面的应用程序,我使用下面的函数来加密相对较小的文件:

- (NSData *)aesEncrypt:(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 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;
}
Run Code Online (Sandbox Code Playgroud)

但我不认为这个代码可以用在iPhone上.如果我尝试加密一个5mb的文件,它将占用至少10mb的内存,因为它将被加载到NSData并返回原样.有没有一种方法可以通过读取小块并将输出写入另一个文件来加密文件?或者我错了这样做

Hac*_*ord 1

尝试 RNCryptor https://github.com/rnapier/RNCryptor

我已经在我的应用程序中成功使用了它。