New SQLCipher encryption

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for SQLite
Post Reply
JohannNell
Posts: 8
Joined: Mon 09 Sep 2013 07:11

New SQLCipher encryption

Post by JohannNell » Fri 24 Jul 2015 05:19

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.

Pinturiccio
Devart Team
Posts: 2420
Joined: Wed 02 Nov 2011 09:44

Re: New SQLCipher encryption

Post by Pinturiccio » Fri 24 Jul 2015 12:04

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.

JohannNell
Posts: 8
Joined: Mon 09 Sep 2013 07:11

Re: New SQLCipher encryption

Post by JohannNell » Fri 24 Jul 2015 23:28

Good morning,
I have sent an email to explain the behaviour. Please let me know what I can do to fix the issue.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: New SQLCipher encryption

Post by Shalex » Thu 30 Jul 2015 14:33

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

JohannNell
Posts: 8
Joined: Mon 09 Sep 2013 07:11

Re: New SQLCipher encryption

Post by JohannNell » Thu 30 Jul 2015 22:36

Good morning,
Thank you very much for the response. The below enabled me to resolve the issue.
I really appreciate the help.
Regards,
Johann

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: New SQLCipher encryption

Post by Shalex » Fri 21 Aug 2015 07:40

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");
    }

Post Reply