SQLCipher 集成到 Xamarin.Forms;SQLite 库不支持加密例外



我正在将SQLCipher实现到Xamarin.Forms应用程序中。我以为一切正常,直到我注意到X.F.应用程序创建的数据库实际上是没有加密或密码的SQLite3 DB。研究了一段时间后,我仍然无法找到解决方案。我遇到一个异常,上面写着

System.InvalidOperationException: 'You specified a password in the connection string, but the native SQLite library you're using doesn't support encryption.'

我目前在此解决方案中有 4 个项目。XamarinForms 中的标准 3(跨平台内容的默认 PCL、Project.Android 和 Project.iOS(。除了这 3 个之外,我还有一个标记为 Project.Core 的自定义 PCL。此 PCL 负责所有 DataAccess,因为它实现了存储库模式、工作单元、DbContext 等。

在第 4 个项目中,在我的 DbContext.cs 类中,我有这个:

// Added for more context
using System;
using System.IO;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Xamarin.Forms;

private SqliteConnection connection;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string connStr = Path.Combine(
path1: Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
path2: "App.db");
string passStr = deviceIdentifier;
string path = Path.GetDirectoryName(connStr);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
// Check if db file exists
if (!File.Exists(connStr))
{
FileStream stream = File.Create(connStr);
stream.Close();
}
// DOCS => https://learn.microsoft.com/en-us/dotnet/standard/data/sqlite/encryption?tabs=netcore-cli
// => https://www.bricelam.net/2016/06/13/sqlite-encryption.html
var connectionString = new SqliteConnectionStringBuilder()
{
DataSource = connStr,
Mode = SqliteOpenMode.ReadWriteCreate,
Password = passStr
}.ToString();
// NOTE: THIS IS WHERE THE EXCEPTION IS THROWN!!!
// THE CODE BELOW THIS IS AN ALTERNATE ROUTE THAT DOENS'T WORK EITHER
**connection.Open();**
// This code doesn't throw anything, but it doesn't key the DB either
using (SqliteCommand command = connection.CreateCommand())
{
command.CommandText = "SELECT quote($password);";
command.Parameters.AddWithValue("$password", passStr);
string escapedPassword = (string)command.ExecuteScalar(); // Protects against SQL injection
command.CommandText = "PRAGMA key = " + escapedPassword /*+ ";"*/;
command.Parameters.Clear();
command.ExecuteNonQuery();
}
#if DEBUG
optionsBuilder.EnableSensitiveDataLogging();
#endif
optionsBuilder.UseSqlite(connection);
SQLitePCL.Batteries_V2.Init();
}        

通过我的研究,似乎此 PCL 中的一个 SQLite/SQLCipher 包可能存在问题(PCL 针对的是 .NET Standard 2.0 以供参考(。

我目前有:

  • Microsoft.Data.Sqlite.Core 3.1.1 (w/依赖于 Microsoft.Data.Sqlite.dll & SQLitePCLRaw.core 2.0.2(
  • SQLitePCLRaw.bundle_sqlcipher 1.1.14 (w 依赖于 SQLitePCLRaw.core 2.0.2, SQLitePCLRaw.batteries_sqlcipher.dll, SQLitePCLRaw.batteries_v2.dll(

其他几点需要注意:

  • 查看 SQLitePCL 命名空间时,它将包显示为sqlitepclraw.bundle_e_sqlite3,而不是具有对 sqlcipher 的引用。.nugetpackagessqlitepclraw.bundle_e_sqlite32.0.2libnetstandard2.0SQLitePCLRaw.batteries_v2.dll
  • 我相信该依赖项可能存在问题,但我不确定,并希望得到任何帮助!

提前谢谢。

PS - 可根据要求提供更多信息

找到了一个可行的解决方案。

在查看软件包后,我发现用此处找到的SQLitePCLRaw.bundle_zetetic替换现有的SQLitePCLRaw捆绑包解决了连接和维护加密数据库的问题。

工作代码片段是:

// StringBuilder here, and the SqliteConnection below are 
// from the Microsoft.Data.Sqlite namespace v3.1.1
var connectionString = new SqliteConnectionStringBuilder()
{
DataSource = connStr,
Mode = SqliteOpenMode.ReadWriteCreate,
Password = passStr
}.ToString();
connection = new SqliteConnection(connectionString);
connection.Open();

最新更新