How to include sqlcipher into a C program on windows and compile it?

97 Views Asked by At

I am really struggling to include sqlcipher into my C program (right now it works with regular sqlite3)

I'm running windows 10 with Mingw-w64 GCC compiler. OpenSSL is installed in C:\Program Files\OpenSSL-Win64

I have managed to compile sqlcipher on windows and I can easily use it in cmd (encrypting with pragma key = "..." and decrypting it.

However I need it to work inside my C program.

With regular sqlite3 I just downloaded their amaglamation files and and "#include sqlite/sqlite3.h", then i compile the pgoram with gcc -o app app.c sqlite/sqlite3.c -lpthread

however when I use "#include sqlcipher/sqlite3.h" and compile with same command the program compiles and "works", sort of. It creates database but not encrypted.

The commands with PRAGMA key = '12345'; simply get ignored, and the database is not encrypted.

I have very little experience with compiling C programs, I just started learning C couple month ago.

Here's a simple C code to create encrypted database, the database gets created but it is not encrypted.

#include <stdio.h>
#include "sqlcipher/sqlite3.h" // Ensure this is the SQLCipher version
#include <stdlib.h>

int main() {
    sqlite3* db;
    int rc;

    // Open a new database file
    rc = sqlite3_open("encrypted.db", &db);
    if (rc) {
        printf("Error: couldn't open the database.\n");
        sqlite3_close(db);
        return 1;
    }

    // Encrypt the database using SQLCipher
    rc = sqlite3_exec(db, "PRAGMA key = '12345';", 0, 0, 0);
    if (rc != SQLITE_OK) {
        printf("Error: couldn't encrypt the database.\n");
        sqlite3_close(db);
        return 1;
    }

    // Create the "people" table
    char* sql = "CREATE TABLE people (id INTEGER, name TEXT);";
    rc = sqlite3_exec(db, sql, 0, 0, 0);
    if (rc != SQLITE_OK) {
        printf("Error: couldn't create table.\n");
        sqlite3_close(db);
        return 1;
    }

    // Close the database
    sqlite3_close(db);

    printf("Encrypted database created successfully.\n");
    return 0;
}

I appreciate any help

  1. I made sqlcipher work on windows in CMD, but it doesn't work inside the C pgogram I wrote. used this tutorial for windows: https://www.domstamand.com/compiling-sqlcipher-sqlite-encrypted-for-windows-using-visual-studio-2022/

  2. I suspect that i need to add something more into my copile command, but I have little prior experience with compiling C programs.

  3. The code provided creates database, but it is not encrypted. The code should create encrypted database.

0

There are 0 best solutions below