C# SQLCipher集成到Xamarin.Forms中;SQLite库没有';不支持加密异常
我正在Xamarin.Forms应用程序中实现SQLCipher。我以为一切都正常,直到我注意到X.F.应用程序创建的数据库实际上是一个SQLite3 DB w/o加密或密码。在研究了一段时间后,我还没有找到解决办法。我遇到了一个例外,那就是C# SQLCipher集成到Xamarin.Forms中;SQLite库没有';不支持加密异常,c#,sqlite,xamarin.forms,portable-class-library,sqlcipher,C#,Sqlite,Xamarin.forms,Portable Class Library,Sqlcipher,我正在Xamarin.Forms应用程序中实现SQLCipher。我以为一切都正常,直到我注意到X.F.应用程序创建的数据库实际上是一个SQLite3 DB w/o加密或密码。在研究了一段时间后,我还没有找到解决办法。我遇到了一个例外,那就是 System.InvalidOperationException: 'You specified a password in the connection string, but the native SQLite library you're using
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(跨平台stuff、Project.Android和Project.iOS的默认PCL)。除了这3个,我还有一个定制的PCL,标签是Project.Core。此PCL负责所有数据访问,因为它实现了存储库模式、工作单元、DbContext等
在第四个项目中,在我的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://docs.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标准2.0供参考)
我目前有:
- Microsoft.Data.Sqlite.Core 3.1.1(与Microsoft.Data.Sqlite.dll和SQLitePCLRaw.Core 2.0.2相关)
- SQLitePCLRaw.bundle\u sqlcipher 1.1.14(依赖于SQLitePCLRaw.core 2.0.2、SQLitePCLRaw.batters\u sqlcipher.dll、SQLitePCLRaw.batters\u v2.dll)
- 查看SQLitePCL命名空间时,它将包显示为sqlitepclraw.bundle_e_sqlite3,而不是对sqlcipher的引用。
\.nuget\packages\sqlitepclraw.bundle\u e\u sqlite3\2.0.2\lib\netstandard2.0\sqlitepclraw.batters\u v2.dll
- 我相信这种依赖性可能会有问题,但我不确定,希望能得到任何帮助李>
PS-可以根据要求提供更多信息找到有效的解决方案 在查看包之后,我发现用
SQLitePCLRaw.bundle\u zettic
found替换现有的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();
您可以稍后标记它,这可以帮助更多有相同问题的人:)。