Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: Protecting SQLite Data

  1. #1
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb Protecting SQLite Data

    I created an application that queries an SQLite database. The tables in the database have taken months to fill.

    Is there a way to protect my data from thieves. I do not want them to just open up the database and taking my work.

    To be precise can one put a password on an SQLite database.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Protecting SQLite Data

    Have you seen my wiki article? [WIKI]Building QSQLITE driver with AES-256 encryption support[/WIKI]

  3. The following 2 users say thank you to Lykurg for this useful post:

    BalaQT (21st February 2011), zim (21st February 2011)

  4. #3
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Protecting SQLite Data

    Thank you.

    I was a little reluctant to follow the tutorial because I have Windows 7 64bit. I also spent weeks trying to build MySQL driver on Win 64 7.

    Anyway this tutorial was precise.

    Thank you
    Last edited by zim; 20th February 2011 at 22:25. Reason: spelling corrections

  5. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Protecting SQLite Data

    Do remember that the database is only secure whilst you keep the passphrase/key confidential. If you expose the key in plain text in your application for example it will be very easy for someone to find with a simple hex editor. Your encryption is then worthless, regardless of what it is.

  6. #5
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Protecting SQLite Data

    Thanx, So if I was to place the key in the source code i.e. bdapp.cpp that works?

    I also wanted to know if there is a way to encrypt an existing database?

    Thanx.

  7. #6
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Protecting SQLite Data

    Quote Originally Posted by zim View Post
    Thanx, So if I was to place the key in the source code i.e. bdapp.cpp that works?
    Thats exactly does NOT works!!

    apply a little protection in your password. A very simple example :-
    suppose your password is "AB".
    instead of
    Qt Code:
    1. database.open(filename, "AB");
    To copy to clipboard, switch view to plain text mode 
    use
    Qt Code:
    1. database.open(filename, QString("BC").replace('B', 'B'-1 ).replace('C', 'C'-1 ));
    To copy to clipboard, switch view to plain text mode 

    ofcourse this is a stupid example in itself but anyone who opens your binary in hex editor will only see "BC" as password.
    But then this also does not prevent anyone who is going to debug your binary. But at least you get the idea.


    I also wanted to know if there is a way to encrypt an existing database?
    i dont know a direct way, but i would prefer just to open the old db and export it to a new encrypted one.

  8. The following user says thank you to nish for this useful post:

    zim (16th April 2011)

  9. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Protecting SQLite Data

    Encrypt an existing database... have a look here: http://sqlcipher.net/documentation/api#atach

  10. The following user says thank you to ChrisW67 for this useful post:

    zim (21st February 2011)

  11. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Protecting SQLite Data

    Quote Originally Posted by squidge View Post
    Do remember that the database is only secure whilst you keep the passphrase/key confidential. If you expose the key in plain text in your application for example it will be very easy for someone to find with a simple hex editor.
    That's a topic what is on my mind since I wrote the article. But since I am not really an encryption specialist and my application where I use encrypted databases is not a high secure one, I am currently satisfied with:
    • Store an ascii string in the source code
    • Do a caesar cipher
    • Do some letter swaps
    • Do some position swaps

    That's not secure after all, I know, but it for my use right now it is ok.


    If any of you is more into encryption stuff, it would be nice you if you would extend our wiki with a short article on how to store a password along an application in a safe way.

  12. #9
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Protecting SQLite Data

    Quote Originally Posted by Lykurg View Post
    That's a topic what is on my mind since I wrote the article. But since I am not really an encryption specialist and my application where I use encrypted databases is not a high secure one, I am currently satisfied with:
    • Store an ascii string in the source code
    • Do a caesar cipher
    • Do some letter swaps
    • Do some position swaps

    That's not secure after all, I know, but it for my use right now it is ok.


    If any of you is more into encryption stuff, it would be nice you if you would extend our wiki with a short article on how to store a password along an application in a safe way.
    no matter how much a mathematic guru anyone is, there is no full proof way to store a password with your application.
    After any type of encryption of your key, You HAVE to decrypt it in your code and pass it to the sqlite function! and thats where your decrypted key can
    be easily seen in the debugger. The cracker doesn't need to go through you super duper decryption function in ASM to see it.

  13. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Protecting SQLite Data

    For the most basic of encryption, the only thing you need to do is to protect it against people "fishing" with a hex editor. For this, even if you just XOR each character of the password with a fixed number, it will satisfy the requirement (so Lykurg's method is more than enough here)

    Protecting against someone armed with a debugger is a complete story on itself. The easiest way to do this is to static link the SQLite library with your application and compress/encrypt the executable with an application such as Armadillo, PEcrypt, ASProtect and such like. The static linking removes the ability for anyone to compile there own (possibly rogue) version of SQLite and attach it to your application. This is permissable in any country which acknowledges software placed into the public domain (as is SQLite). Typically, this will make it too much effort for someone to want to hack you application to find the password (unless they are really determined, in which case, there's nothing you can do).

    Of course, you still need a reasonable password. Anyone can run your database through a dictionary attack. Ensure you use a long password consisting of letters, numbers and special characters.
    Last edited by squidge; 21st February 2011 at 08:13.

  14. #11
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Protecting SQLite Data

    Thank you everyone!

    I followed through the SQLCIPHER tutorial on their website. Code ran, no errors.

    created a new db, set the key, encrypted it, attached it, copied my existing sql tables to it.

    I then opened the newly encrypted db with a regular firefox extesion. I was able to browse all the data on the encrypted db. I even tried using different keys on the same db, worked.

    It seems like the QSQLCIPHER is not encrypting the db, since the db can be viewed without unencrypting it.

  15. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Protecting SQLite Data

    Works fine for me. You are using the Sqlcipher version of sqlite3, aren't you?

    Qt Code:
    1. // First, a freshly built sqlcipher
    2. chrisw@newton /tmp/sqlcipher $ ./sqlite3 plain.db
    3. SQLite version 3.7.2
    4. Enter ".help" for instructions
    5. Enter SQL statements terminated with a ";"
    6. sqlite> create table data(a integer);
    7. sqlite> insert into data values (1);
    8. sqlite> insert into data values (2);
    9. sqlite> insert into data values (3);
    10. sqlite> ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'secret';
    11. sqlite> create table encrypted.data as select * from data;
    12. sqlite> select * from encrypted.data;
    13. 1
    14. 2
    15. 3
    16. sqlite> .q
    17.  
    18. // See that the encrypted file is not identified as Sqlite
    19. chrisw@newton /tmp/sqlcipher $ file plain.db encrypted.db
    20. plain.db: SQLite 3.x database
    21. encrypted.db: data
    22.  
    23. // And that the data is there
    24. chrisw@newton /tmp/sqlcipher $ ./sqlite3 encrypted.db
    25. SQLite version 3.7.2
    26. Enter ".help" for instructions
    27. Enter SQL statements terminated with a ";"
    28. sqlite> pragma key = "secret";
    29. sqlite> .tables
    30. data
    31. sqlite> select * from data;
    32. 1
    33. 2
    34. 3
    35. sqlite> .q
    36.  
    37. // Now with the system, non-encrypting sqlite3
    38. chrisw@newton /tmp/sqlcipher $ /usr/bin/sqlite3 encrypted.db
    39. SQLite version 3.7.2
    40. Enter ".help" for instructions
    41. Enter SQL statements terminated with a ";"
    42. sqlite> .tables
    43. Error: file is encrypted or is not a database
    44. sqlite> .q
    To copy to clipboard, switch view to plain text mode 

  16. The following user says thank you to ChrisW67 for this useful post:

    zim (24th February 2011)

  17. #13
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: Protecting SQLite Data

    This is my code, and I am not getting any errors.

    Somehow the db is unencrypted.

    Qt Code:
    1. #include <QtSql/QSqlError>
    2. #include <QtSql/QSqlDatabase>
    3. #include <QtSql/QSqlQuery>
    4. #include <QtSql/QSqlRecord>
    5. #include <QVariant>
    6. #include <QDebug>
    7.  
    8. shonaDB::shonaDB()
    9. {
    10. db = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLCIPHER","newDB"));
    11. dbCopy = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLCIPHER","oldDB"));
    12. dbCopy->setDatabaseName("shonaOld.db");
    13. db->setDatabaseName("shonaNew.db");
    14.  
    15. if(!db->open())
    16. {
    17. qDebug() << "db open fail";
    18. }
    19.  
    20. if(!dbCopy->open())
    21. {
    22. qDebug() << "dbCopy open fail";
    23. }
    24.  
    25.  
    26. QSqlQuery qry(QSqlDatabase::database("newDB"));
    27.  
    28. qry.prepare("PRAGMA key = 'xyz'");
    29.  
    30. if(!qry.exec())
    31. {
    32. qDebug() << "key fail";
    33. }
    34.  
    35. }
    36.  
    37.  
    38.  
    39.  
    40. void shonaDB::copyDatabase()
    41. {
    42.  
    43. QSqlQuery qry(QSqlDatabase::database("newDB"));
    44.  
    45. qry.prepare("ATTACH DATABASE 'newDB' AS encrypted KEY 'xyz' ");
    46.  
    47. if(!qry.exec())
    48. {
    49. qDebug() << dbCopy->lastError().text();
    50. }else{
    51. qDebug()<< "Success ATTACH";
    52. }
    53.  
    54. qry.prepare("CREATE TABLE encrypted.dictionary (txtVar text, txtVarTwo text, txtVarThree text);");
    55.  
    56. if(!qry.exec())
    57. {
    58. qDebug() << dbCopy->lastError().text();
    59. }else{
    60. qDebug()<< "Success ct";
    61. }
    62.  
    63.  
    64. qry.prepare("INSERT INTO encrypted.dictionary SELECT * FROM dictionary;");
    65.  
    66. if(!qry.exec())
    67. {
    68. qDebug() << dbCopy->lastError().text();
    69. }else{
    70. qDebug()<< "Success transfer";
    71. }
    72.  
    73.  
    74. }
    To copy to clipboard, switch view to plain text mode 

  18. #14
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Protecting SQLite Data

    Your copyDatabase() method should open the unencrypted database (i.e. oldDB), attach the encrypted database, and then push the table contents into the attached database. You are currently opening the newDB and attaching a second copy of the newDB. I am surprised that the create table doesn't fail.

  19. #15
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Protecting SQLite Data

    Hi

    do you need two database objects to attach the encrypted database.

  20. #16
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Protecting SQLite Data

    If you are doing this in Qt code then you use a single db object and use a QSqlQuery to execute an ATTACH statement. The attachment is only good on that db instance in my experience. You need to open the unencrypted database and attach the encrypted database (your last code post doesn't do that).

    Once you have the encrypted database, do you need to allow the user to repeat this in your application? If not then why not just do the process from the command line as in my earlier example.
    Last edited by ChrisW67; 22nd March 2011 at 23:11.

  21. The following user says thank you to ChrisW67 for this useful post:

    zim (23rd March 2011)

  22. #17
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Protecting SQLite Data

    It might be simpler to protect the website through any of a number of server controls that require a user to enter a password before access to a site is granted. These are simple to set up and most hosting services provide the services and tools required. Your unencrypted database then lives within the security provided by restricted access to the web page that interacts with it.

    This also makes it simple to grant a few priveleged users (or only yourself) access to administrator functions.

  23. #18
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Protecting SQLite Data

    Website? This looks like a desktop application.

  24. #19
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Protecting SQLite Data

    Quote Originally Posted by ChrisW67 View Post
    Website? This looks like a desktop application.
    It wasn't clear to me; Firefox was mentioned earlier, so I thought I'd mention the server-based approach as an alternative.

  25. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Protecting SQLite Data

    I just read the thread and I'm still not convinced this whole approach is safe. At some point there will be a plaintext version of the password somewhere in the process memory and no executable mangling will prevent that. Dumping core at this moment will sooner or later reveal this password. To me only situations where you do not pass the plaintext password (like challenge and response solutions) are possible candidates for a good solution.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Qt to Sqlite data insertion doubt.
    By rex in forum Qt Programming
    Replies: 15
    Last Post: 16th December 2010, 12:31
  2. Replies: 13
    Last Post: 6th December 2010, 04:41
  3. Sqlite and UTF8 data
    By kroenecker in forum Qt Programming
    Replies: 2
    Last Post: 19th April 2009, 14:49
  4. data not being retained in sqlite DB
    By sticcino in forum Qt Programming
    Replies: 2
    Last Post: 2nd July 2008, 10:42
  5. Write protecting cells
    By therealjag in forum Qt Programming
    Replies: 2
    Last Post: 12th February 2006, 09:47

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.