IOS - 如果文件太大,AES如何解密大文件,将其全部加载到内存中? [英] IOS - How do I AES decrypt a large file if the file is too large to load all of it into memory?

查看:943
本文介绍了IOS - 如果文件太大,AES如何解密大文件,将其全部加载到内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何AES加密和解密一个 NSData ,但这需要先将整个文件加载到内存中。



说我有一个50MB的加密文件叫做$ code> data.dat.enc ,如何将它解密到一个文件 data.dat 而不必首先将其全部加载到内存中。

解决方案

编辑:此代码已经被 http://github.com/rnapier/RNCryptor






RNCryptManager 是一个很好的例子,如何做到这一点。它来自 iOS5:PTL 的第11章示例代码。看看:

  +(BOOL)decryptFromStream:(NSInputStream *)fromStream 
toStream:(NSOutputStream *)toStream
密码:(NSString *)密码
错误:(NSError **)错误;

它假定盐和IV已经被添加到流中(这一切都在书中解释)。有关AES加密的一般性讨论,请参阅使用CommonCrypto使用AES进行正确加密。 p>

有关其使用的示例,请参阅 CPCryptController.m 在同一个项目中。



如果有足够的兴趣,我可以把这个对象拉出来,并支持它一个独立的项目,而不仅仅是一个示例代码。对人来说似乎是有用的。但是,并不是很难按照原样整合。



更一般的答案是你创建一个具有 CCCryptorCreate 然后调用每个块的 CCCryptorUpdate 。然后你调用 CCCryptorFinal 来完成。


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.

这篇关于IOS - 如果文件太大,AES如何解密大文件,将其全部加载到内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆