4

I am having issues trying to encrypt a SQLite database. I have tried numerous things so far but none seem to work.

  • Initially I have used the (unsupported) SQLiteConnection.SetPassword() method provided by System.Data.SQLite, however since a while this method has been removed from the System.Data.SQLite package. Trying to take an older SQLite version turned out to be difficult since UWP is UAP and not .NET Framework.
  • Therefore I have moved to Microsoft.Data.Sqlite.Core and used this documentation for installing the SQLitePCLRaw.bundle_e_sqlcipher package and calling either SQLitePCL.Batteries_V2.Init() or SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provided_e_sqlcipher()). Then executing the query PRAGMA KEY = 'password' does not encrypt the database file.
  • I have also seen posts where people used SQLitePCLRaw.bundle_sqlcipher rather than ...e_sqlcipher however in that case the ...V2.Init() did not work.
  • I have also looked into using sqlite-net-sqlcipher however executing SQLiteConnection.Query<int>("PRAGMA key = 'password'"); does also not encrypt the database. When trying to query to the database using key: "password" will then result in The file is no database error denoting the lack of password on the database.

How can I encrypt a SQLite database for UWP, preferably using Microsoft.Data.Sqlite but other options are fine as well.
I am also primarily looking for a free way to achieve this, since $900 is quite over-budget for the project I am working on.

Leander
  • 854
  • 4
  • 17

1 Answers1

2

Although I did not test with every package I listed, I found an answer to my question:

First I followed the instruction from the answer posted on this question. Then instead of trying to PRAGMA key = 'password' an existing database, instead I created a new database with key = 'password' specified in the SQLiteConnectionString constructor.

This created a new, already encrypted, database for me.

EDIT: I have now also tested this with Microsoft.Data.Sqlite.Core and SQLitePCLRaw.bundle_e_sqlcipher and it turns out that also there creating a new database in combination with specifying Password = 'password' in for example the SqliteConnectionStringBuilder will create a new database which is encrypted.

I have posted an example project with code here

Leander
  • 854
  • 4
  • 17
  • @CarloMendoza https://github.com/Leander-van-Boven/UWP-Encrypted-SQLite I hope this helps, please let me know if anything isn't clear. – Leander Apr 21 '21 at 14:57