Xcode 11, decryption is not working - not able to open the encrypted DB from code

Issue facing is - not able to open the encrypted DB from the code - Xcode 11
Steps followed:

  • In my Mac encrypted .db file with the help of CIPHER and set the password.
  • Just dragged and dropped that encrypted .db to my Xcode project.
  • Followed the steps as per the tutorial https://www.zetetic.net/sqlcipher/ios-tutorial/ and written code to decrypt the DB by using the password.

No errors are coming - It was printing the CIPHER version number in all the cases :

  • Attempt to open a database with a correct key and verify that the operation succeeds
  • Attempt to open a database with an incorrect key and verify that the operation succeeds
  • Attempt to open a database without any key, and verify the operation succeeds

Tried both the options-1 and option-2
Please help me out.

Thanks in advance.

Hello @Devan

Thanks for your interest in SQLCipher. Opening and keying a database are precursors to accessing the content within the database. After you have opened the database and provided a key, please then try to read from the database file. A common query to validate the encryption key is correct is to execute the following which will always be present regardless of your application schema:

SELECT count(*) FROM sqlite_master;

Would you give that a try and report your results?

Hello @developernotes,

I am facing similar issue. Everything was working fine before Xcode 11. Same code is not working in Xcode 11 and not doing decryption. If I run same code from Xcode 10, it is working fine. C Flags and everything is correctly set.

Also I tried to make provided select query and I am getting SQLITE_NOTADB error.

Any help will be appreciated. Thanks!

Hello @Rupal - For testing purposes please update your application to call PRAGMA cipher_version; and report the results. Are you getting a value back from that statement? This will help determine whether SQLCipher is properly linked.

SQLCipher Version - 3.4.2

Also looks like decryption issue is in iOS 13 not Xcode 11. Because Xcode 11 with iOS 12 works fine. If it is linking issue It should not even work for iOS 12 too ? I am trying to investigate how same setup is wrong for iOS 13.

Below is the observation:
sqlite3_exec(db, ( const char ) "SELECT count() FROM sqlite_master;", NULL , NULL , NULL ); returns SQLITE_NOTADB
sqlite3_key returns SQLITE_OK
sqlite3_prepare_v2 returns SQLITE_NOTADB below is the query
sqlite3_prepare_v2( db, [stringStatement UTF8String], kREAD_ENTIRE_STRING, &_SQLStatement, NULL );

Hello @Rupal - just to clarify, I am not specifically interested in what version you are using. I want to have you execute the PRAGMA cipher_version; statement and report the results. This will verify the linking. If that statement produces a result set, then the application is properly linked with SQLCipher. If it does not produce a result, then it means there is a linking problem and the application is using the system version of SQLite, not SQLCipher. Please modify the application accordingly and let me know what the results are. Feel free to run it once with iOS 12 and once with iOS 13 since you believe that to be related.

Hello @sjlombardo,

PRAGMA cipher_version is executing correctly. I was using below code and it returns sqlcipher_valid as true and returning cipher_version as 3.4.2. Hope this what you want to me to do to see if the SQLCipher linked properly. Let me know.

if(sqlite3_prepare_v2(db, "PRAGMA cipher_version;", -1, &stmt, NULL) == SQLITE_OK) {
           if(sqlite3_step(stmt)== SQLITE_ROW) {
               const unsigned char *ver = sqlite3_column_text(stmt, 0);
               if(ver != NULL) {
                   sqlcipher_valid = YES;
                   NSLog(@"ciper_version = %s", ver);
                   // password is correct (or database initialize), and verified to be using sqlcipher

               }
           }
           sqlite3_finalize(stmt);
       }

So question is if set up is correct than what could go wrong for select query and why it is returning 26( SQLITE_NOTADB) ?

Ok so I fixed it. Added -lsqlite3 in Other Linker Flags of project settings and that fixes the issue.

Hello @Rupal

This will cause you to link the standard SQLite library with your application, you do not want to do this if you’re attempting to integrate SQLCipher within your application. The project configuration may be the issue.

Would you mind detailing your specific situation as it relates to:

  • If the database you are interfacing with is already encrypted with SQLCipher, what version of SQLCipher was used to interface with the database?
  • If the database you are interfacing with is already encrypted with SQLCipher, can you please verify the output of the following command appears undecipherable: hexdump -C YourDatabaseFile.db
  • Are you building SQLCipher directly from the current master branch, or a specific tag?
  • Is there a reason you appear to be using 3.4.2?