# Not able to decrypt the database using C APIs

**URL:** <https://discuss.zetetic.net/t/not-able-to-decrypt-the-database-using-c-apis/5957>\
**Category:** Issues\
**Created:** [May 31, 2023, 10:08am UTC](https://discuss.zetetic.net/t/not-able-to-decrypt-the-database-using-c-apis/5957 "2023-05-31T10:08:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shyamal\_Shah](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/shyamal_shah/32/2019_2.png) [@Shyamal\_Shah](https://discuss.zetetic.net/u/Shyamal_Shah)\
**Post date:** [May 31, 2023, 10:08am UTC](https://discuss.zetetic.net/t/not-able-to-decrypt-the-database-using-c-apis/5957/1 "2023-05-31T10:08:59Z")

</div>

I am able to decrypt the database using the raw key with the DB Browser (SQLite) with the SQLChiper 3 default settings. But when I set the same settings through PRAGMA using sqlite3\_exec, I am not able to decrypt it.  
In DB Browser (SQLite), I am passing a raw key in ‘0x7Ab…’ format and I am passing same key to PRAGMA KEY.

Can someone guide what I may be doing wrong?

```auto
 snprintf(keyPragma, sizeof(keyPragma), "PRAGMA key = \"x'%s'\"", key);
        result = sqlite3_exec(connection, keyPragma, NULL, NULL, &err_msg);
        if (result != SQLITE_OK) {
            handle_error("Failed to execute 'PRAGMA key'");
        }
        
        result = sqlite3_exec(connection, "PRAGMA cipher_default_compatibility = 3;", NULL, NULL, &err_msg);
        if (result != SQLITE_OK) {
            handle_error("Failed to execute 'PRAGMA cipher_default_compatibility'");
        }

        result = sqlite3_exec(connection, "PRAGMA cipher_page_size = 1024;", NULL, NULL, &err_msg);
        if (result != SQLITE_OK) {
            handle_error("Failed to execute 'PRAGMA cipher_page_size'");
        }

        result = sqlite3_exec(connection, "PRAGMA kdf_iter = 64000;", NULL, NULL, &err_msg);
        if (result != SQLITE_OK) {
            handle_error("Failed to execute 'PRAGMA kdf_iter'");
        }

        result = sqlite3_exec(connection, "PRAGMA cipher_hmac_algorithm = HMAC_SHA1;", NULL, NULL, &err_msg);
        if (result != SQLITE_OK) {
            handle_error("Failed to execute 'PRAGMA cipher_hmac_algorithm'");
        }

        result = sqlite3_exec(connection, "PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1;", NULL, NULL, &err_msg);
        if (result != SQLITE_OK) {
            handle_error("Failed to execute 'PRAGMA cipher_kdf_algorithm'");
        }
        result = sqlite3_exec(connection, "PRAGMA cipher_plaintext_header_size = 0;", NULL, NULL, &err_msg);
        if (result != SQLITE_OK) {
            handle_error("Failed to execute 'PRAGMA cipher_plaintext_header_size'");
        }

```

---

<div class="post-metadata">

**Author:** ![Nithin\_Malik](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/nithin_malik/32/1995_2.png) [@Nithin\_Malik](https://discuss.zetetic.net/u/Nithin_Malik)\
**Post date:** [June 5, 2023, 6:34am UTC](https://discuss.zetetic.net/t/not-able-to-decrypt-the-database-using-c-apis/5957/2 "2023-06-05T06:34:46Z")

</div>

@developernotes @sjlombardo please help here ?  
Thanks

---

<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 5, 2023, 3:48pm UTC](https://discuss.zetetic.net/t/not-able-to-decrypt-the-database-using-c-apis/5957/3 "2023-06-05T15:48:31Z")

</div>

Hi @Shyamal_Shah @Nithin_Malik

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

[PRAGMA cipher\_default\_compatibility](https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_default_compatibility) will cause all _connections opened after the statement executes_ to operate in compatibility mode as referenced in the API documentation:

> Calling this PRAGMA and passing in 1, 2, 3, or 4 will cause SQLCipher to operate with the default settings consistent with that major version number as the default for the currently executing process (i.e. **all connections opened after the statement executes** ).

If you’d like to set the compatibility mode on a single connection after keying the database you’ll want to have a look at: [PRAGMA cipher\_compatibility](https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_compatibility) which causes the _current connection_ to operate in compatibility mode:

> Calling this PRAGMA and passing in 1, 2, 3, or 4 will cause SQLCipher to operate with default settings consistent with that major version number for the **current connection**

If you’re opening multiple connections in compatibility mode then it makes sense to use `PRAGMA cipher_default_compatibility` but you’ll want to execute the PRAGMA prior to opening any of the database connections (for example using an in memory database connection).

Additionally, when properly setting the `cipher_compatibility` or `cipher_default_compatibility` PRAGMA’s you shouldn’t need the `cipher_page_size`, `kdf_iter`, `cipher_hmac_algorithm`, `cipher_kdf_algorithm`, or `cipher_plaintext_header_size` PRAGMAs as they are all set to the correct defaults for the compatibility version.
