.NET+Sqlite支持加密的操作方法
Sqlite
SQLite
來(lái)源于公共領(lǐng)域 SQLite Is Public Domain
、
確保代碼不會(huì)受到任何專(zhuān)有或許可內(nèi)容的污染,沒(méi)有任何來(lái)自互聯(lián)網(wǎng)上的未知來(lái)源復(fù)制。即全是原創(chuàng)的。
雖然是免費(fèi)的,無(wú)需許可證,可用于任何目的,但如果你的公司必須要一個(gè)許可證,你也能申請(qǐng)授權(quán)https://sqlite.org/purchase/license.
但不支持加密。如果想支持登錄加密,需要另外的擴(kuò)展SQLite 加密擴(kuò)展(SQLite Encryption Extension,),具有讀取/寫(xiě)入 AES 加密數(shù)據(jù)庫(kù)的附加功能。具體授權(quán)可參考 https://www.sqlite.org/prosupport.html
Sqlite加密
一直以來(lái),FreeSql
開(kāi)發(fā)群中,總會(huì)有一些開(kāi)發(fā)者來(lái)詢(xún)問(wèn)Sqlite
加密的問(wèn)題,事實(shí)上,官方提供的Sqlite加密功能是收費(fèi)的。當(dāng)連接串上使用Password
時(shí),會(huì)提示授權(quán)問(wèn)題。
如果底層依賴(lài)于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
如果底層依賴(lài)于Microsoft.Data.Sqlite
也會(huì)提示
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)建一個(gè)控制臺(tái)項(xiàng)目,起名 OvOv.SqliteSystemCore
dotnet new console -n OvOv.SqliteSystemCore cd OvOv.SqliteSystemCore
安裝包
dotnet add package System.Data.SQLite.Core
使用SQLiteConnection
創(chuàng)建一個(gè)連接,使用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)行項(xiàng)目
dotnet run
就會(huì)出現(xiàn)如下錯(cuò)誤。
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)建一個(gè)控制臺(tái)項(xiàng)目,起名 OvOv.SqliteMicrosoft
dotnet new console -n OvOv.SqliteMicrosoft cd OvOv.SqliteMicrosoft
安裝包
dotnet add package Microsoft.Data.Sqlite
使用SqliteConnection
創(chuàng)建一個(gè)連接,使用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)行項(xiàng)目
dotnet run
就會(huì)出現(xiàn)如下錯(cuò)誤。
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()
其實(shí)微軟已經(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
重新運(yùn)行項(xiàng)目 ,就會(huì)發(fā)現(xiàn),他正常執(zhí)行。沒(méi)有任何報(bào)錯(cuò)。
有關(guān)使用不同的本機(jī)庫(kù)進(jìn)行加密的詳細(xì)信息,請(qǐng)參閱自定義 SQLite 版本。
我們從 自定義 SQLite 版本上可以看到。
默認(rèn)情況下,主 Microsoft.Data.Sqlite
包引入 SQLitePCLRaw.bundle_e_sqlite3
。 若要使用不同的捆綁,請(qǐng)改為安裝 Microsoft.Data.Sqlite.Core
包以及要使用的捆綁包。
SQLitePCLRaw.bundle_e_sqlcipher
提供 SQLCipher 的非官方開(kāi)放源代碼內(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ASP.NET多彩下拉框開(kāi)發(fā)實(shí)例
有人曾經(jīng)提出開(kāi)發(fā)一個(gè)根據(jù)不同選擇而顯示不同顏色的管理工具,本文主要就是演示如何讀取系統(tǒng)顏色并在下拉框中的每個(gè)條目中顯示對(duì)應(yīng)的顏色,需要的朋友可以參考下2015-09-09ASP.NET中實(shí)時(shí)圖表的實(shí)現(xiàn)方法分享
這篇文章介紹了ASP.NET中實(shí)時(shí)圖表的實(shí)現(xiàn)方法,有需要的朋友可以參考一下2013-11-11asp.net+ajax+sqlserver自動(dòng)補(bǔ)全功能實(shí)現(xiàn)解析
這篇文章主要介紹了asp.net + ajax + sqlserver 自動(dòng)補(bǔ)全功能,需要的朋友可以參考下2014-03-03asp.net Repeater綁定時(shí)使用函數(shù)
asp.net repeater綁定函數(shù)使用實(shí)例2008-08-08詳解ASP.NET Core 之 Identity 入門(mén)(二)
本篇文章主要介紹了ASP.NET Core 之 Identity 入門(mén),主要負(fù)責(zé)對(duì)用戶(hù)的身份進(jìn)行認(rèn)證,有興趣的可以了解一下。2016-12-12