# Use of unresolved identifier 'sqlite3\_key'

**URL:** <https://discuss.zetetic.net/t/use-of-unresolved-identifier-sqlite3-key/1305>\
**Category:** SQLCipher\
**Created:** [April 26, 2016, 2:00pm UTC](https://discuss.zetetic.net/t/use-of-unresolved-identifier-sqlite3-key/1305 "2016-04-26T14:00:23Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdleung](https://avatars.discourse-cdn.com/v4/letter/j/58f4c7/32.png) [@jdleung](https://discuss.zetetic.net/u/jdleung)\
**Post date:** [April 26, 2016, 2:00pm UTC](https://discuss.zetetic.net/t/use-of-unresolved-identifier-sqlite3-key/1305/1 "2016-04-26T14:00:23Z")

</div>

App is coding with Swift, SQLCipher installed and had tried to compile the app without using database, it worked well, but when I tried to use sqlite3\_key, it gives an error: Use of unresolved identifier ‘sqlite3\_key’.

I have defined -DSQLITE\_HAS\_CODEC in CFLAGS and added #import in bridgeHeader.h

Can anyone help to solve this error?

Thanks.

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [April 26, 2016, 2:16pm UTC](https://discuss.zetetic.net/t/use-of-unresolved-identifier-sqlite3-key/1305/2 "2016-04-26T14:16:30Z")

</div>

Hi @jdleung

How did you install SQLCipher into your application? Are you certain you are including `-DSQLITE_HAS_CODEC` for the target you are building (i.e., Debug vs. Release)?

---

<div class="post-metadata">

**Author:** ![jdleung](https://avatars.discourse-cdn.com/v4/letter/j/58f4c7/32.png) [@jdleung](https://discuss.zetetic.net/u/jdleung)\
**Post date:** [April 27, 2016, 7:56am UTC](https://discuss.zetetic.net/t/use-of-unresolved-identifier-sqlite3-key/1305/3 "2016-04-27T07:56:02Z")

</div>

Hi @developernotes

My installation, step by step, followed the instrution on this site [https://www.zetetic.net/sqlcipher/ios-tutorial/](https://www.zetetic.net/sqlcipher/ios-tutorial/)

The codes have included some sqlite3 operation before installing SQLCipher, and the App compile without any sqlite error after the SQLCipher’s installation. But when I tried to add “sqlite3\_key”, it gave me “unresolved identifier” error.

Here is what I do in CFLAGS for the TARGET:

 ![](https://us1.discourse-cdn.com/flex016/uploads/zetetic/original/1X/4942610c2d1378f1033aad5eba047d92dd069695.png)

BTW, I got 22 warnings something like the following:  
sqlite3.c:30923:6: “gethostuuid() is disabled.”  
sqlite3.c:33984:42: Implicit conversion loses integer precision: ‘unsigned long long’ to ‘int’  
sqlite3.c:76743:42: Implicit conversion loses integer precision: ‘sqlite3\_uint64’ (aka ‘unsigned long long’) to ‘int’

Thanks.

---

<div class="post-metadata">

**Author:** ![jdleung](https://avatars.discourse-cdn.com/v4/letter/j/58f4c7/32.png) [@jdleung](https://discuss.zetetic.net/u/jdleung)\
**Post date:** [April 27, 2016, 1:52pm UTC](https://discuss.zetetic.net/t/use-of-unresolved-identifier-sqlite3-key/1305/4 "2016-04-27T13:52:45Z")

</div>

Hi @developernotes

My installation, step by step, followed the instrution on this site [https://www.zetetic.net/sqlcipher/ios-tutorial/](https://www.zetetic.net/sqlcipher/ios-tutorial/)

The codes have included some sqlite3 operation before installing SQLCipher, and the App compile without any sqlite error after the SQLCipher’s installation. But when I tried to add “sqlite3\_key”, it gave me “unresolved identifier” error.

Here is what I do in CFLAGS for the TARGET:

 ![](https://us1.discourse-cdn.com/flex016/uploads/zetetic/original/1X/4942610c2d1378f1033aad5eba047d92dd069695.png)

BTW, I got 22 warnings something like the following:  
sqlite3.c:30923:6: “gethostuuid() is disabled.”  
sqlite3.c:33984:42: Implicit conversion loses integer precision: ‘unsigned long long’ to ‘int’  
sqlite3.c:76743:42: Implicit conversion loses integer precision: ‘sqlite3\_uint64’ (aka ‘unsigned long long’) to ‘int’

Thanks.

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [April 28, 2016, 8:50pm UTC](https://discuss.zetetic.net/t/use-of-unresolved-identifier-sqlite3-key/1305/5 "2016-04-28T20:50:04Z")

</div>

Hello @jdleung

Have you removed any other SQLite references such as the default SQLite dylib? If any other references to SQLite are present outside of SQLCipher you may see issues like this.

---

<div class="post-metadata">

**Author:** ![jdleung](https://avatars.discourse-cdn.com/v4/letter/j/58f4c7/32.png) [@jdleung](https://discuss.zetetic.net/u/jdleung)\
**Post date:** [April 29, 2016, 2:38pm UTC](https://discuss.zetetic.net/t/use-of-unresolved-identifier-sqlite3-key/1305/6 "2016-04-29T14:38:47Z")

</div>

Hi, @developernotes

According to the configuration instruction:  
🔥 Hot Tip: If libsqlite3.dylib or another SQLite framework is listed in your Link Binary With Libraries list be sure to remove it, or you’ll see duplicate symbol errors.

I had removed libsqlite3.tbd when adding SQLCipher to my projects , and I now add it in the project again, but the problem still exists.

Is there any issue will cause such problem? If isn’t, I may need to try a whole new configuration again.

Thanks.

---

<div class="post-metadata">

**Author:** ![jdleung](https://avatars.discourse-cdn.com/v4/letter/j/58f4c7/32.png) [@jdleung](https://discuss.zetetic.net/u/jdleung)\
**Post date:** [April 29, 2016, 2:45pm UTC](https://discuss.zetetic.net/t/use-of-unresolved-identifier-sqlite3-key/1305/7 "2016-04-29T14:45:00Z")

</div>

@developernotes

Other SQLite3 operations are working fine, such as sqlite3\_open, sqlite3\_close…

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [April 29, 2016, 3:17pm UTC](https://discuss.zetetic.net/t/use-of-unresolved-identifier-sqlite3-key/1305/8 "2016-04-29T15:17:19Z")

</div>

> [@jdleung](#):
>
> I had removed libsqlite3.tbd when adding SQLCipher to my projects , and I now add it in the project again, but the problem still exists.
> 
> Is there any issue will cause such problem? If isn’t, I may need to try a whole new configuration again.

You will need to remove all other references to SQLite aside from any references you source for SQLCipher. If your application is picking up on any other version of SQLite while trying to include SQLCipher it will cause issues.

---

<div class="post-metadata">

**Author:** ![jdleung](https://avatars.discourse-cdn.com/v4/letter/j/58f4c7/32.png) [@jdleung](https://discuss.zetetic.net/u/jdleung)\
**Post date:** [April 30, 2016, 3:55am UTC](https://discuss.zetetic.net/t/use-of-unresolved-identifier-sqlite3-key/1305/9 "2016-04-30T03:55:25Z")

</div>

Hello, @developernotes

I tried to build two TOTALLY NEW projects, one is Swift, anther is Objective-c. Adding SQLCipher in the two projects with the same steps.

The results are: Objective-c works, but Swift doesn’t. Still "use of unresolved identifier ‘sqlite3\_key’. sqlite3\_open and others works fine.

 ![](https://us1.discourse-cdn.com/flex016/uploads/zetetic/original/1X/7b98dc43818dd0b861f6e58e2bf19fc0db7e4a5c.png)

 ![](https://us1.discourse-cdn.com/flex016/uploads/zetetic/original/1X/85ec57b92b8fbc594fa1a46bf6e7ab6b7bc0a05e.png)

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [May 3, 2016, 2:39pm UTC](https://discuss.zetetic.net/t/use-of-unresolved-identifier-sqlite3-key/1305/10 "2016-05-03T14:39:23Z")

</div>

Hi @jdleung

I was able to replicate the behavior you are seeing, a solution that works for me was to include the following within my bridging header:

```auto
#define SQLITE_HAS_CODEC 1

```

Would you give that a try and let us know your results? Thanks!

---

<div class="post-metadata">

**Author:** ![jdleung](https://avatars.discourse-cdn.com/v4/letter/j/58f4c7/32.png) [@jdleung](https://discuss.zetetic.net/u/jdleung)\
**Post date:** [May 4, 2016, 2:09pm UTC](https://discuss.zetetic.net/t/use-of-unresolved-identifier-sqlite3-key/1305/11 "2016-05-04T14:09:58Z")

</div>

Hello, @developernotes

Thanks a lot! Your suggestion works great! The database can be encrypted now. Writing code like this:

```
let sql2 = "ATTACH DATABASE '\(encryptedFile)' AS encrypted KEY 'testkey'"
if sqlite3_exec(db, sql2, nil, nil, nil) != SQLITE_OK{
    let errmsg = String.fromCString(sqlite3_errmsg(db))
    print("error has : \(errmsg)")
}

let sql3 = "SELECT sqlcipher_export('encrypted')"
sqlite3_exec(db, sql3, nil, nil, nil)

let sql4 = "DETACH DATABASE encrypted"
sqlite3_exec(db, sql4, nil, nil, nil)

```

I must enter correct key when open the database with DB browser for SQLite on Mac, but I get 2 problems when using it on iOS:

**1.** The encrypted database can be opened without correct key once number of key characters matches, the following operation can open the database.

```
    if sqlite3_open(encryptedFile, &db) == SQLITE_OK {
        let key = "testkey333" // wrong key
        sqlite3_key(db, key, 7); // correct number of characters "7"

```

If I place the key before opening database, both the key and the number of characters must be correct:

```
    let key = "testkey" // correct key
    sqlite3_key(db, key, 7); // correct number of characters "7"
    if sqlite3_open(encryptedFile, &db) == SQLITE_OK {

```

**2.** I got 22 warnings below:

 ![](https://us1.discourse-cdn.com/flex016/uploads/zetetic/original/1X/244c53c193cfc5526bc9b9c4325726e4a178a88d.png)

Thanks.

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [May 4, 2016, 5:57pm UTC](https://discuss.zetetic.net/t/use-of-unresolved-identifier-sqlite3-key/1305/12 "2016-05-04T17:57:13Z")

</div>

Hi @jdleung

I am glad to hear that worked for you. With regard to your secondary problem, you must call `sqlite3_open` before you attempt to invoke `sqlite3_key`. You may wish to run something like the following against the output associated with your `encryptedFile` to verify it is being encrypted:

```auto
hexdump -C sqlcipher.db

```

Also, I would recommend checking the result code of your call to `sqlite3_exec` when you are performing your call to `sqlcipher_export(…)`.

---

<div class="post-metadata">

**Author:** ![jdleung](https://avatars.discourse-cdn.com/v4/letter/j/58f4c7/32.png) [@jdleung](https://discuss.zetetic.net/u/jdleung)\
**Post date:** [May 5, 2016, 12:28am UTC](https://discuss.zetetic.net/t/use-of-unresolved-identifier-sqlite3-key/1305/13 "2016-05-05T00:28:18Z")

</div>

Hi, @developernotes

I think the file is encrypted, here is the screenshot after using command `hexdump -C encrypted.db`  
 ![](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex016/uploads/zetetic/original/1X/cb6471c61c5166f214ec115f7414fea2b1e25aa7.png)

**But BIG PROBLEM is the encrypted database can be opened with the exact string length of the original key, and the key doesn’t matter.**

For example:  
I used key “testkey” when encrypted the database, it has 7 characters.

`ATTACH DATABASE '\(newFile)' AS encrypted KEY 'testkey'`

After the encrypted database is opened, I use the **WRONG KEY and CORRECT STRING LENGTH, THE CONTENT OF THE DATABASE CAN BE READ!**

```
let key = "testkey_WRONG"
sqlite3_key(db, key, 7);

```

Did I make anything wrong when encrypting the database? Or is it a bug?

Thanks.

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [May 5, 2016, 12:51pm UTC](https://discuss.zetetic.net/t/use-of-unresolved-identifier-sqlite3-key/1305/14 "2016-05-05T12:51:43Z")

</div>

Hello @jdleung

Can you post a full example of what you are doing? Can you adjust your process to not perform your attach, but then open the encrypted database directly, key it, finally query the database?

---

<div class="post-metadata">

**Author:** ![sjlombardo](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/sjlombardo/32/3_2.png) [@sjlombardo](https://discuss.zetetic.net/u/sjlombardo)\
**Post date:** [May 5, 2016, 1:11pm UTC](https://discuss.zetetic.net/t/use-of-unresolved-identifier-sqlite3-key/1305/15 "2016-05-05T13:11:21Z")

</div>

> [@jdleung](#):
>
> But BIG PROBLEM is the encrypted database can be opened with the exact string length of the original key, and the key doesn’t matter.
> 
> For example:I used key “testkey” when encrypted the database, it has 7 characters.
> 
> ATTACH DATABASE ‘(newFile)’ AS encrypted KEY ‘testkey’

In this case the key is ‘testkey’. When using attach, all characters in the string are used for key material.

> [@jdleung](#):
>
> After the encrypted database is opened, I use the WRONG KEY and CORRECT STRING LENGTH, THE CONTENT OF THE DATABASE CAN BE READ!
> 
> let key = “testkey\_WRONG”  
> sqlite3\_key(db, key, 7);
> 
> Did I make anything wrong when encrypting the database? Or is it a bug?

In this second case, the key is also ‘testkey’. Even though you have set the string to ‘testkey\_WRONG’, by passing the third argument to sqlite3\_key as 7, you are instructing the library to use only the first 7 characters as key material. Thus, the keys used in the first case and the second case are actually the same.

However, if you were to change your code to read as follows, where the first 7 characters are different, then the operation would fail because the first 7 characters of the string would be different, i.e. `'testkey' != 'yektset'`.

```
let key = "yektset_WRONG"

```

Likewise, if you were to use the full string length, instead of truncating it, it would also fail:

```
let key = "testkey_WRONG"
sqlite3_key(db, key, 13);

```

In this case, the full string would be used for key material, and `'testkey' != 'testkey_WRONG'`

---

<div class="post-metadata">

**Author:** ![jdleung](https://avatars.discourse-cdn.com/v4/letter/j/58f4c7/32.png) [@jdleung](https://discuss.zetetic.net/u/jdleung)\
**Post date:** [May 6, 2016, 7:34am UTC](https://discuss.zetetic.net/t/use-of-unresolved-identifier-sqlite3-key/1305/16 "2016-05-06T07:34:41Z")

</div>

Hi, @sjlombardo

Yes, I get it!

I only added characters after the original key, and I thought the key must be fully matched even if the third argument was not changed.

It works perfect now except 22 warnings 🙂

@developernotes @sjlombardo  
Thanks.

---

<div class="post-metadata">

**Author:** ![sjlombardo](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/sjlombardo/32/3_2.png) [@sjlombardo](https://discuss.zetetic.net/u/sjlombardo)\
**Post date:** [May 6, 2016, 12:33pm UTC](https://discuss.zetetic.net/t/use-of-unresolved-identifier-sqlite3-key/1305/17 "2016-05-06T12:33:42Z")

</div>

@jdleung I’m glad to hear everything is working for you now.

The warnings you referred to are likely from the upstream SQLite source. As a policy, we work to avoid changing core SQLite code in SQLCipher unless strictly required, as it makes auditing and periodic merges more reliable. We’ll look into the warnings, but it is possible that they may not be fixed.

---

<div class="post-metadata">

**Author:** ![jdleung](https://avatars.discourse-cdn.com/v4/letter/j/58f4c7/32.png) [@jdleung](https://discuss.zetetic.net/u/jdleung)\
**Post date:** [May 7, 2016, 2:15am UTC](https://discuss.zetetic.net/t/use-of-unresolved-identifier-sqlite3-key/1305/18 "2016-05-07T02:15:58Z")

</div>

Hello, @sjlombardo

I choose to ignore the warnings, because they by now will not impact the APP’s compiling. Just don’t know if they will decrease the security of SQLCipher? 😆

Thanks again.
