Reading for Encrypted DB not working for iOS 10

最后都变了- 提交于 2019-12-23 06:49:42

问题


I'm working on an iOS application. Encryption & Decryption to read & write was working till iOS 9. But after upgrade to iOS 10 it started to giving issue with following message that "file is encrypted or is not a database".

For DB encryption I'm using following code:

sqlite3 *db1;
if (sqlite3_open([[self.databaseURL path] UTF8String], &db1) == SQLITE_OK) {
const char* key = [@"strong" UTF8String];
sqlite3_key(db1, key, (int)strlen(key));

   if (sqlite3_exec(db1, (const char*) "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK) {
      NSLog(@"Password is correct, or a new database has been initialized");
   } else {
      NSLog(@"Incorrect password!");
   }
  sqlite3_close(db1);
}

& it's working perfectly fine.

For opening and reading operation I'm using following code:

-(void)openDB
{
    NSString *docsDir;
    docsDir = [self getDirectoryPath];
    aPath = [docsDir stringByAppendingPathComponent: @"SQLITE_DEMO.sqlite"];
    dbpath = [aPath UTF8String];
}

Reading:

if (sqlite3_open(dbpath, &contactDBNew) == SQLITE_OK)
        {
            NSString querySQL = [NSString stringWithFormat:@"SELECT  FROM USER"];

            const char *query_stmt = [querySQL UTF8String];
            char *err;

            int check = sqlite3_exec(contactDBNew, query_stmt, NULL, NULL, &err);

            if (sqlite3_prepare_v2(contactDBNew, query_stmt, -1, &statement, NULL) == SQLITE_OK)
            { 
                 // Successfully executed.
         } else {
              // Error in execution.
        }
  }

Here it get failed while reading prepared statement with following error message: "file is encrypted or is not a database".

Please suggest what I'm missing !!


回答1:


You need to run PRAGMA key= or use sqlite3_key after every database open. In fact, it is best to open the database once at the start of your application, then close it at the end. It is very expensive to repeatedly open and close the database due to key derivation.



来源:https://stackoverflow.com/questions/40263384/reading-for-encrypted-db-not-working-for-ios-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!