I know how to AES encrypt and decrypt an NSData
, but that requires loading the whole file into memory first.
Say I have a 50mb encrypted file called data.dat.enc
, how can I decrypt it to a file data.dat
without having to first load it all into memory?
EDIT: This code has been expanded by http://github.com/rnapier/RNCryptor.
RNCryptManager is a good example of how to do this. It comes from the Chapter 11 sample code of iOS5:PTL. Look at:
+ (BOOL)decryptFromStream:(NSInputStream *)fromStream
toStream:(NSOutputStream *)toStream
password:(NSString *)password
error:(NSError **)error;
It assumes that the salt and IV have been prepended to the stream (this is all explained in the book). For some more general discussion on AES encryption, see Properly encrypting with AES with CommonCrypto.
For an example of its use, see CPCryptController.m in the same project.
If there's sufficient interest, I could pull this object out and support it as a stand-alone project rather than just as a piece of sample code. It seems reasonably useful to people. But it's not that difficult to integrate as-is.
The more general answer is that you create a cryptor with CCCryptorCreate
and then make calls to CCCryptorUpdate
for each block. Then you call CCCryptorFinal
to finish things up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With