.NET+Sqlite如何支持加密


.NET+Sqlite如何支持加密

相關文章

Sqlite

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()

其實微軟已經提供了加密的方案。

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 的非官方開放源代碼內部版本。此版本支持加密

ADO.NET 修改Sqlite密碼

static int UpdatePassword(string oldPassword, string newPassword)
{
    string baseConnectionString = "Data Source=local.db";
    var connectionString = new SqliteConnectionStringBuilder(baseConnectionString)
    {
        Mode = SqliteOpenMode.ReadWriteCreate,
        Password = oldPassword
    }.ToString();

    using (var connection = new SqliteConnection(connectionString))
    {
        connection.Open();
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT quote($newPassword);";
            command.Parameters.AddWithValue("$newPassword", newPassword);
            var quotedNewPassword = command.ExecuteScalar() as string;

            command.CommandText = "PRAGMA rekey = " + quotedNewPassword;
            command.Parameters.Clear();
            int x = command.ExecuteNonQuery();
            return x;
        }
    }
}

string oldPassword = "123qwe";
string newPassword = "abcd";
UpdatePassword(oldPassword, newPassword);

完整代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM