Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
757 views
in Technique[技术] by (71.8m points)

c# - Where is "SetPassword" or "ChangePassword" in SQLiteConnection class?

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"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...