Not able to decrypt the database using C APIs

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?

 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'");
        }

@developernotes @sjlombardo please help here ?
Thanks

Hi @Shyamal_Shah @Nithin_Malik

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

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

1 Like