1

I need SQLCipher version 4.x for my C++ project. I do have SQLCipher 4.6.0 Community in my Ubuntu, but it shows 3.4.1 in my C++ program. I have tried to compile and use SQLCipher 4.6.0 and 4.6.1 for compiling my source code, but it just gives me the same result.

I tried to deal with a SQLCipher 3 (HMAC_SHA1) encrypted database and it works! BUT I do need to deal with some database which encrypts with SQLCipher 4 (HMAC_SHA512)。

My target is that my C++ program should work with SQLCipher 4.6.0 community and successful query the encrypted database.

Source code:

#define DSQLITE_HAS_CODEC = 1
#include <iostream>
#include <sqlcipher/sqlite3.h>
using namespace std;
void executeSQL(sqlite3* db, const char* sql) 
{
 char* errMsg = nullptr;
 int rc = sqlite3_exec(db, sql, nullptr, nullptr, &errMsg);
 
 cout << "SQL CMD: " << sql << endl;
 
 if (rc != SQLITE_OK) 
 {
 cerr << "SQL error: " << errMsg << endl;
 sqlite3_free(errMsg);
 } 
 else 
 {
 cout << "SQL command executed successfully." << endl;
 }
}
int main() {
 sqlite3 *db;
 sqlite3_stmt *stmt;
 int rc;
 
 rc = sqlite3_open("enc_Dev.db", &db);
 const char *sql_cipher_key = "PRAGMA key = '<64-byte key>';";
 executeSQL(db, sql_cipher_key);
 
 const char *sql_cipher_ver = "PRAGMA cipher_default_compatibility = 4;";
 executeSQL(db, sql_cipher_ver);
 const char *sql_cipher_page_size = "PRAGMA cipher_page_size = 4096;";
 executeSQL(db, sql_cipher_page_size);
 const char *sql_cipher_kdf_iter = "PRAGMA kdf_iter = 256000;";
 executeSQL(db, sql_cipher_kdf_iter);
 
 const char *sql_cipher_hmac_algo = "PRAGMA cipher_hmac_algorithm = HMAC_SHA512;";
 executeSQL(db, sql_cipher_hmac_algo);
 
 const char *sql_cipher_kdf_algo = "PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA512;";
 executeSQL(db, sql_cipher_kdf_algo);
 
 const char *sql_cipher_pt_hsize = "PRAGMA cipher_plaintext_header_size = 0;";
 executeSQL(db, sql_cipher_pt_hsize);
 if (sqlite3_exec(db, "SELECT count(*) FROM input_records;", NULL, NULL, NULL) != SQLITE_OK)
 {
 cerr << "Open encrypted database fail!" << endl;
 }
 const char *sql_ck = "PRAGMA cipher_version;";
 rc = sqlite3_prepare_v2(db, sql_ck, -1, &stmt, 0);
 int i = 0;
 while (sqlite3_step(stmt) == SQLITE_ROW) 
 {
 const unsigned char *output = sqlite3_column_text(stmt, i);
 cout << "PRAGMA cipher_version;\nResult: " << output;
 i++;
 }
 cout <<endl;
 const char *sql_prvdck = "PRAGMA cipher_provider_version;";
 rc = sqlite3_prepare_v2(db, sql_prvdck, -1, &stmt, 0);
 i = 0;
 while (sqlite3_step(stmt) == SQLITE_ROW)
 {
 const unsigned char *output = sqlite3_column_text(stmt, i);
 cout << "PRAGMA cipher_provider_version;\nResult: " << output;
 i++;
 }
 cout <<endl;
 sqlite3_finalize(stmt);
 sqlite3_close(db);
 return 0;
}

Output:

SQL CMD: PRAGMA key = '<64-byte key>';
SQL command executed successfully.
SQL CMD: PRAGMA cipher_default_compatibility = 4;
SQL command executed successfully.
SQL CMD: PRAGMA cipher_page_size = 4096;
SQL command executed successfully.
SQL CMD: PRAGMA kdf_iter = 256000;
SQL command executed successfully.
SQL CMD: PRAGMA cipher_hmac_algorithm = HMAC_SHA512;
SQL command executed successfully.
SQL CMD: PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA512;
SQL command executed successfully.
SQL CMD: PRAGMA cipher_plaintext_header_size = 0;
SQL command executed successfully.
Open encrypted database fail!
PRAGMA cipher_version;
Result: 3.4.1
PRAGMA cipher_provider_version;
Result: OpenSSL 3.0.0 7 sep 2021

My Ubuntu SQLCipher version:

SQLite version 3.45.3 2024年04月15日 13:34:05 (SQLCipher 4.6.0 community)
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite>
JaMiT
18.7k5 gold badges20 silver badges44 bronze badges
asked Sep 3, 2024 at 10:23
1
  • #define X = Y -- that's not how this works, because X is now = Y. You mean #define X Y. I guess that won't be the reason for your issue though. Commented Sep 3, 2024 at 12:34

1 Answer 1

0

Just fix!!

  1. Remove sqlcipher
  2. Restart
  3. Re-install sqlcipher 4.6.1
  4. Restart
  5. Compile
  6. Done!
answered Sep 3, 2024 at 14:56
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.