如何在 iPhone 上加密 CoreData 内容 [英] How can I encrypt CoreData contents on an iPhone

查看:21
本文介绍了如何在 iPhone 上加密 CoreData 内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些信息想以静态加密方式存储在 iPhone 应用程序上.我是 iPhone 开发的新手,有些我不太熟悉 CoreData 以及它如何与视图集成.我有 JSON 格式的数据,但我可以轻松地将它放入 SQLITE3 数据库或任何其他支持数据格式中.我将采用最简单的 (a) 加密和 (b) 与 iPhone 视图层集成.

I have some information I'd like to store statically encrypted on an iPhone application. I'm new to iPhone development, some I'm not terribly familiar with CoreData and how it integrates with the views. I have the data as JSON, though I can easily put it into a SQLITE3 database or any other backing data format. I'll take whatever is easiest (a) to encrypt and (b) to integrate with the iPhone view layer.

每次启动应用程序时,用户都需要输入密码来解密数据.加密的目的是在用户丢失手机时防止数据被访问.

The user will need to enter the password to decrypt the data each time the app is launched. The purpose of the encryption is to keep the data from being accessible if the user loses the phone.

出于速度原因,我更愿意一次加密和解密整个文件,而不是加密数据库每一行中的每个单独字段.

For speed reasons, I would prefer to encrypt and decrypt the entire file at once rather than encrypting each individual field in each row of the database.

注意:这不是问题 929744,其中的目的是防止用户弄乱或看到数据.数据在使用时应该完全透明.

Note: this isn't the same idea as Question 929744, in which the purpose is to keep the user from messing with or seeing the data. The data should be perfectly transparent when in use.

另请注意:我愿意使用 SQLCipher 存储数据,但更愿意使用 iPhone/CoreData 框架上已经存在的东西,而不是经历冗长的构建/集成过程.

Also note: I'm willing to use SQLCipher to store the data, but would prefer to use things that already exist on the iPhone/CoreData framework rather than go through the lengthy build/integration process involved.

推荐答案

您可以通过将 Core Data 模型实体中的单个属性设为 可转换属性,然后创建一个 NSValueTransformer 子类,该子类将加密和解密该属性的数据.虽然这不是您正在寻找的整个数据库解密,但与将整个数据库解密到内存中相比,它的内存占用要低得多.此外,它将允许延迟完成解密,而不是全部预先完成,因此您的应用程序加载速度会快得多.根据使用的加密,我什至希望加载每个实体的磁盘数据访问会比属性的解密过程慢,因此在访问属性时您不会看到太多的性能损失.

You can encrypt individual properties in your Core Data model entities by making them transformable properties, then creating an NSValueTransformer subclass which will encrypt and decrypt the data for that property. While this is not the whole-database decryption that you're looking for, it will have a much lower memory footprint than decrypting an entire database into memory. Additionally, it will allow the decryption to be done lazily, rather than all up front, so your application will load much faster. Depending on the encryption used, I would even expect that the on-disk data accesses for loading each entity would be slower than the decryption process for the properties, so you won't see that much of a performance penalty when accessing the properties.

像这样的可转换属性非常易于使用,因为您可以正常读取和写入它们,而加密/解密在幕后进行.

Transformable properties like this are very easy to use, because you read and write to them as normal, while the encryption / decryption goes on behind the scenes.

这篇关于如何在 iPhone 上加密 CoreData 内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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