Calling sqlite3_rekey_v2() exits with "no codec attached to db"

Just installed SQLCipher with cocoapods and is trying to encrypt an existing sqlite db using sqlite3_rekey_v2(). However while tracing it seems that it gets to this condition. I was under the impression that if a database is not encrypted calling sqlite3_rekey_v2 will encrypt it in-place.

if(ctx == NULL) {
/* there was no codec attached to this database, so this should do nothing! */
CODEC_TRACE(“sqlite3_rekey_v2: no codec attached to db, exiting\n”);
return SQLITE_OK;
}

Thanks in advance.

I was able to get a legit encrypted database and I’m able to open it using DB Browser for SQLite on OSX providing the encrypted passphrase. But when I try to decrypt in objective-c it always comes up as invalid. I’m getting error code 26. Do I have a build issue?

       const char* key = [self.keyForEncryption UTF8String];
        int keylen = (int)strlen(key);
        int result = sqlite3_key(_database, key, keylen);
        if (result != SQLITE_OK) {
            NSLog(@"Error decryption failed with %d", result);
        } else {
            result = sqlite3_exec(_database, (const char*) "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL);
            if (result != SQLITE_OK) {
                //It's always returning 26 even though the passphrase is correct.
            }
        }

Hi @dannyl07

Thanks for your interest in SQLCipher and for posting to the discussion forum.

You’ll want to use the sqlcipher_export() convenience function to encrypt a plaintext database.

This pretty much always points to the key material you’re using being incorrect or the database being encrypted with different settings than the default.

  • How was the database encrypted?
  • Which settings are you using in DB Browser for SQLite to open the encrypted database?
  • Which version on SQLCipher are you using?

Hi,

Thank you for responding.

  • The database was encrypted by me. By ATTACH …filepath AS encrypted KEY …passphrase and then SELECT sqlcipher_export(‘encrypted’); I’m able to open it with DB Browser for SQLite by providing the correct password.
  • The DB Browser setting is “SQLCipher 4 defaults”, using passphrase.
  • The SQLCipher version is “4.4.3 community” when I run “PRAGMA cipher_version;”

One thing I just learned is if I were to create a brand new bare-bone project I’m able to decrypt the database successfully. However the exact same piece of code (hard-coded path + passphrase) results in error-code 26 in my dev project. So that means my project is not configured correctly. But not sure where to look.

@dannyl07

Are you including separate sqlite3 dependencies within your non-bare-bones project (potentially from other cocoa pods)?

Please see the note on this documentation page under Cocoa pods integration: SQLCipher Community Edition - iOS and macOS Tutorial - Zetetic

I think I am including separate sqlite3 dependencies inadvertently. I just deleted my pod folder and did a pod install, and now my project doesn’t compile. I’m getting “redefinition of sqlite3_…” errors. It shows the previous definition is in “iPhoneSimulator.sdk/usr/include/sqlite3.h”. I am using another pod "od ‘SQLite.swift/SQLCipher’, ‘~> 0.12.2’, but in the standalone project using the same pod definition this was not an issue. Thank you.

@dannyl07

We don’t maintain that project (SQLite.swift), but if you’re still experiencing difficulties I would recommend incrementally adding each of your pod dependencies to a bare-bones project to see if you’re able to reproduce the issue there. Since you mentioned previously that you were able to successfully open the database using just the SQLCipher pod (or SQLite.swift/SQLCipher pod), that points to some other conflict where sqlite3 is being included from another dependency.

Hi @mmoore,

Thanks for your suggestion. I did that and found two culprits:
pod ‘CouchbaseLite-Swift-Enterprise’, ‘~> 2.8’
pod ‘Firebase/Analytics’

Couchbase is a nosql database for mobile apps and Firebase is Google Analytics. Both uses sqlite underneath the hood. Is there something I’m missing in terms of configuring my project? Is there a workaround for them to operate together?

Thank you

@dannyl07

You can have a look at my post here: Cannot open encrypted database with SQLCipher 4 - #4 by mmoore

@mmoore
Thank you. I think I finally figured out that an additional step is to modify the “Other Linker Flags” and ensure I insert -framework “SQLCipher” as the first item before $(inherited). That was the only way I can get all of this to work. Thanks