.NET+Sqlite支持加密的操作方法

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了.NET+Sqlite支持加密的操作方法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

SQLITe

@H_512_3@SQLite 来于公共领域 SQLite Is Public Domain
确保代码不会受到任何专有或许可内容的污染,没有任何来自互联网上的未知来源复制。即全是原创的。

虽然是免费的,无需许可证,可用于任何目的,但如果你的公司必须要一个许可证,你也能申请授权https://sqlite.org/purchase/license.

但不支持加密。如果想支持登录加密,需要另外的扩展SQLite 加密扩展(SQLite Encryption Extension,),具有读取/写入 AES 加密数据库的附加功能。具体授权可参考 https://www.sqlite.org/prosupport.html

Sqlite加密

一直以来,FreeSql开发群中,总会有一些开发者询问Sqlite加密的问题,事实上,官方提供的Sqlite加密功能是收费的。当连接串上使用Password时,会提示授权问题。
如果底层依赖于System.Data.SQLite.Core

Could not load file or asSEMbly 'System.Data.SQLite.SEE.License,
Version=1.0.115.5, Culture=neutral, PublicKeyToken=433d9874d0bb98c5,
PRocessorArchitecture=MSIL

如果底层依赖于Microsoft.Data.Sqlite 也会提示

You sPEcified a password in the connection string, but the native SQLite

library 'e_sqlite3' doesn't support encryption.

System.Data.SQLite.Core

创建一个控制台项目,起名 OvOv.SqliteSystemCore

dotnet new console -n OvOv.SqliteSystemCore
cd OvOv.SqliteSystemCore

安装包

dotnet add package System.Data.SQLite.Core

使用SQLiteConnection创建一个连接,使用Password指定密码

using System.Data.SQLite;

static void Open()
{
    string baseConnectionString = "Data Source=local.db";
    VAR connectionString = new SQLiteConnectionStringBuilder(baseConnectionString)
    {
        Password = "123qwe"
    }.ToString();

    using SQLiteConnection? connection = new SQLiteConnection(connectionString);
    connection.Open();
}
Open();

运行项目

dotnet run

就会出现如下错误。

System.IO.FileNotFoundException:“Could not load file or assembly

'System.Data.SQLite.SEE.License, Version=1.0.115.5, Culture=neutral, PublicKeyToken=433d9874d0bb98c5, processorArchitecture=MSIL'.

系统找不到指定的文件。”

Microsoft.Data.Sqlite

创建一个控制台项目,起名 OvOv.SqliteMicrosoft

dotnet new console -n OvOv.SqliteMicrosoft
cd OvOv.SqliteMicrosoft

安装包

dotnet add package Microsoft.Data.Sqlite

使用SqliteConnection创建一个连接,使用Password指定密码

using Microsoft.Data.Sqlite;

static void Open()
{
    string baseConnectionString = "Data Source=local.db";
    var connectionString = new SqliteConnectionStringBuilder(baseConnectionString)
    {
        Mode = SqliteOpenMode.ReadWriteCreate,
        Password = "123qwe"
    }.ToString();

    using SqliteConnection? connection = new SqliteConnection(connectionString);
    connection.Open();
}

Open();

运行项目

dotnet run

就会出现如下错误。

Unhandled exception. System.InvalidoperationException: You specified a password in the connection string, 

but the native SQLite library

'e_sqlite3' doesn't support encryption. at Microsoft.Data.Sqlite.SqliteConnection.Open()

其实微软已经提供了加密的方案

https://docs.microsoft.com/zh-cn/dotnet/standard/data/sqlite/encryption?tabs=netcore-cli

dotnet remove package Microsoft.Data.Sqlite
dotnet add package Microsoft.Data.Sqlite.Core
dotnet add package SQLitePCLRaw.bundle_e_sqlcipher

重新运行项目 ,就会发现,他正常执行。没有任何报错。

有关使用不同的本机库进行加密的详细信息,请参阅自定义 SQLite 版本

我们从 自定义 SQLite 版本上可以看到。

默认情况下,主 Microsoft.Data.Sqlite 包引入 SQLitePCLRaw.bundle_e_sqlite3。 若要使用不同的捆绑,请改为安装 Microsoft.Data.Sqlite.Core 包以及要使用的捆绑包。

SQLitePCLRaw.bundle_e_sqlcipher

提供 SQLCipher 的非官方开放源代码内部版本。此版本支持加密。

完整代码

https://github.COM/luoyunchong/dotnetcore-examples/blob/master/Database-Drivers/OvOv.SqliteMicrosoftCore/Program.cs

到此这篇关于.NET+Sqlite如何支持加密的文章就介绍到这了,更多相关.NET Sqlite加密内容请搜索脚本宝典以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本宝典!

脚本宝典总结

以上是脚本宝典为你收集整理的.NET+Sqlite支持加密的操作方法全部内容,希望文章能够帮你解决.NET+Sqlite支持加密的操作方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: