PDA

View Full Version : how to use a hashed password as a private key for data encryption/decryption in Qt?



Pachuca
8th February 2020, 15:56
I'm sure I'm not the first to ask something like this, but I haven't been able to find anything useful so I'm posting this to get your help, thank you.

I'm trying to create an app to store some user data. I'm using sqlite to keep track of everything. The idea is that a user can create their own login. The login password will be hashed and the cipher kept in the db. The user should be able to use that hashed password as a private key to encrypt/decrypt other data they enter into the app. The data is stored locally on their computer so I don't want anyone to just open up the db and view all the user's info that's why I need to encrypt/decrypt that data during run time. I don't know how to write something like this. Can anyone give me some suggestions on where to start. I can't find any Qt class that would allow for encryption/decryption during run time. For the hash I was thinking of using QCryptographicHash, but can't figure out how to properly do that with a password. Would anyone mind showing me what the syntax for that would be? Do I need to import any libraries or should I be using only the qt functions? Anyway, I would really appreciate any help with this because it's the part I'm stuck on and can't finish my app without this feature.

Also, the encryption needs to be strong enough to store sensitive data like social security numbers or credit card numbers.

Edit: I'm aware that this isn't very secure, but for now I just need to have the password hashed and the data encrypted. I'm going to work on making it more secure later like adding 2 factor authorization.

ChrisW67
11th February 2020, 09:08
There are fully encrypted Sqlite database tools like SQLite Crypt (http://www.sqlite-crypt.com/) and SQLite Encryption Extension (https://www.sqlite.org/see) available commercially. SQL Cipher (https://www.zetetic.net/sqlcipher/) is available as open source or commercially.

It might be best to build the Qt Sqlite plugin using one of these rather than roll your own. The user password should be run through a key stretching (https://en.wikipedia.org/wiki/Key_stretching) before it is used to create the Sqlite file and later decrypt the file. The file cannot be opened with an incorrect password.

Pachuca
13th February 2020, 19:13
It might be best to build the Qt Sqlite plugin using one of these rather than roll your own.

That sounds great. I'm going to use the sqlcipher open source, but how do I do this part? It's my first time making a project like this. I've never even made an executable program before, this is my first attempt. Do I need to include it in the .pro file or should I clone it from git than make >> sudo make install ? Where can I find a step by step guide for doing this with qt creator (v5.14.1) in Ubuntu 18.04?

edit:

I think I found something from DB Browsers github as a step by step (https://github.com/sqlitebrowser/sqlitebrowser/wiki/SQLCipher:-Build-from-Source-on-Debian-Stretch---Newbie-Instructions) for getting sqlcipher installed, but how do I add it to Qt Creator?

ChrisW67
15th February 2020, 02:27
Assuming you followed those instructions then you have a libsqlcipher.so shared library somewhere on your system. If configured with system-sqlite option, Qt Sqlite driver will be looking for a libsqlite.so on your system to link with at run time.
To use the aternate library you need to clone the Qt Sqlite driver (https://doc.qt.io/qt-5/sql-driver.html#qsqlite) source, rename it, and modify it to use the new shared library.

Alternatively, you obtain the Qt source, replace the 3rdparty/sqlite code with the sqlcipher equivalent, and build the Qt Sqlite driver using the embedded code.

This is done with the Qt and compiler you are using to compile your programs, not Qt Creator's run time version of Qt. There is nothing to do in Qt Creator to make the resulting plugin available.