# Calling sqlite3\_rekey\_v2() exits with "no codec attached to db"

**URL:** <https://discuss.zetetic.net/t/calling-sqlite3-rekey-v2-exits-with-no-codec-attached-to-db/5027>\
**Category:** SQLCipher\
**Created:** [June 22, 2021, 4:28am UTC](https://discuss.zetetic.net/t/calling-sqlite3-rekey-v2-exits-with-no-codec-attached-to-db/5027 "2021-06-22T04:28:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dannyl07](https://avatars.discourse-cdn.com/v4/letter/d/47e85d/32.png) [@dannyl07](https://discuss.zetetic.net/u/dannyl07)\
**Post date:** [June 22, 2021, 4:28am UTC](https://discuss.zetetic.net/t/calling-sqlite3-rekey-v2-exits-with-no-codec-attached-to-db/5027/1 "2021-06-22T04:28:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dannyl07](https://avatars.discourse-cdn.com/v4/letter/d/47e85d/32.png) [@dannyl07](https://discuss.zetetic.net/u/dannyl07)\
**Post date:** [June 22, 2021, 5:46am UTC](https://discuss.zetetic.net/t/calling-sqlite3-rekey-v2-exits-with-no-codec-attached-to-db/5027/2 "2021-06-22T05:46:33Z")

</div>

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.
            }
        }
```

---

<div class="post-metadata">

**Author:** ![mmoore](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/mmoore/32/858_2.png) [@mmoore](https://discuss.zetetic.net/u/mmoore)\
**Post date:** [June 22, 2021, 1:33pm UTC](https://discuss.zetetic.net/t/calling-sqlite3-rekey-v2-exits-with-no-codec-attached-to-db/5027/3 "2021-06-22T13:33:00Z")

</div>

Hi @dannyl07

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

> [@dannyl07](#):
>
> 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.

You’ll want to use the [sqlcipher\_export()](https://www.zetetic.net/sqlcipher/sqlcipher-api/#sqlcipher_export) convenience function to encrypt a plaintext database.

> [@dannyl07](#):
>
> 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?

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?

---

<div class="post-metadata">

**Author:** ![dannyl07](https://avatars.discourse-cdn.com/v4/letter/d/47e85d/32.png) [@dannyl07](https://discuss.zetetic.net/u/dannyl07)\
**Post date:** [June 22, 2021, 1:55pm UTC](https://discuss.zetetic.net/t/calling-sqlite3-rekey-v2-exits-with-no-codec-attached-to-db/5027/4 "2021-06-22T13:55:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mmoore](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/mmoore/32/858_2.png) [@mmoore](https://discuss.zetetic.net/u/mmoore)\
**Post date:** [June 22, 2021, 2:19pm UTC](https://discuss.zetetic.net/t/calling-sqlite3-rekey-v2-exits-with-no-codec-attached-to-db/5027/5 "2021-06-22T14:19:32Z")

</div>

@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](https://www.zetetic.net/sqlcipher/ios-tutorial/)

---

<div class="post-metadata">

**Author:** ![dannyl07](https://avatars.discourse-cdn.com/v4/letter/d/47e85d/32.png) [@dannyl07](https://discuss.zetetic.net/u/dannyl07)\
**Post date:** [June 22, 2021, 3:04pm UTC](https://discuss.zetetic.net/t/calling-sqlite3-rekey-v2-exits-with-no-codec-attached-to-db/5027/6 "2021-06-22T15:04:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mmoore](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/mmoore/32/858_2.png) [@mmoore](https://discuss.zetetic.net/u/mmoore)\
**Post date:** [June 22, 2021, 7:18pm UTC](https://discuss.zetetic.net/t/calling-sqlite3-rekey-v2-exits-with-no-codec-attached-to-db/5027/7 "2021-06-22T19:18:40Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![dannyl07](https://avatars.discourse-cdn.com/v4/letter/d/47e85d/32.png) [@dannyl07](https://discuss.zetetic.net/u/dannyl07)\
**Post date:** [June 23, 2021, 2:17am UTC](https://discuss.zetetic.net/t/calling-sqlite3-rekey-v2-exits-with-no-codec-attached-to-db/5027/8 "2021-06-23T02:17:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mmoore](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/mmoore/32/858_2.png) [@mmoore](https://discuss.zetetic.net/u/mmoore)\
**Post date:** [June 23, 2021, 2:07pm UTC](https://discuss.zetetic.net/t/calling-sqlite3-rekey-v2-exits-with-no-codec-attached-to-db/5027/9 "2021-06-23T14:07:39Z")

</div>

@dannyl07

> [@dannyl07](#):
>
> Is there a workaround for them to operate together?

You can have a look at my post here: [Cannot open encrypted database with SQLCipher 4 - #4 by mmoore](https://discuss.zetetic.net/t/cannot-open-encrypted-database-with-sqlcipher-4/3654/4)

---

<div class="post-metadata">

**Author:** ![dannyl07](https://avatars.discourse-cdn.com/v4/letter/d/47e85d/32.png) [@dannyl07](https://discuss.zetetic.net/u/dannyl07)\
**Post date:** [June 23, 2021, 3:33pm UTC](https://discuss.zetetic.net/t/calling-sqlite3-rekey-v2-exits-with-no-codec-attached-to-db/5027/10 "2021-06-23T15:33:45Z")

</div>

@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
