我创建了一个小型商业应用程序..在我的应用程序中,我用sqlite数据库来存储数据..在这里我决定使用加密方法使用安全框架..我知道关于sqlite,但我不知道如何实施sqlite加密方法...请指导我....iphone sqlite加密数据
回答
您使用NSFileProtectionComplete功能(它只适用于ios 4及更高版本)。
以下是为example.sqlite创建NSPersistentStoreCoordinator的示例,如果在ios4上使用该示例,那么它将被加密。
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"example.sqlite"];
NSURL *storeUrl = [NSURL fileURLWithPath:storePath ];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
// Handle error
}
if(RSRunningOnOS4OrBetter())
{
NSDictionary *fileAttributes = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];
if (![[NSFileManager defaultManager] setAttributes:fileAttributes ofItemAtPath:storePath error:&error]) {
// Handle error
}
}
return persistentStoreCoordinator;
}
BOOL RSRunningOnOS4OrBetter(void) {
static BOOL didCheckIfOnOS4 = NO;
static BOOL runningOnOS4OrBetter = NO;
if (!didCheckIfOnOS4) {
NSString *systemVersion = [UIDevice currentDevice].systemVersion;
NSInteger majorSystemVersion = 3;
if (systemVersion != nil && [systemVersion length] > 0) { //Can't imagine it would be empty, but.
NSString *firstCharacter = [systemVersion substringToIndex:1];
majorSystemVersion = [firstCharacter integerValue];
}
runningOnOS4OrBetter = (majorSystemVersion >= 4);
didCheckIfOnOS4 = YES;
}
return runningOnOS4OrBetter;
}
谢恩鲍威尔接受的答案是不正确的。
后设置NSFileProtectionComplete为NSFileProtectionKey addPersistentStoreWithType:配置:URL:选择:错误:没有效果,即,默认设置(NSFileProtectionCompleteUntilFirstUserAuthentication)被施加,这是不太安全。
正确的做法是设置为NSFileProtectionComplete NSPersistentStoreFileProtectionKey(注意,这关键是特定于持久性存储)中通过了该选项的参数字典...
NSDictionary *fileAttributes = @{NSPersistentStoreFileProtectionKey : NSFileProtectionComplete};
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:fileAttributes error:&error]) {
...
我测试了使用PhoneView并且能够在初始解锁后使用接受的答案方法访问锁定设备上的SQLite,但在使用我建议的方法进行初始解锁后,我无法访问锁定设备上的SQLite。
我想在Xcode 7中的iOS 9上启用sqlite数据库加密,我正在采用这种方法。但是,我总是可以看到.sqlite以及.sqlite-wal和.sqlite-shm。出于某种原因,可以随时浏览数据。我已经把我的代码放在这里了,如果你可以看看,我会非常感激:http://stackoverflow.com/questions/39151959/nsfileprotectioncomplete-doesnt-encrypt-the-core-data-file – EmbCoder 2016-08-26 13:54:20
- 1. sqlite加密/解密+ sqlcipher + iPhone
- 2. iPhone - 读取密码加密的SQLite数据库
- 3. SQLite数据库加密C#?
- 4. 核心数据SQLite加密?
- 5. iPhone SQLite密码字段加密
- 6. SQLCIPHER sqlite加密iphone ios将未加密的数据库转换为加密数据库
- 7. sqlite文件被加密或不是数据库iPhone sms.db
- 8. 如何加密/解密SQLite数据库中的数据?
- 9. 替代SQLite数据库加密
- 10. 在Delphi中加密SQLite数据库OLEDB
- 11. Sqlite数据库备份和加密
- 12. 如何加密android sqlite数据库?
- 13. 在C#中加密SQLite数据库
- 14. SQLCipher面临的iPhone SQLite加密错误
- 15. 加密和解密Sqlite文件(使用核心数据)
- 16. WinRT SQLite加密
- 17. 在iPad/iPhone上加密的数据?
- 18. 在iPhone中发送加密数据
- 19. 数据库在sqlite的iPhone
- 20. 更新iPhone SQLite数据库
- 21. iPhone中的SQLite数据库
- 22. 检索sqlite的数据,iPhone
- 23. iphone中的sqlite数据库
- 24. iPhone SQLite数据库读写
- 25. 是否可以使用Javascript来加密/解密SQLite数据库?
- 26. 无法在.NET4中加密/解密SQLite数据库
- 27. 加密/解密SQLite数据库并“动态地”使用它
- 28. 使用java加密/解密sqlite数据库
- 29. 如何使用sqlite在iphone中加密和解密pashto语言
- 30. iphone sdk:添加数据到sqlite数据库不工作?
powell感谢您的回复..... – donkarai 2011-03-02 10:11:49
但我怎么可能解密当我想从sqlite中看到我的数据库... – Jitendra 2014-03-26 10:37:10
上面的例子将自动加密/解密你的磁盘。您只需使用上述代码在应用程序中正常访问数据库即可。所以你不必做任何事情。我可以看到上述代码唯一的缺点是当设备被锁定时无法访问数据库,因为IOS在锁定时无法解密数据库。因此,如果使用NSFileProtectionComplete加密数据库,则在锁定屏幕中访问数据库的后台应用程序无法使用。 – 2014-03-26 18:11:10