【问题标题】:Where is "SetPassword" or "ChangePassword" in SQLiteConnection class?SQLiteConnection 类中的“SetPassword”或“ChangePassword”在哪里?
【发布时间】:2020-09-29 08:05:51
【问题描述】:

我只是在学习如何加密/解密 SQLite 数据库。
我找到了一种加密方法,如这篇文章SQLite with encryption/password protection

这个页面的最佳答案说我们可以使用SEE,wxSQLite,SQLCipher,SQLiteCrypt等...进行加密。

我能理解。

而且,另一个答案说我们可以使用:

SQLiteConnection conn = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
conn.SetPassword("password");
conn.open();

这个页面也有同样的说法: Password Protect a SQLite DB. Is it possible?

但是,SQLiteConnection 没有 SetPasswordChangePassword 方法。 我很困惑。

SetPaswordChangePassword 在哪里?
这些在SEEwxSQLiteSQLCipherSQLiteCrypt 中吗?

【开发环境】
VisualStudio2010 .NET 框架 4 客户端配置文件 System.Data.SQLite.dll (https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki)

我从那里下载了 zip,然后我选择了“System.Data.SQLite.dll”

【问题讨论】:

标签: c# sqlite


【解决方案1】:

如果你想加密/解密 SQLite 数据库。有一些信息给你

如果您有其他问题,请查看官方文档中的Specify the key section

加密和解密现有数据库的方法因您使用的解决方案而异。例如,您需要在 SQLCipher 上使用 sqlcipher_export() 函数。查看解决方案的文档以了解详细信息。

您可以使用 SQLCipher API 来完成您的工作:

  1. 如果你想建立一个加密数据库, 只需设置您的连接字符串,如下所示:Data Source = encryptedName.db; Password=YourPassword

  2. 如果您想更改加密数据库中的密码

    // you can use this in startup.cs
    using var changePasswordDb = new DBContext(
        new SqliteConnection(
                new SqliteConnectionStringBuilder()
                    {
                         DataSource = "encryptedName.db",
                         Mode = SqliteOpenMode.ReadWriteCreate,
                         Password = oldPassword
                    }.ToString()
            ));
    changePasswordDb.Database.ExecuteSqlRaw($"PRAGMA rekey = {newPassword}");
    
    // or use SqliteConnection
    var connection = new SqliteConnection("Data Source =encryptedName.db.db;Password=yourPassword");
    connection.Open();
    var command = connection.CreateCommand();
    command.CommandText = "PRAGMA rekey = " + newPassword;
    command.ExecuteNonQuery();  
    
  3. 如果要加密明文 SQLite 数据库,不直接支持 SQLCipher。所以你可以使用 sqlcipher_export() 来达到目标​​。 看这个How to encrypt a plaintext SQLite database to use SQLCipher (and avoid “file is encrypted or is not a database” errors)

// Example
command.CommandText = "ATTACH DATABASE 'encryptedName.db' AS encrypted KEY 'YourNewPassword';SELECT sqlcipher_export('encrypted');DETACH DATABASE encryptedName;";
  1. 如果您在 Asp.net Core 和 Entity Framework Core 中遇到一些性能问题, 你需要检查这些
  1. 如果你有其他性能问题,你可以检查这个SQLCipher Performance Optimization

【讨论】:

    【解决方案2】:

    SQLite 不再有“SetPassword”或“ChangePassword”。

    要初始设置密码,请使用以下内容:

    var connectionString = new SqliteConnectionStringBuilder(baseConnectionString)
    {
        Mode = SqliteOpenMode.ReadWriteCreate,
        Password = password
    }.ToString();
    

    要更改密码,您可以执行以下操作:

    var command = connection.CreateCommand();
    command.CommandText = "SELECT quote($newPassword);";
    command.Parameters.AddWithValue("$newPassword", newPassword);
    var quotedNewPassword = (string)command.ExecuteScalar();
    
    command.CommandText = "PRAGMA rekey = " + quotedNewPassword;
    command.Parameters.Clear();
    command.ExecuteNonQuery();
    

    【讨论】:

    • 哦,你的意思是“System.Data.SQLite 以前有这些方法。但现在什么都没有”,对吧?
    • 正确。但他们只在 .Net Framework 项目中工作。它们从未在 .Net Standard 或 .Net Core 中可用。
    • 非常感谢。我能理解。
    • 顺便说一句,我试过你建议的代码。但是,我无法设置密码,也无法打开加密数据库。您的代码用于 SQLCipher 库?我在 Microsoft 文档中找到了相同的代码。 "docs.microsoft.com/en-us/dotnet/standard/data/sqlite/…
    • 虽然在微软文档中提到过,但是 Sqlite db 文件仍然会在没有密码的情况下打开。
    猜你喜欢
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 2014-05-14
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多