iOS中使用Core Data进行数据加密

时间:2012-03-13 19:37:16

标签: iphone ios core-data encryption

我只需要确认一下。

使用iPhone 3GS及以上版本,使用硬件加密对写入文件系统的任何数据进行加密是否正确?只需在文件系统上创建XXX.sqlite文件,存储在其中的数据就已经加密了。

还提供了进一步的安全性NSFileProtectionComplete

感谢。

3 个答案:

答案 0 :(得分:7)

不,这不正确。您需要在sqlite文件上启用加密。创建persistentStoreCoordinator后添加以下内容:

// Make sure the database is encrypted when the device is locked
NSDictionary *fileAttributes = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];
if (![[NSFileManager defaultManager] setAttributes:fileAttributes ofItemAtPath:[storeURL path] error:&error]) {
    // Deal with the error
}

答案 1 :(得分:7)

[_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:@{ NSPersistentStoreFileProtectionKey : NSFileProtectionComplete } error:&error]

答案 2 :(得分:3)

不,你的假设不正确。

来自NSPersistentStoreCoordinator类文档:

  

对于在iOS v5.0之上或之后构建的所有应用程序,默认值为NSFileProtectionCompleteUntilFirstUserAuthentication。所有旧应用程序的默认值为NSFileProtectionNone。

要启用NSFileProtectionComplete,在调用addPersistentStoreWithType:configuration:URL:options:error:method时,需要将NSFileProtectionComplete的NSPersistentStoreFileProtectionKey添加到选项NSDictionary。

请记住,只有在用户设置了密码时才会启用此文件加密。