3

I want to use C# (winform) to read an SQLCipher database and find an open source project SQLitePCL, but I have not found examples of this use, and the documentation is not a lot. Have any friends used this thing, can you provide some examples to help me?

Thank you!

桑榆肖物
  • 73
  • 2
  • 8

1 Answers1

5

This will work for winforms, wpf, asp.net, xamarin.android, xamarin.ios, UWP and any other .net project since this is a cross-platform library:

Install nuget by Frank A. Krueger "sqlite-net-pcl".

Use tutorial from his github page.

Now, once we have database and all the stuff. How to encrypt newly created database or open encrypted database?

Add nuget by Eric Sink: "SQLitePCLRaw.bundle_sqlcipher"

In the code, where you create SQLiteConnection specify encryption key:

connection = new SQLiteConnection(dbPath, openFlags: SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.FullMutex | SQLiteOpenFlags.Create);
connection.Query<int>("PRAGMA key=xzy1921");

That's it. The rest will work as if it was not crypted.

Access Denied
  • 8,723
  • 4
  • 42
  • 72
  • I should use it in winform program . Error The package "sqlite-net-pcl 1.4.118" could not be installed. You are trying to install this package into a project with the target ".NETFramework, Version=v3.5", but the package does not contain any assembly references or content files that are compatible with the framework. For more information, please contact the package author. 0 – 桑榆肖物 Sep 07 '18 at 06:31
  • Don't use .net 3.5. Update you winforms app to use .NET 4.7 – Access Denied Sep 07 '18 at 06:39
  • https://blogs.msdn.microsoft.com/cdndevs/2015/08/20/upgrading-projects-to-net-4-6/ – Access Denied Sep 07 '18 at 06:40
  • 2
    Add nuget by Eric Sink: "SQLitePCLRaw.bundle_sqlcipher" 。 What special operations are needed after that? SQLite.SQLiteException:“file is not a database” – 桑榆肖物 Sep 07 '18 at 07:21
  • @诺倾情 It means that you are trying to open database with password even though it's not encrypted. Or you open encrypted database without password or with incorrect one. – Access Denied Sep 07 '18 at 08:12
  • but i can use sqlcipher.exe open this EnMsg.db whith mypassword. Thank you for helping me so much, let me try other methods. – 桑榆肖物 Sep 07 '18 at 09:24
  • @诺倾情 I tested proposed solution on my app. Not quite sure how come that it does not work for your db. – Access Denied Sep 07 '18 at 09:26
  • @诺倾情 I had the same problem trying to read databases created by the Android Room SQLCipher extension and using the sqlite-net-sqlcipher NuGet package worked for me: https://www.nuget.org/packages/sqlite-net-sqlcipher – rjschnorenberg Oct 13 '22 at 17:20