Page 1 of 1

New SQLCipher encryption

Posted: Fri 24 Jul 2015 05:19
by JohannNell
Good afternoon,
I am evaluating SQLCipher encryption. I have an existing database with data that has never been encrypted, and now want to change it to a SQLCipher encrypted database. Can you please help with some example code?
I have followed the example at https://www.devart.com/dotconnect/SQLit ... ipher.html, but when I try to apply this to my existing database, the code fails.

Re: New SQLCipher encryption

Posted: Fri 24 Jul 2015 12:04
by Pinturiccio
Please send us the message and stack trace of the exception and all inner exceptions.

Please also make sure that your application uses the sqlite3.dll library with the SQLCipher extension, and that the library has the same capacity (32bit or 64bit) as your application.

Re: New SQLCipher encryption

Posted: Fri 24 Jul 2015 23:28
by JohannNell
Good morning,
I have sent an email to explain the behaviour. Please let me know what I can do to fix the issue.

Re: New SQLCipher encryption

Posted: Thu 30 Jul 2015 14:33
by Shalex
The conn.ChangePassword("best2") method runs
cmd = New SQLiteCommand("PRAGMA rekey='best2'", conn)
cmd.ExecuteNonQuery()

underneath. In its turn, the PRAGMA rekey command requires the database to be already ENCRYPTED, i.e. be created with SQLCipher's SQLite.Interop.dll. Looks like your databaseOld.db database was created with a standard plaintext sqlite3.dll.

To fix the problem, export existing plaintext databaseOld.db to a new encrypted database file:

Code: Select all

    Sub EncryptPlainTextDb(existing As String, newEncrypted As String)

        ' the ATTACH DATABASE command will create a file specified in the newEncrypted path
        Using conn As New SQLiteConnection("FailIfMissing = true; Data Source=" + existing)
            conn.Open()
            Dim cmd = conn.CreateCommand()
            cmd.CommandText = "ATTACH DATABASE '" + newEncrypted + "' AS encrypted KEY 'mypassword'"
            cmd.ExecuteNonQuery()
            cmd.CommandText = "SELECT sqlcipher_export('encrypted')"
            cmd.ExecuteNonQuery()
            cmd.CommandText = "DETACH DATABASE encrypted"
            cmd.ExecuteNonQuery()
        End Using
    End Sub

Re: New SQLCipher encryption

Posted: Thu 30 Jul 2015 22:36
by JohannNell
Good morning,
Thank you very much for the response. The below enabled me to resolve the issue.
I really appreciate the help.
Regards,
Johann

Re: New SQLCipher encryption

Posted: Fri 21 Aug 2015 07:40
by Shalex
The SQLiteConnection.SQLCipherExport() method is added to migrate from an existing non-encrypted database to a new encrypted one in the newest (5.3.478) build of dotConnect for SQLite: http://forums.devart.com/viewtopic.php?f=29&t=32321.

Here is an example:

Code: Select all

    using (var conn = new SQLiteConnection()) {

        conn.ConnectionString = @"Data Source=D:\databaseOld.db;Encryption = SQLCipher;";
        conn.Open();
        conn.SQLCipherExport(@"D:\databaseNew.db", "11111");
    }