Anyone successfully used password on sqlite database in Monotouch? - sqlite

I have a Monotouch app which uses a sqlite database. I want to encrypt the database so I am doing this:
_mainConnection = new SqliteConnection("Uri="+finalDB);
_mainConnection.Open();
_mainConnection.ChangePassword("mypassword");
However, its not working (on simulator and iphone). It gets this error:
at (wrapper managed-to-native)
Mono.Data.Sqlite.UnsafeNativeMethods.sqlite3_rekey
(intptr,byte[],int) <0x0005c> at
(wrapper managed-to-native)
Mono.Data.Sqlite.UnsafeNativeMethods.sqlite3_rekey
(intptr,byte[],int) <0x0005c> at
Mono.Data.Sqlite.SQLite3.ChangePassword
(byte[]) <0x00053> at
Mono.Data.Sqlite.SqliteConnection.ChangePassword
(byte[]) <0x0004b> at
Mono.Data.Sqlite.SqliteConnection.ChangePassword
(string) <0x0005b>
Has anyone successfully used password protection on an sqlite database in Monotouch?

As per my research there are a few options for database encryption using MonoTouch. I have a forthcoming blog post on the subject, but for now these are your top two options:
SQLCipher
I've automated the SQLCipher build process substantially. All it takes is a simple make command and you've got a library that you can link into your project. It makes use of the awesome SQLite-NET library. After that, all that's required is to provide the key in the SQLite.cs file.
SQLCipherNet: https://github.com/anujb/SQLCipherNet
CSharp-SQLite
This is a managed port of the SQLite library in C#. Performance is only about ~2x slower, which is pretty awesome considering it's not native code!
Encryption: http://code.google.com/p/csharp-sqlite/wiki/ENCRYPTION
Perf Benchmarks: http://code.google.com/p/csharp-sqlite/wiki/Benchmarks

Try adding ";Password=mypassword" to your connection string, and remove the call to ChangePassword.
Please note that, by default, the iPhone implementation of sqlite does not support encryption, so the sqlite commands for that will be no-ops.
You can get a (paid) copy of the encrypt-able version of sqlite from http://www.hwaci.com/sw/sqlite/see.html, and compile it into your application, making sure to remove the libsqlite3*.dylib from your project if you've linked that in.
You may have to do a bit of digging in the Monotouch documentation and/or experimentation to make sure that the Monotouch library itself is not including the default sqlite implementation, but in fact links to the implementation you specify. Try it first, if things still don't work that's where I'd start looking.
You can do this experiment without paying for the encrypted version, simply using the sqlite3 source code available on the net, with appropriate break points.
Good luck!
PS: Note that there is no comparable solution for Android at this point, this works on iPhone because iPhone runs native C code.
PPS: There is also SQLCipher that claims to encrypt sqlite on iPhone. However I found the configuration requirements to be below my standards for simplicity. I'm also not sure if it will properly insert itself between Monotouch's framework code and the default iPhone sqlite implementation.

SQLCipher for MonoTouch provides full database encryption for SQLite databases.
http://sqlcipher.net/sqlcipher-for-monotouch
There is also a SQLCipher on Mono for Android, which allows you to reuse the same code across Mono Touch and MonoDroid applications
http://sqlcipher.net/sqlcipher-for-monodroid

Just thinking out loud but could this be due to sqlite's dynlib that comes with the iPhoneSDK not being threadsafe?
For an alternative you might try looking at WWDC Vid 209 and just lock/encrypt the DB when you're outside the app.

You can probably do it yourself by issuing a "pragma rekey" in a raw SQLite query -- that is, if the SQLite version installed is actually SqlCipher.

I had the same problem but with a windows form application in C#.
I could not find the solution so i had to encrypt my data manually when saving it and decrypt it when retrieving.

Related

How to use Sqlite Database in browser for offline usage

I have developed one application in ionic for offline mode. I have used sqlite database to store the data. Now i want to create a browser build. But sqlite is not supported in browser. Is there any way to use sqlite in browser?
Or else is there any database which works same as sqlite for browser.(kindly do not suggest websql database and pouchdb)
i am using pouchdb for now, but there is lots of redevelopment because queries in pouchdb and sqlite are very different
As of this moment there is no way of using SQLite with ionic in the browser. Did you maybe find a solution to your own problem?
For anyone reading this that wants to purely use SQLite in their app (instead of localstorage) you can use a livereload build on a test device and easily debug and test your code this way. Just connect dev tools and see all the console messages.

To use SQLite with Datasnap

Is it ok to use SQLite as a backend database in Datasnap ? Will there be a danger of database locking since SQLite is filebased or will the datasnap server provide the necessary backbone for this not to happen ?
I don't know about Datasnap, but with working directly with SQLite (...) I know SQLite is able to use one file/database from several processes and keep track of lock using mutexes. (See more about sqlite's mutex functions here) How they work exactly depends on which compile settings were used when building sqlite3.dll. I've been using the precompiled binaries from the website without any problem.

How to integrate sqlcipher with qt5

I'm completely new to Qt. Actually I'm trying to build an app, which stores it's data to an encrypted sqlite database file. Ok, I've build sqlcipher successfully and found the files under ./libs (I'm on a Mac).
Where do I've to put the files in my Qt project for development, and later on, for deploy it with the app?
qDebug() << QSqlDatabase::drivers();
gives me
("QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3", "QPSQL", "QPSQL7")
Thanks in advance.
I don't think a driver for SQLCipher exists in Qt. You'll only get the default drivers, which include SQLite.
I know two options: you use sqlcipher with its C API like a normal library, link to your app and you're done. Or you create the driver yourself. Creating that driver should be pretty simple as it should be almost identical to the one for sqlite, being the API almost identical. Simply linking the sqlite driver to sqlcipher instead of sqlite should be enough.
I found this online that should help you. But there are probably also other useful resources. That seems to simply re-use the sqlite plugin, linking to sqlcipher.

How to connect Dart to SQLite?

My old application used web2py with SQLite as the database. Now I want to try porting this app to Dart and again use SQLite as the database.
I can't find any documentation on how to use it. I just found out how to use MySQL with sqljocky.
I've tried to read the web_sql and indexed_db APIs, but I can't find a way to connect to SQLite. How can I use SQLite from Dart?
As far as I can tell, dart-sqlite is the only public attempt at SQLite bindings for Dart, but it's out of date. My guess is that it would actually be pretty easy to update the code and get it up-and-running.
web_sql and indexed_db are both for use in the browser, which, as far as I can tell from your question, isn't what you're looking for.
Update: I updated dart-sqlite so that all the tests now pass, at least on my machine. You're definitely venturing into uncharted territory if you use it, but it's a start.

Is there a sqlite gui that supports custom collations?

I have a custom collation sequence that I named NOCASE_UTF8 that I'm using in a couple sqlite dbs for an iPhone app I made. I would love to use a sqlite gui on my Mac to be able to manage my dbs rather than doing it all through my app's C code. I would also like to not have to roll my own gui tool JUST to handle this one problem.
I've tried a couple sqlite guis and neither one allows you to interact with collation unless I made a horrible oversight. I've tried using SQLite Manager and Base to insert rows into my tables that use the custom collation. Both just blow up with the error message "no such collation sequence: NOCASE_UTF8". I saw this answer for a list of guis but nothing I've read of the guis talks about custom collation support.
Am I just SOL or is there a sqlite gui out there with custom collation support?
I haven't tested it, but this solution is open source so if it doesn't support custom collation, you could add it yourself... if you dare.
http://code.google.com/p/phpliteadmin/
There are very few options for Mac OS in this category. I've always used the Firefox addon, but I've never used weird collations.

Resources