如何测试 Core Data SQLite 文件是否加密?
How to test if Core Data SQLlite file is encrypted?
核心数据似乎在设备锁定时默认加密,但仅在第一次解锁之前。
For apps built for iOS 5.0 or later, persistent stores now store data
by default in an encrypted format on disk. The default protection
level prevents access to the data until after the user unlocks the
device for the first time.
所以我将其设置为在设备锁定时进行加密。 SQLite 文件的加密设置是在返回 _persistentStoreCoordinator 之前设置的,如下所示:
NSDictionary *fileAttributes = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];
if (![[NSFileManager defaultManager] setAttributes:fileAttributes ofItemAtPath:storeURL.path error:&error]) {
//Handle error
}
return _persistentStoreCoordinator;
代码来自 here.
我想测试一下文件是否真的加密了
我所做的是锁定设备并使用 Xcode->Window->Devices 下载应用程序容器。但是该文件未显示在容器中。如果我在设备解锁时做同样的事情,我就可以在容器中找到它。这是为什么?更重要的是,当 phone 被锁定或文件丢失时,我可以测试文件是否被加密就足够了。
编辑:根据此answer建议更好地为核心数据设置加密是:
NSDictionary *storeOptions = @{NSPersistentStoreFileProtectionKey: NSFileProtectionComplete};
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:storeOptions error:&error]){
//handle error
}
What I did is lock the device and download the app container using
Xcode->Window->Devices. However the file is not shown in the
container. If I do the same when the device is unlocked I can then
find it in the container.
据我了解,这就是 Apple 加密的工作原理。当您的设备被锁定时,您在设备容器中看不到加密文件,当它打开时您会看到它们。
核心数据似乎在设备锁定时默认加密,但仅在第一次解锁之前。
For apps built for iOS 5.0 or later, persistent stores now store data by default in an encrypted format on disk. The default protection level prevents access to the data until after the user unlocks the device for the first time.
所以我将其设置为在设备锁定时进行加密。 SQLite 文件的加密设置是在返回 _persistentStoreCoordinator 之前设置的,如下所示:
NSDictionary *fileAttributes = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];
if (![[NSFileManager defaultManager] setAttributes:fileAttributes ofItemAtPath:storeURL.path error:&error]) {
//Handle error
}
return _persistentStoreCoordinator;
代码来自 here.
我想测试一下文件是否真的加密了
我所做的是锁定设备并使用 Xcode->Window->Devices 下载应用程序容器。但是该文件未显示在容器中。如果我在设备解锁时做同样的事情,我就可以在容器中找到它。这是为什么?更重要的是,当 phone 被锁定或文件丢失时,我可以测试文件是否被加密就足够了。
编辑:根据此answer建议更好地为核心数据设置加密是:
NSDictionary *storeOptions = @{NSPersistentStoreFileProtectionKey: NSFileProtectionComplete};
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:storeOptions error:&error]){
//handle error
}
What I did is lock the device and download the app container using Xcode->Window->Devices. However the file is not shown in the container. If I do the same when the device is unlocked I can then find it in the container.
据我了解,这就是 Apple 加密的工作原理。当您的设备被锁定时,您在设备容器中看不到加密文件,当它打开时您会看到它们。