Ios couchbase lite SQLCipher unable to decrypt database

Hi,

I enabled encryption through cocoapods installation:

pod 'couchbase-lite-ios', '~> 1.3.1' pod 'couchbase-lite-ios/SQLCipher', '1.3.1'


Database encryption is successful, while i’m getting these errors when decrypting the database:

DB Error: 26 "file is encrypted or is not a database"
WARNING: CBL_SQLiteStorage: database is unreadable (err 26) {at -[CBL_SQLiteStorage decryptWithKey:error:]:284}
Error code: 401
Error message: unauthorized


Here’s the complete Database log:

18:11:29.720| Database: Created CBLManager[0x6080002ef280 /Users/.../data/Containers/Data/Application/.../Library/Application Support/CouchbaseLite]

18:11:35.393| Database: Opening CBLDatabase[<0x600000338880>katsynctest2]

18:11:35.393| Database: Using CBL_SQLiteStorage for db at /Users/…/data/Containers/Data/Application/…/Library/Application Support/CouchbaseLite/katsynctest2.cblite2; upgrade=0

18:11:35.457| Couchbase Lite using SQLite version 3.8.10.2 (2015-05-20 18:17:19 2ef4f3a5b1d1d0c4338f8243d40a2452cc1f7fe4)

18:11:35.458| Database: Opening <CBL_SQLiteStorage: 0x60000046e380>

18:11:35.458| Database: Open /Users/.../data/Containers/Data/Application/.../Library/Application Support/CouchbaseLite/katsynctest2.cblite2/db.sqlite3 with SQLite (flags=6, encryption key given)

2017-02-05 18:11:35.502 Suites[15160:248939] DB Error: 26 "file is encrypted or is not a database"

2017-02-05 18:11:35.503 Suites[15160:248939] DB Query: SELECT count(*) FROM sqlite_master

18:11:35.503| WARNING: CBL_SQLiteStorage: database is unreadable (err 26) {at -[CBL_SQLiteStorage decryptWithKey:error:]:284}

2017-02-05 18:11:35.503 Suites[15160:248939] Cannot open database. Error message: unauthorized


Here is how I create the CBL Manager with CBLManagerOptions hoping to fix the current error:

NSError *error; CBLManagerOptions options = {NO, NSDataWritingFileProtectionCompleteUntilFirstUserAuthentication};

self.manager = [[CBLManager alloc] initWithDirectory: [CBLManager defaultDirectory] options: &options error: &error];

Are you providing the same encryption key when reopening the database? If not, you’ll get exactly that error.

yes, I am providing the same encryption key

It definitely looks like it’s not the same key. Have you verified it by setting a breakpoint and examining the parameters when opening the database, or else logging the key parameter?

I already checked with breakpoints and logging the key, it is the same key…

upon researching for causes, i stumble upon this post:

so i tried implementing this, but still won’t work:

NSError *error; CBLManagerOptions options = {NO, NSDataWritingFileProtectionCompleteUntilFirstUserAuthentication};

self.manager = [[CBLManager alloc] initWithDirectory: [CBLManager defaultDirectory] options: &options error: &error];

I’m currently researching on one of your reply:

You should also read the iOS docs on file protection, if you’re not familiar with it.

Thank you.

From the logs: if your database path looks like /Users/.../data/Containers/Data/Application/.../Library/Application Support/CouchbaseLite/katsynctest2.cblite2, you’re not running on an iOS device. Looks like it’s the iOS simulator. The simulator doesn’t implement file protection, so that rules it out as a cause.

Could you show the code you use to open the database?

here

That looks fine. I don’t know what’s going wrong, but we do have many other developers using SQLCipher successfully, so I don’t think this is a bug in Couchbase Lite. All I can suggest is double-checking that the password is the same. Maybe try changing password in the above code with a string literal like @"fakepassword", just to guarantee it’ll always be the same. If that makes the problem go away, you’ll know that somehow your passwords don’t match.

I did that but erorr is still the same… Although I just noticed from the Database logs, during initialisation and database creation (with encryption), _encryption file was deleted.

Here's the logs:
Database: Created CBLManager[0x6080000f4500 /...]
Database: Opening CBLDatabase[<0x6080001302c0>databasename]
Database: Using CBL_SQLiteStorage for db at /.../databasename.cblite2; upgrade=0
Database: Opening <CBL_SQLiteStorage: 0x600000272f00>
Database: Open /.../databasename.cblite2/db.sqlite3 with SQLite (flags=6, encryption key given)
Database: No pending doc expirations
CBLBlobStore: decrypting /.../databasename.cblite2/attachments
No blobs to copy; done.
Database: Deleted file /.../databasename.cblite2/attachments/_encryption
Database: Opening <CBL_SQLiteStorage: 0x600000272f00>
Database: Open /.../databasename.cblite2/db.sqlite3 with SQLite (flags=6(null))

could that be it?

Yes, i’m testing using simulator. Sorry Im still new to iOS/mobile development. I’ll try testing on the actual device. I’ll let you know how it goes. Thanks a lot! :slight_smile:

Those log message, especially “decrypting…” show that you removed encryption from the database (by calling -changeEncryptionKey: nil error:....

If after that you try to reopen the database using the old encryption key, it will fail.

1 Like