Skip to content
.NET 开发者指北.NET 开发者指北
CMS
.NET指北
FreeKit
Docker
关于
博客
github icon
    • Blogs分享
      • git emoji
        • .NET+Sqlite 如何支持加密
          • 相关文章
            • Sqlite
              • Sqlite 加密
                • System.Data.SQLite.Core
                  • Microsoft.Data.Sqlite
                    • ADO.NET 修改 Sqlite 密码
                  • .NET 编码的基础知识
                    • C#委托的介绍
                      • 多个 IFreeSql实例,如何注入使用

                      .NET+Sqlite 如何支持加密

                      calendar icon2022年1月7日timer icon大约 3 分钟word icon约 819 字

                      此页内容
                      • 相关文章
                      • Sqlite
                      • Sqlite 加密
                      • System.Data.SQLite.Core
                      • Microsoft.Data.Sqlite
                        • ADO.NET 修改 Sqlite 密码

                      # .NET+Sqlite 如何支持加密

                      # 相关文章

                      • FreeSql.Provider.SqliteCore 如何加密open in new window

                      # Sqlite

                      SQLite 来源于公共领域 SQLite Is Public Domain、 确保代码不会受到任何专有或许可内容的污染,没有任何来自互联网上的未知来源复制。即全是原创的。

                      虽然是免费的,无需许可证,可用于任何目的,但如果你的公司必须要一个许可证,你也能申请授权https://sqlite.org/purchase/licenseopen in new window.

                      但不支持加密。如果想支持登录加密,需要另外的扩展SQLite 加密扩展(SQLite Encryption Extension,),具有读取/写入 AES 加密数据库的附加功能。具体授权可参考 https://www.sqlite.org/prosupport.htmlopen in new window

                      # 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
                      
                      1
                      2
                      3

                      如果底层依赖于Microsoft.Data.Sqlite 也会提示

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

                      # System.Data.SQLite.Core

                      创建一个控制台项目,起名 OvOv.SqliteSystemCore

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

                      安装包

                      dotnet add package System.Data.SQLite.Core
                      
                      1

                      使用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();
                      
                      1
                      2
                      3
                      4
                      5
                      6
                      7
                      8
                      9
                      10
                      11
                      12
                      13
                      14

                      运行项目

                      dotnet run
                      
                      1

                      就会出现如下错误。

                      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'.
                      系统找不到指定的文件。”
                      
                      1
                      2
                      3

                      # Microsoft.Data.Sqlite

                      创建一个控制台项目,起名 OvOv.SqliteMicrosoft

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

                      安装包

                      dotnet add package Microsoft.Data.Sqlite
                      
                      1

                      使用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();
                      
                      1
                      2
                      3
                      4
                      5
                      6
                      7
                      8
                      9
                      10
                      11
                      12
                      13
                      14
                      15
                      16

                      运行项目

                      dotnet run
                      
                      1

                      就会出现如下错误。

                      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()
                      
                      1
                      2
                      3

                      其实微软已经提供了加密的方案。

                      • https://docs.microsoft.com/zh-cn/dotnet/standard/data/sqlite/encryption?tabs=netcore-cliopen in new window
                      dotnet remove package Microsoft.Data.Sqlite
                      dotnet add package Microsoft.Data.Sqlite.Core
                      dotnet add package SQLitePCLRaw.bundle_e_sqlcipher
                      
                      1
                      2
                      3

                      重新运行项目 ,就会发现,他正常执行。没有任何报错。

                      有关使用不同的本机库进行加密的详细信息,请参阅自定义 SQLite 版本open in new window。

                      我们从 自定义 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);
                      
                      1
                      2
                      3
                      4
                      5
                      6
                      7
                      8
                      9
                      10
                      11
                      12
                      13
                      14
                      15
                      16
                      17
                      18
                      19
                      20
                      21
                      22
                      23
                      24
                      25
                      26
                      27
                      28
                      29

                      # 完整代码

                      • https://github.com/luoyunchong/dotnetcore-examples/blob/master/Database-Drivers/OvOv.SqliteMicrosoftCore/Program.csopen in new window
                      edit icon在 GitHub 上编辑此页open in new window
                      上次编辑于: 2022/9/13 11:34:29
                      贡献者: igeekfan,igeekfan,luoyunchong
                      上一页
                      git emoji
                      下一页
                      .NET 编码的基础知识
                      MIT Licensed | Copyright © 2021-present luoyunchong
                      苏ICP备16046457号-1

                      该应用可以安装在你的 PC 或移动设备上。这将使该 Web 应用程序外观和行为与其他应用程序相同。它将在出现在应用程序列表中,并可以固定到主屏幕,开始菜单或任务栏。此 Web 应用程序还将能够与其他应用程序和你的操作系统安全地进行交互。

                      详情