IOS - 如果文件太大,无法加载所有的内容,如何AES解密大文件? [英] IOS - How do I AES decrypt a large file if the file is too large to load all of it into memory?

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

问题描述

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

I know how to AES encrypt and decrypt an NSData, but that requires loading the whole file into memory first.

说我有一个名为 data.dat.enc 的50mb加密文件,我如何解密它到一个文件 data.dat

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?

推荐答案

编辑:此代码已扩展 http://github.com/rnapier/RNCryptor

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

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;

它假设salt和IV已经被添加到流中了)。有关AES加密的一般性讨论,请参阅使用AES和CommonCrypto正确加密

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.

有关使用示例,请参见 CPCryptController。 m

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.

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

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天全站免登陆