. NET+Sqlite how to support encryption

Posted by haydndup on Tue, 04 Jan 2022 11:50:37 +0100

. NET+Sqlite how to support encryption

Sqlite

SQLite comes from the public domain
Ensure that the code is not contaminated by any proprietary or licensed content and is not copied from any unknown source on the Internet. That is, it's all original.

Although it is free, does not need a license and can be used for any purpose, you can also apply for authorization if your company needs a license https://sqlite.org/purchase/license.

But encryption is not supported. If you want to support login encryption, you need another extension SQLite Encryption Extension, which has the additional function of reading / writing AES encrypted database. For specific authorization, please refer to https://www.sqlite.org/prosupport.html

Sqlite encryption

For a long time, there are always some developers in the FreeSql development group to ask about Sqlite encryption. In fact, the official Sqlite encryption function is charged. When Password is used on the connection string, an authorization problem will be prompted.
If the underlying layer depends on 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

If the underlying layer depends on Microsoft Data. SQLite will also prompt

You specified a password in the connection string, but the native SQLite
library 'e_sqlite3' doesn't support encryption.

System.Data.SQLite.Core

Create a console project named ovov SqliteSystemCore

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

Installation package

dotnet add package System.Data.SQLite.Core

Use SQLiteConnection to create a connection, and use Password to specify the 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();

Run project

dotnet run

The following error will occur.

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'.
The system cannot find the specified file

Microsoft.Data.Sqlite

Create a console project named ovov SqliteMicrosoft

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

Installation package

dotnet add package Microsoft.Data.Sqlite

Use SqliteConnection to create a connection and Password to specify the 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();

Run project

dotnet run

The following error will occur.

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

In fact, Microsoft has provided an encryption scheme.

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

Re run the project and you will find that it is running normally. There are no errors reported.

For more information about using different native libraries for encryption, see Custom SQLite version.

We can see from the custom SQLite version.

By default, the primary Microsoft Data. SQLite package introduces sqlitepclraw bundle_ e_ sqlite3. To use a different bundle, install microsoft.com instead Data. Sqlite. Core packages and bundles to use.

SQLitePCLRaw.bundle_e_sqlcipher

Provide unofficial open source builds of SQLCipher. This version supports encryption.

Complete code

Topics: SQLite .NET