.NET+Sqlite支持加密的操作方法
Sqlite
SQLite
來源于公共領(lǐng)域 SQLite Is Public Domain
、
確保代碼不會受到任何專有或許可內(nèi)容的污染,沒有任何來自互聯(lián)網(wǎng)上的未知來源復(fù)制。即全是原創(chuàng)的。
雖然是免費的,無需許可證,可用于任何目的,但如果你的公司必須要一個許可證,你也能申請授權(quán)https://sqlite.org/purchase/license.
但不支持加密。如果想支持登錄加密,需要另外的擴展SQLite 加密擴展(SQLite Encryption Extension,),具有讀取/寫入 AES 加密數(shù)據(jù)庫的附加功能。具體授權(quán)可參考 https://www.sqlite.org/prosupport.html
Sqlite加密
一直以來,FreeSql
開發(fā)群中,總會有一些開發(fā)者來詢問Sqlite
加密的問題,事實上,官方提供的Sqlite加密功能是收費的。當連接串上使用Password
時,會提示授權(quán)問題。
如果底層依賴于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
創(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();
運行項目
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();
運行項目
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
dotnet remove package Microsoft.Data.Sqlite dotnet add package Microsoft.Data.Sqlite.Core dotnet add package SQLitePCLRaw.bundle_e_sqlcipher
重新運行項目 ,就會發(fā)現(xiàn),他正常執(zhí)行。沒有任何報錯。
有關(guān)使用不同的本機庫進行加密的詳細信息,請參閱自定義 SQLite 版本。
我們從 自定義 SQLite 版本上可以看到。
默認情況下,主 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
到此這篇關(guān)于.NET+Sqlite如何支持加密的文章就介紹到這了,更多相關(guān).NET?Sqlite加密內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
asp.net+ajax+sqlserver自動補全功能實現(xiàn)解析
這篇文章主要介紹了asp.net + ajax + sqlserver 自動補全功能,需要的朋友可以參考下2014-03-03詳解ASP.NET Core 之 Identity 入門(二)
本篇文章主要介紹了ASP.NET Core 之 Identity 入門,主要負責對用戶的身份進行認證,有興趣的可以了解一下。2016-12-12