dotConnect for SQLite Documentation
In This Topic
    Using SQLCipher
    In This Topic

    dotConnect for SQLite fully support connecting to SQLCipher encrypted databases, however it does not provide the SQLCipher extension itself. In order to use SQLCipher encryption you need to buy SQLCipher separately. SQLCipher offers a library, named SQLite.Interop.dll. For use with dotConnect for SQLite, it must be renamed to sqlite3.dll.

    To connect to SQLCipher encrypted database, you should set the Encryption connection string parameter to SQLCipher and specify the Password and Encryption License Key connection string parameters.

    Unlike other SQLite encryption extensions, SQLCipher does not allow encrypting an existing unencrypted database directly, and you cannot simply use the ChangePassword for an unencrypted database. Instead you need to create an encrypted copy of an unencrypted database with the SQLCipherExport method of SQLiteConnection. Note that after the execution of this method, the connection is still connected to the unencrypted original database. You will need to connect to the new encrypted database manually in order to work with it.

    In order to create an unencrypted copy of a database, call the SQLCipherExport method with an empty string as the password parameter.

    Here is an example of using dotConnect for SQLite with SQLCipher. You need to paste your own SQLCipher license key instead of "OmNjXDZDiz....ljNw" in this example.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Devart.Data.SQLite;
    
    namespace SQLCipherSample
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Creating a new database without encryption
                SQLiteConnection conn = new SQLiteConnection("Data Source=dbUnencrypted.db;FailIfMissing = false;Pooling=false");
                conn.Open();
    
                // Add a table to database, because it must not be empty if we want to encrypt it with SQLCipher
                SQLiteCommand cmd = new SQLiteCommand(@"CREATE TABLE DEPT (
                                                            DEPTNO INTEGER PRIMARY KEY,
                                                            DNAME VARCHAR(14),
                                                            LOC VARCHAR(13)
                                                        );", conn);
                cmd.ExecuteNonQuery();
                cmd.CommandText = "INSERT INTO DEPT VALUES (10,'ACCOUNTING','NEW YORK');";
                cmd.ExecuteNonQuery();
                conn.Close();
    
                // Use SQLCipherExport method to create a new database dbEncrypted.db with SQLCipher encryption 
                // and copy data from the unencrypted database dbUnencrypted.db
                conn = new SQLiteConnection("Data Source=dbUnencrypted.db;Encryption=SQLCipher; FailIfMissing = false;Pooling=false; Encryption License Key=OmNjXDZDiz....ljNw");
                conn.Open();
                conn.SQLCipherExport("dbEncrypted.db", "password");
                conn.Close();
    
                // Open the encrypted with SQLCipher dbEncrypted.db database
                conn = new SQLiteConnection("Data Source=dbEncrypted.db;Encryption=SQLCipher; FailIfMissing = false;Password=password; Pooling=false; Encryption License Key=OmNjXDZDiz....ljNw");
                conn.Open();
                cmd.CommandText = "select dname from dept where deptno=10";
                cmd.Connection = conn;
                Console.WriteLine(cmd.ExecuteScalar());
                Console.WriteLine("The encrypted database is successfully opened");
                conn.ChangePassword("newPass"); //Use ChangePassword method to change password for the dbEncrypted.db database
                conn.Close();
    
                // Open the encrypted with SQLCipher dbEncrypted.db database with the new password
                conn = new SQLiteConnection("Data Source=dbEncrypted.db;Encryption=SQLCipher; FailIfMissing = false;Password=newPass; Pooling=false; Encryption License Key=OmNjXDZDiz....ljNw");
                conn.Open();
                cmd.CommandText = "select dname from dept where deptno=10";
                cmd.Connection = conn;
                Console.WriteLine(cmd.ExecuteScalar());
                Console.WriteLine("The database with the new password is successfully opened");
                conn.Close();
    
                // decrypt the database
                // Use SQLCipherExport method to create a new database dbUnencrypted1.db without SQLCipher encryption and copy 
                // data from the encrypted database dbEncrypted.db
                conn = new SQLiteConnection("Data Source=dbEncrypted.db;Encryption=SQLCipher; FailIfMissing = false;Password=newPass; Pooling=false; Encryption License Key=OmNjXDZDiz....ljNw");
                conn.Open();
                conn.SQLCipherExport("dbUnencrypted1.db", "");
                conn.Close();
    
                // Open the decrypted database
                conn = new SQLiteConnection("Data Source=dbUnencrypted1.db;FailIfMissing = false;Pooling=false");
                conn.Open();
                cmd.CommandText = "select dname from dept where deptno=10";
                cmd.Connection = conn;
                Console.WriteLine(cmd.ExecuteScalar());
                Console.WriteLine("The decrypted database is successfully opened");
                conn.Close();
            }
        }
    }
    
    
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports Devart.Data.SQLite
    
    Namespace SQLCipherSample
        Class Program
            Private Shared Sub Main(args As String())
                'Creating new database without encryption
                Dim conn As New SQLiteConnection("Data Source=dbUnencrypted.db;FailIfMissing = false;Pooling=false")
                conn.Open()
    
                'Add a table to database, because it must not be empty if we want to encrypt it with SQLCipher
                Dim cmd As New SQLiteCommand("CREATE TABLE DEPT (DEPTNO INTEGER PRIMARY KEY, DNAME VARCHAR(14), Loc VARCHAR(13));", conn)
                cmd.ExecuteNonQuery()
    
                cmd.CommandText = "INSERT INTO DEPT VALUES (10,'ACCOUNTING','NEW YORK');" 
                cmd.ExecuteNonQuery()
                conn.Close()
    
                'Use SQLCipherExport method to create a new database dbEncrypted.db with SQLCipher encryption 
    			'and copy data from the unencrypted database dbUnencrypted.db
                conn = New SQLiteConnection("Data Source=dbUnencrypted.db;Encryption=SQLCipher; FailIfMissing = false;Pooling=false; Encryption License Key=OmNjXDZDiz....ljNw")
                conn.Open()
                conn.SQLCipherExport("dbEncrypted.db", "password")
                conn.Close()
    
                'Open the encrypted with SQLCipher dbEncrypted.db database
                conn = New SQLiteConnection("Data Source=dbEncrypted.db;Encryption=SQLCipher;FailIfMissing=false;Password=password; Pooling=false; Encryption License Key=OmNjXDZDiz....ljNw")
                conn.Open()
                cmd.CommandText = "select dname from dept where deptno=10" 
                cmd.Connection = conn
                Console.WriteLine(cmd.ExecuteScalar())
                Console.WriteLine("The encrypted database is successfully opened")
                conn.ChangePassword("newPass") 'Use ChangePassword method to change password for the dbEncrypted.db database
                conn.Close()
    
                'Open the encrypted with SQLCipher dbEncrypted.db database with the new password
                conn = New SQLiteConnection("Data Source=dbEncrypted.db;Encryption=SQLCipher; FailIfMissing = false;Password=newPass; Pooling=false; Encryption License Key=OmNjXDZDiz....ljNw")
                conn.Open()
                cmd.CommandText = "select dname from dept where deptno=10" 
                cmd.Connection = conn
                Console.WriteLine(cmd.ExecuteScalar())
                Console.WriteLine("The database with the new password is successfully opened")
                conn.Close()
    
                'decrypt the database
                'Use SQLCipherExport method to create a new database dbUnencrypted1.db without SQLCipher encryption 
    			'and copy data from the encrypted database dbEncrypted.db
                conn = New SQLiteConnection("Data Source=dbEncrypted.db;Encryption=SQLCipher; FailIfMissing = false;Password=newPass; Pooling=false; Encryption License Key=OmNjXDZDiz....ljNw")
                conn.Open()
                conn.SQLCipherExport("dbUnencrypted1.db", "")
                conn.Close()
    
                'Open the dencrypted database
                conn = New SQLiteConnection("Data Source=dbUnencrypted1.db;FailIfMissing = false;Pooling=false")
                conn.Open()
                cmd.CommandText = "select dname from dept where deptno=10" 
                cmd.Connection = conn
                Console.WriteLine(cmd.ExecuteScalar())
                Console.WriteLine("The decrypted database is successfully opened")
                conn.Close()
            End Sub
        End Class
    End Namespace