4

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"

Vega
  • 27,856
  • 27
  • 95
  • 103
kanikama
  • 43
  • 1
  • 3
  • Does this answer your question? [Password Protect a SQLite DB. Is it possible?](https://stackoverflow.com/questions/1381264/password-protect-a-sqlite-db-is-it-possible) – Edney Holder Jun 10 '20 at 03:15
  • No, It is’nt. my question is Where "SetPasword" or "ChangePassword" is. SQLiteConnection doesn’t have these method. – kanikama Jun 10 '20 at 03:32

3 Answers3

0

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
Ci Ty Chen
  • 41
  • 1
  • 5
0

It seems that starting with System.Data.SQLite version 1.0.113.0, support for encryption via SetPassword() and ChangePassword() was removed. See my answer here for details: https://stackoverflow.com/a/72073407/8586332

scooter
  • 185
  • 1
  • 8
-2

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();
Edney Holder
  • 1,140
  • 8
  • 22
  • 1
    Oh, you mean “System.Data.SQLite had these method before. but now nothing”, right? – kanikama Jun 10 '20 at 06:48
  • Correct. But they only worked in .Net Framework projects. They have never been available in .Net Standard or .Net Core. – Edney Holder Jun 10 '20 at 11:06
  • Thank you very much. I can understand. – kanikama Jun 10 '20 at 11:23
  • 1
    By the way, I tried your suggested code. But, I couldn't set password, And couldn't open encrypted database. Your code is for SQLCipher library? I found same code in Microsoft documentation. "https://learn.microsoft.com/en-us/dotnet/standard/data/sqlite/encryption?tabs=visual-studio – kanikama Jun 10 '20 at 13:13
  • Although it is mentioned in Microsoft docs, however, Sqlite db file would still open without password. – Hassan Naqvi Dec 08 '20 at 08:01