PDA

View Full Version : sql cipher query is not working with Qt



subhashree satpathy
31st January 2018, 11:59
Decrypt the database to a plaintext database

$ sqlcipher u.db

sqlite> PRAGMA key = '234ii';
sqlite> PRAGMA cipher_use_hmac = OFF;
sqlite> ATTACH DATABASE 'plaintext.db' AS plaintext KEY '';
sqlite> SELECT sqlcipher_export('plaintext');
sqlite> DETACH DATABASE plaintext;
sqlite> .q

This is working fine, but while coming to Qt Code we are trying to execute the same queries by using QSqlQuery.

This is the code flow how we are following in Qt-4.7.4,

QSqlDatabase db1 = QSqlDatabase::addDatabase("SQLITECIPHER");
db1.setDatabaseName("Qt.db");
qDebug() << QSqlDatabase::drivers();

db1.open();

QSqlQuery qry;
qry.exec("PRAGMA key = 'test123';");
qry.exec("PRAGMA cipher_use_hmac = OFF");
qry.exec("ATTACH DATABASE 'plaintext.db' AS plaintext KEY ''; ");
qry.exec("SELECT sqlcipher_export('plaintext');");
qry.exec("DETACH DATABASE plaintext; ");
qry.exec(".q");

db1.close();

In This case qry.exec("SELECT sqlcipher_export('plaintext');"); is not executing.

Please help me to sort out this issue.

d_stranz
31st January 2018, 17:40
In This case qry.exec("SELECT sqlcipher_export('plaintext');"); is not executing.

How do you know that -any- of the Qt code is working? Your code doesn't check for errors anywhere (and with the typos it won't even compile). The key is different between the sqlite command line code and the Qt code as well.

subhashree satpathy
2nd February 2018, 04:50
can u please tell me if der is any other procedure to encrypt and decrypt data by using sqlcipher in QT 4.7.4.

d_stranz
2nd February 2018, 16:35
QSqlDatabase db1 = QSqlDatabase::addDatabase("SQLITECIPHER");

SQLITECIPHER is not one of the database drivers supplied with Qt. Do you have a Qt driver for it? Was it compiled using your compiler toolchain and your version of Qt? Have you installed it in the correct place for a db plugin?

See the QSqlDatabase docs.

And as I said in my original answer to your post: You do not appear to be checking any of the return values for the Qt SQL methods you are calling. My guess is that the addDatabase() call is failing, and because of that nothing else that follows works either. You can't simply write code and assume it will work. So go back, add error checking, and run your code in a debugger to determine where it fails.

subhashree satpathy
5th February 2018, 05:28
Hi,
Please just tell me what are the database drivers available/supported in Qt for encryption and decryption of database. It will be very helpful for me to compile/build Qt.

Actually we were already checking the error handling, but i had typed here simple way to explain, how i am doing ? sorry for that.

Any how now i am posting actual code content from my sample code.


QSqlDatabase db1 = QSqlDatabase::addDatabase("SQLITECIPHER");

db1.setDatabaseName("Qt.db");
qDebug() << QSqlDatabase::drivers();

db1.open();

QSqlQuery qry;
bool ret = qry.exec("PRAGMA key = 'test123';");
qDebug()<<"Pragma key exec ret:::"<<ret;
ret = qry.exec("PRAGMA cipher_use_hmac = OFF");
qDebug()<<"Pragma cipher exec ret:::"<<ret;

ret = qry.exec("create table employee(sno INTEGER,empname varchar(100)); ");
qDebug()<<"create table exec ret:::"<<ret;
if(ret == false)
{
QMessageBox::warning(this,"Error-msg","Table already created",QMessageBox::Ok);
}

ret = qry.exec("insert into employee(sno,empname)values(46,'abc');");
qDebug()<<"insert into table exec ret:::"<<ret;
ret = qry.exec("insert into employee(sno,empname)values(11,'xyz');");
qDebug()<<"insert into table exec ret:::"<<ret;

db1.close();
if(ret == true)
QMessageBox::information(this,"Info","Encrypted Successfully",QMessageBox::Ok);
else
QMessageBox::warning(this,"Error-msg","Encryption Failed",QMessageBox::Ok);