Reading for Encrypted DB not working for iOS 10

Hi, 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 !!

@shubhapatnim86 have you reviewed and implemented all of the guidance from the Important Advisory: SQLCipher with Xcode 8 and new SDKs?

Ok. I am having same issue.
Issue

My database is actually encrypted. But if i am going to perform any operation after encryption than got error which i send you above post.

Thanks for sharing, so encryption is working as expected. I cross checked to open encrypted file in SqliteManager then it ask for Encryption key and proceed.

We checked in application & there it allow to open & establish connection but FAILED to perform DB operation, this not even allow Read operation.

So even my application compatible to iOS-8 & iOS-10 but it getting crash. Surprisingly it’s work for iOS-10.

This is all happened after upgrade from “X-Code 7 & iOS-9” to “X-Code 8 & iOS-10”.

@Nirav_Patel @shubhapatnim86 - neither of you answered the rather important question of whether you implemented all of the guidance from the SQLCipher with Xcode 8 and new SDKs Advisory. Can you please review that and make sure you have addressed all the guidance there?

@sjlombardo - I have uploaded the demo which i have created for sqlcipher in xcode 8 and ios 10.
But getting issue while performing any DB operation. Please check the demo and let me know if any mistake on my side.

Also, i have follow the steps which you mentioned SQLCipher with Xcode 8 and new SDKs Advisory.

Demo : SqlCipher.git

1 Like

@sjlombardo Did you please check in sample created by Nirav, It’s same issue.

@Nirav_Patel @shubhapatnim86 - I took a brief look at the example, but it doesn’t build cleanly. Furthermore, a review of the compiler flags, linker settings, and search paths show that it is not setup consistently with the guidelines I posted earlier in this thread, or the instructions for including the source version with Xcode (e.g. with respect to settings at the project level). It would be helpful if you verified the configuration of the project.

Hi, I had the same error message which appears only on Simulator with iOS10, on iOS10 device is everything ok.
I’ve exactly followed your Advisory - added -lSQLCipher to Other Link flags and ensured in runtime that SQLCipher is active (actually the runtime check told me YES even before any changes to linker flags).

So it looks to me like iOS10 related issue but not the one is mentioned above and in Advisory.
Do you have any hints where to look?

The crash with “file is encryped or not a database” happens after specifying database password and trying to check if it’s right by following http://sqlcipher.net/sqlcipher-api/#key

@appmagnetics - Are you saying that you are able to successfully get a value back from PRAGMA cipher_version, but when you key the database it is not working? Are you trying to encrypt an already existing plaintext SQLite Database?

Yes, I confirm that the flow is exactly as you describe. The whole scope is the following: after fresh install DB does not exist and it’s then created on first start, encrypted from the beginning. At this first run everything works perfectly. On second and all farther runs DB cannot be initialized due to error described above.

Can you access the SQLCipher database used within the simulator from the SQLCipher command line shell to see if you are able to access the content following the second attempt from your application?