2011-03-02 101 views
0

我创建了一个小型商业应用程序..在我的应用程序中,我用sqlite数据库来存储数据..在这里我决定使用加密方法使用安全框架..我知道关于sqlite,但我不知道如何实施sqlite加密方法...请指导我....iphone sqlite加密数据

回答

1

您使用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; 
} 
+0

powell感谢您的回复..... – donkarai 2011-03-02 10:11:49

+0

但我怎么可能解密当我想从sqlite中看到我的数据库... – Jitendra 2014-03-26 10:37:10

+0

上面的例子将自动加密/解密你的磁盘。您只需使用上述代码在应用程序中正常访问数据库即可。所以你不必做任何事情。我可以看到上述代码唯一的缺点是当设备被锁定时无法访问数据库,因为IOS在锁定时无法解密数据库。因此,如果使用NSFileProtectionComplete加密数据库,则在锁定屏幕中访问数据库的后台应用程序无法使用。 – 2014-03-26 18:11:10

2

谢恩鲍威尔接受的答案是不正确的。

后设置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。

+0

我想在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