-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to consume SQLCipher using Xamarin Forms Template #508
Comments
You might need to move that bit of code into a separate project. For example, create a Droid library project, move the droid dependency service stuff in there, reference it from your Droid app project, and reference sqlite-net-pcl from that droid library project. |
Hey @ericsink ! Thanks for the quick answer! But into the Droid Library project should I use sql-net-pcl or just the SQLitePCLRaw.bundle_sqlcipher? Or should I use the sql-net-pcl in the Droid Library and the SQLitePCLRaw.bundle_sqlcipher in the Droid App? |
I think in the droid library you will need to use sqlite-net-pcl and in the droid app, use bundle_sqlcipher (and not sqlite-net-pcl) Let me know if this works. I'm not 100% sure about it. |
@ericsink trying right now! |
Didn't work @ericsink. The Droid App project claim for the SQLite-net assembly |
"The Droid App project claim for the SQLite-net assembly" I don't understand what this means. |
I'm sorry if my question is an off topic but how to enable sqlcipher features in my project? I mean, where I have to enter a password when creating / opening of the database? |
Oh, thanks Eric! |
Hi @acidbless , This might help you. I have tested in Android device & encryption is working properly. Sorry I could not add separate unit test.I am still wondering how to write unit test executable from device. Any help in this regard would be very helpful. ** if any chance if app crashes on launch in android emulator, please use real device to test. |
@supratimpatra unfortunately that link is broken, I'm using the sqlite-net-pcl plugin for a xamarin forms cross platform app, but i'm totally stumped as to how to add sqlcipher? What level of encryption does it implement? I must have missed it on the docs? Do I need to add the SQLitePCLRaw.bundle_sqlcipher bundle to all the projects or just the ios/android and uwp ones? any starter for 10 greatly appreciated :-) |
@ericsink thanks for the quick reply. Currently I initialise the db in the pcl library, using a new SQLiteConnection. This uses the dependency service to locate the target db file as follows Also I couldn't find any docs on the type of encryption used? |
Oh, yeah... To actually use the encryption features, you'll need to use PRAGMA key. See this link: |
okay so I've added reference sqlitepclraw.bundle_sqlcipher to each of my app projects (ios, and android and UWP). now I can just go ahead and use the following code |
I haven't used sqlcipher myself in a while, so my answers might be wrong-ish around the edges, but... Yeah, it's basically that easy. To confirm that encryption is really happening, I would grab the sqlite file out of your device or emulator, copy it to something like a unix system, and open it with regular sqlite. It should be unreadable. For extra fun, run Unix 'strings' on it and see that none of the strings you inserted as data are present in the output. |
unbelievable!!! @ericsink you sir are a star!! so I just ran it and tried opening the db file with DB browser for sqlite....no dice, invalid file format.... then deleted and re-ran and tried to open the database, this time without encryption, and I could see all the data. ....really realy impressed, thx very much for your work. just to re-cap in case it helps anyone else add the sqlite_net_pcl package to the PCL project add create a connection in you pcl...probably using the dependency service now the crucial part which keys the database, using a PRAGMA, see docs here https://www.zetetic.net/sqlcipher/sqlcipher-api/
and every subsequent operation to the database will be encrytped, by my calculations with default 256bit AES cbc |
@mawarnes added sqlitepclraw.bundle_sqlcipher to my app projects and added In my PCL project: but whenever I try to open in DB Browser for SQL Lite I get "Error changing data: attempt to write a readonly database and in SQLite Studio I try top open it as SQLCipher with the pass as "pragmakey" I get "could not add database" Whenever I try to read from it I get: Edit: |
Michael, Will you be able to post the sample code where Sqlcipher worked with Xamarin forms? |
Sorry @mawarnes I was not actively following this thread. Here is the repo with updated SQLCipher package. https://github.com/supratimpatra/Sqlcipher_sqlite-net-pcl_Xamarin.Forms.git |
I am working on a better nuget package to make this easier in #597 For now, you can use the beta nuget package |
I followed as suggested, Added sqlite-net pcl to my PCL project and added sqlitepclraw.bundle_sqlcipher to my iOS project, initialised "SQLitePCL.Batteries_V2.Init();" in appdelegate.cs, below is my code to create DB connection for the first time in PCL project.
But when I run the app, it can't retrieve any data from the database, results are 0. I pulled the database from the device to check if it is encrypted, opened it in "DB Browser for SQLite" on Mac and it does not ask for any password just opens fine.
|
I think you need to give PRAGMA key a string literal:
|
Sorry I tried that, but it doesn't seems to work! I'm also using SQLiteNetExtensions in PCL project, The SQLiteNetExtensions has dependency on SQLite.Net-PCL (by Øystein Krog,Frank Krueger,Tim Heuer), I've sqqlite-net-pcl by @praeclarum installed, does this create any conflicts? Edit:
|
Ok, After so many trial and errors I got this working, here is some background about my App. Step1: I used tutorial by James Montemagno to convert my existing PCL project into .NET Standard Step 2: Removed all SQLite related dependencies from my project, restarted Visual Studio. Step 3: Now added sqlite-net-sqlcipher v1.6.258-beta into both iOS and Main projects. Here the version is very much important, better install v1.6.258-beta or above. Step 4: This is optional if you are using SQLiteNetExtenions by twincoders for ORM, kindly use SQLiteNetCipherExtensions by same developers, I'm using v2.1.0 Step 5: Make sure SQLitePCLRaw.bundle_sqlcipher is installed in platform-specific projects (Android or iOS) Step 6: Initialize SQLitePCL.Batteries_V2.Init(); in main pages of platform specific projects. Now all set, you can now open already encrypted database: Or encrypt a plaintext database |
Reading the wiki from ericsink for SQLitePCL.raw, it's says that I have to install the sqlite-net-pcl only for the PCL project, for the App project (iOS and Android) I have to install only the SQLitePCLRaw.bundle_sqlcipher, by doing that my class that's servers as Dependency Service for my PCL to initiate the connection with the sqlite database file on Android App can't find the correct assembly, for sqlite-net-pcl.
The text was updated successfully, but these errors were encountered: