2011-03-02 66 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