Question

i am writing an application which encrypt the SQLITE3 database. for encryption i have used SQLCIPHER.i am able to perform encryption and decryption of Database from command line.i am writing the client app using c++. here is my code to test the key (used for encryption).

int main()
{
    sqlite3 *DataBase;

    if(sqlite3_open("encrypted.db", &DataBase)== SQLITE_OK)
    {

        int result = sqlite3_exec(DataBase, "PRAGMA KEY = 'testkey';", NULL, NULL, NULL); 

        result = sqlite3_exec(DataBase,"SELECT user_id FROM USER;", NULL, NULL, NULL);
        if(result == SQLITE_OK)
        {
            cout<<"sqlite3_exec success"<<endl;
        }        
        else 
        {
                    cout<<"sqlite3_exec failed"<<endl;
                    cout<<"sqlite3_exec returns "<<result<<endl;

            }       

    }
}

i am able to the same using command line:

./sqlite3 encrypted.db
PRAGMA key = 'testkey';
SELECT user_id FROM USER;

i have encrypted my access.db using:

./sqlite3 access.db 
sqlite> ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'testkey'; 
sqlite> SELECT sqlcipher_export('encrypted'); 
sqlite> DETACH DATABASE encrypted; 

i dont get why i am able to do the same using command line and not by c++ code. please let me know if i am doing something wrong.

please also let me know if i have to access the encrypted database then i should first decrypt it and then i should read the plaintext database, are there no API's which i could use directly to read encrypted database?

any help is appreciated.

thanks,

Was it helpful?

Solution 2

finally i am able to do it. while executing my program i need to include -lsqlciper also. adding that successfully executed the code. i build it using "g++ testSQLCipher.cpp -I /usr/local/include -L /usr/local/lib -lsqlcipher -L /usr/lib/i386-linux-gnu -ldl -o testSQL"

OTHER TIPS

If you are going to issue a PRAGMA key command, you must quote the passphrase value in your C++ application. Reviewing the sample you submitted, you have not done this. Alternatively, you could just call sqlite3_key.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top