我的 C++ 项目需要 SQLCipher 版本 4.x。我的 Ubuntu 中确实有 SQLCipher 4.6.0 社区,但它在我的 C++ 程序中显示 3.4.1。我尝试编译并使用 SQLCipher 4.6.0 和 4.6.1 来编译我的源代码,但它只给了我相同的结果。
我尝试处理 SQLCipher 3 (HMAC_SHA1) 加密数据库,它有效!但我确实需要处理一些使用 SQLCipher 4 (HMAC_SHA512) 加密的数据库。
我的目标是我的C++程序应该与SQLCipher 4.6.0社区一起工作并成功查询加密数据库。
源代码:
#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;
}
输出:
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
我的 Ubuntu SQLCipher 版本:
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>
修好!!