0

I'm using a .NET Core project in order to manage SQLite db encrypted and I would be able to create a db copy programmatically without encryption, but I'm not finding any sample code and I don't even know if this is possible.

I'm starting from scratch with the following project: https://github.com/paragpkulkarni/SQLiteEncryptionUsingEFCore

7
  • Does it need to be generic to handle any DB with any scheme, or just for a particular scheme? Commented Oct 27, 2020 at 10:53
  • only for my db (particular scheme). Commented Oct 27, 2020 at 10:58
  • Easier to just load up all the tables to memory, then write them to the new DB unencrypted you have. Commented Oct 27, 2020 at 11:01
  • 1
    Any sample? I'm looking for sqlcipher_export() function which should accomplish my purpose but I'm unable to find some samples with ado.net and so on.. Commented Oct 27, 2020 at 11:06
  • That will be off-topic right? Commented Oct 27, 2020 at 11:26

1 Answer 1

2

This is the final solution:

void DecryptDB(string sourceFilename, string destinationFilename, string password)
        {
            var connectionString = new SqliteConnectionStringBuilder
            {
                DataSource = sourceFilename,
                Mode = SqliteOpenMode.ReadWriteCreate,
                Password = password 
            };

            using var cn = new SqliteConnection(connectionString.ToString());
            cn.Open();

            using var c = cn.CreateCommand();
            c.CommandText = @$"ATTACH DATABASE '{destinationFilename}' AS plaintext KEY '';";
            var r = c.ExecuteNonQuery();

            c.CommandText = @"SELECT sqlcipher_export('plaintext')";
            r = c.ExecuteNonQuery();
        }
Sign up to request clarification or add additional context in comments.

4 Comments

after using plaintext.db as destinationFileName i am getting below mentioned error :----- {SQLite.SQLiteException: unable to open database: plaintext.db at SQLite.SQLiteCommand.ExecuteNonQuery () [0x000cd] in <01c96d411fe34bd5940c99a56515582b>:0 at TundraNG.Core.DataStore.Handlers.ConnectionHandler.copyDB (System.String destinationFile, System.String FName) [0x00095] in } Can you please help me on it..
Make sure your application is able to create a new file on directory specified on destinationFilename parameter.
yes... permission is enabled to that folder. Also, I tried to manually create a file in the same location and then execute this code, but i am getting the same error. :(
Try to post your entire code snippet or even better try to open a new post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.