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>
1 Answer 1
Just fix!!
- Remove sqlcipher
- Restart
- Re-install sqlcipher 4.6.1
- Restart
- Compile
- Done!
#define X = Y-- that's not how this works, becauseXis now= Y. You mean#define X Y. I guess that won't be the reason for your issue though.