4
votes

I'm just learning how to encrypt/decrypt SQLite database.
I found a way to encrypt, as in this post SQLite with encryption/password protection

This page's best answer said that we can use SEE,wxSQLite,SQLCipher,SQLiteCrypt, etc... to encrypt.

I can understand.

And, another answer said that we can use:

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

This page also says the same: Password Protect a SQLite DB. Is it possible?

However, SQLiteConnection doesn't have SetPassword nor ChangePassword methods. I'm very confused.

Where are SetPasword or ChangePassword?
Are these in SEE, wxSQLite,SQLCipher,SQLiteCrypt?

[Development environment]
VisualStudio2010 .NET Framework 4 Client Profile System.Data.SQLite.dll (https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki)

I downloaded zip from there, and I picked up "System.Data.SQLite.dll"

2
No, It is’nt. my question is Where "SetPasword" or "ChangePassword" is. SQLiteConnection doesn’t have these method. - kanikama

2 Answers

0
votes

if you want to encrypt/decrypt SQLite database. there are some information for you

Please check the Specify the key section in the official documentation if you have other problem.

The method for encrypting and decrypting existing databases varies depending on which solution you're using. For example, you need to use the sqlcipher_export() function on SQLCipher. Check your solution's documentation for details.

You can use the SQLCipher API to finish your work:

  1. If you want to build a encrypting database, just set your connection string, like this:Data Source = encryptedName.db; Password=YourPassword

  2. if you want to change password in a encrypting database

    // 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. if you want to encrypt a plaintext SQLite database , SQLCipher is not support directly. so you can use sqlcipher_export() to get the goal. look this 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. if you have some Performance Issue in Asp.net Core and Entity Framework Core , you need to check to these
  1. if you have other Performance Issue , you can check this SQLCipher Performance Optimization
-3
votes

SQLite no longer has "SetPassword" or "ChangePassword".

To initially set the password you use the following:

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

To change the password you would do the following:

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