.NET+Sqlite如何支持加密
點擊上方藍(lán)字關(guān)注我們
.NET+Sqlite如何支持加密
Sqlite
SQLite?來源于公共領(lǐng)域?SQLite Is Public Domain、
確保代碼不會受到任何專有或許可內(nèi)容的污染,沒有任何來自互聯(lián)網(wǎng)上的未知來源復(fù)制。即全是原創(chuàng)的。
雖然是免費(fèi)的,無需許可證,可用于任何目的,但如果你的公司必須要一個許可證,你也能申請授權(quán)https://sqlite.org/purchase/license.
但不支持加密。如果想支持登錄加密,需要另外的擴(kuò)展SQLite 加密擴(kuò)展(SQLite Encryption Extension,),具有讀取/寫入 AES 加密數(shù)據(jù)庫的附加功能。具體授權(quán)可參考 https://www.sqlite.org/prosupport.html
Sqlite加密
一直以來,FreeSql開發(fā)群中,總會有一些開發(fā)者來詢問Sqlite加密的問題,事實上,官方提供的Sqlite加密功能是收費(fèi)的。當(dāng)連接串上使用Password時,會提示授權(quán)問題。
如果底層依賴于System.Data.SQLite.Core,
如果底層依賴于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
創(chuàng)建一個控制臺項目,起名?OvOv.SqliteSystemCore
dotnet new console -n OvOv.SqliteSystemCore cd OvOv.SqliteSystemCore安裝包
dotnet add package System.Data.SQLite.Core使用SQLiteConnection創(chuàng)建一個連接,使用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();運(yùn)行項目
dotnet run就會出現(xiàn)如下錯誤。
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'. 系統(tǒng)找不到指定的文件。”Microsoft.Data.Sqlite
創(chuàng)建一個控制臺項目,起名?OvOv.SqliteMicrosoft
dotnet new console -n OvOv.SqliteMicrosoft cd OvOv.SqliteMicrosoft安裝包
dotnet add package Microsoft.Data.Sqlite使用SqliteConnection創(chuàng)建一個連接,使用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();運(yùn)行項目
dotnet run就會出現(xiàn)如下錯誤。
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()其實微軟已經(jīng)提供了加密的方案。
https://docs.microsoft.com/zh-cn/dotnet/standard/data/sqlite/encryption?tabs=netcore-cli
重新運(yùn)行項目 ,就會發(fā)現(xiàn),他正常執(zhí)行。沒有任何報錯。
有關(guān)使用不同的本機(jī)庫進(jìn)行加密的詳細(xì)信息,請參閱自定義 SQLite 版本。
我們從 自定義 SQLite 版本上可以看到。
默認(rèn)情況下,主?Microsoft.Data.Sqlite?包引入?SQLitePCLRaw.bundle_e_sqlite3。若要使用不同的捆綁,請改為安裝?Microsoft.Data.Sqlite.Core?包以及要使用的捆綁包。
SQLitePCLRaw.bundle_e_sqlcipher
提供 SQLCipher 的非官方開放源代碼內(nèi)部版本。此版本支持加密。
完整代碼
https://github.com/luoyunchong/dotnetcore-examples/blob/master/Database-Drivers/OvOv.SqliteMicrosoftCore/Program.cs
可點擊原文查看
Nacos配置中心+ASP.NET Core
除了Swagger UI,你還能選擇 IGeekFan.AspNetCore.RapiDoc
github訪問慢,npm 等 半天,dev-sidecar這個工具幫你輕松搞定一切。
點個在看你最好看
總結(jié)
以上是生活随笔為你收集整理的.NET+Sqlite如何支持加密的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Dotnet 6.0 深度探索(一)
- 下一篇: c#winform自定义窗体(含源码)