我創建了一個小型商業應用程序..在我的應用程序中,我用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. SQLite數據庫加密
- 6. iPhone SQLite密碼字段加密
- 7. SQLCIPHER sqlite加密iphone ios將未加密的數據庫轉換爲加密數據庫
- 8. 加密SQLite數據庫中的密碼?
- 9. iphone安全數據加密
- 10. 如何處理iPhone中的加密sqlite數據庫?
- 11. iPhone數據加密使用後無法讀取我的sqlite
- 12. sqlite文件被加密或不是數據庫iPhone sms.db
- 13. 如何加密/解密SQLite數據庫中的數據?
- 14. 如何加密sqlite數據庫中的數據?
- 15. SQLCipher面臨的iPhone SQLite加密錯誤
- 16. 替代SQLite數據庫加密
- 17. Sqlite數據庫備份和加密
- 18. 在Delphi中加密SQLite數據庫OLEDB
- 19. 如何加密android sqlite數據庫?
- 20. 在C#中加密SQLite數據庫
- 21. 加密和解密Sqlite文件(使用核心數據)
- 22. iPhone XML數據進行加密
- 23. 在iPad/iPhone上加密的數據?
- 24. 在iPhone中發送加密數據
- 25. 檢索sqlite的數據,iPhone
- 26. iPhone上的SQLite數據庫
- 27. iphone中的sqlite數據庫
- 28. 更新iPhone SQLite數據庫
- 29. iPhone SQLite數據庫讀寫
- 30. 數據庫在sqlite的iPhone
powell感謝您的回覆..... – donkarai 2011-03-02 10:11:49
但我怎麼可能解密當我想從sqlite中看到我的數據庫... – Jitendra 2014-03-26 10:37:10
上面的例子將自動加密/解密你的磁盤。您只需使用上述代碼在應用程序中正常訪問數據庫即可。所以你不必做任何事情。我可以看到上述代碼唯一的缺點是當設備被鎖定時無法訪問數據庫,因爲IOS在鎖定時無法解密數據庫。因此,如果使用NSFileProtectionComplete加密數據庫,則在鎖定屏幕中訪問數據庫的後臺應用程序無法使用。 – 2014-03-26 18:11:10