SQLCipher for ADO.NET - Full Database Encryption for .NET on Windows

SQLCipher for ADO.NET provides full database SQLite encryption for .NET based Windows applications. This ADO.NET provider, based on System.Data.SQLite, makes it quick and easy to integrate SQLCipher's AES-256 full database encryption into any application, and is fully interoperable with other SQLCipher platform libraries.

The SQLCipher ADO.NET provider is a "commercial edition" library that can be purchased directly from the Zetetic Store. Licensed software is delivered immediately upon payment.

Buy SQLCipher for ADO.NET Now »

The remainder of this document provides a practical example of how to integrate SQLCipher for ADO.NET. This tutorial assumes that you are using Visual Studio 2010 and on your local development machine, adding SQLCipher to an existing project.

Unzip Package

Start by unzipping the SQLCipher binary package, sqlcipher-ado.

Then, move the expanded binary package into your project folder structure.

Add References

Next, open your project in Visual Studio. Right click on Referencesin the Solution Explorer, and select "Add References".

Switch to the "Browse" tab, and then locate the folder containing the binary packages and add System.Data.SQlite.dll and System.Data.SQLite.Linq.dll to the project from subfolder appropriate for the target architecture:

  1. If application will target both 32 and 64 bit systems, include the binaries from the "x86" folder (if you're not sure, choose this)
  2. If application will target only 64 bit systems, include the binaries from the "x64" folder

Use SQLCipher

Next, make sure that your configuration is set for the appropriate architecture chosen above, for example, x86. Add "using System.Data.SQLite;" to your program source files, and then being using the library.

Code Example

The following example code demonstrates how to create and query an encrypted SQLCipher database.


using System.Data.SQLite;

namespace Example
{
    class Program
    {
        private static string _connectionString = 
            "Data Source=sqlcipher.db;Pooling=false;Synchronous=Full;";
        private static string _password = "test123";

        static void Main(string[] args)
        {

            // create the database
            using (var conn = new SQLiteConnection(_connectionString))
            {
                conn.SetPassword(_password);
                conn.Open();

                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "CREATE TABLE t1(a,b);";
                    cmd.ExecuteNonQuery();

                    cmd.CommandText = "INSERT INTO t1(a,b) VALUES ('test0', 'test1');";
                    cmd.ExecuteNonQuery();
                }
                conn.Close();
            }

            using (var conn = new SQLiteConnection(_connectionString))
            {
                conn.SetPassword(_password);
                conn.Open();

                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT * FROM t1;";
                    var reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        System.Console.WriteLine(string.Format("{0} | {1}", reader.GetString(0), reader.GetString(1)));
                    }
                }
            }
        }
    }
}

Using ADO.NET System.Data.Common with SQLCipher

It's also possible to use the System.Data.Common ADO.NET interfaces with SQLCipher. Using this API you'd load the SQLCipher provider via the DbProviderFactory interfaces:


using System.Data.Common;
using System.Data.SQLite;

...

DbProviderFactory provider = DbProviderFactories.GetFactory("System.Data.SQLite");
using (DbConnection conn = provider.CreateConnection())
{
    conn.ConnectionString = "Data Source=sqlcipher.db;Pooling=false;Synchronous=Full;";
    ((SQLiteConnection)conn).SetPassword("secret");
    conn.Open();

    using (IDbCommand command = conn.CreateCommand())
    {
       command.CommandText = "CREATE TABLE t1(a,b);";
       command.ExecuteNonQuery();
    }
    ...
}

When using System.Data.Common you must add the following the App.config so that the provider can be loaded at runtime (note that the Version for the provider changes from release to release, and is found in the package README.


<configuration>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite"/>
      <add name="SQLite Data Provider" invariant="System.Data.SQLite" 
        description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=91.0.80.0, Culture=neutral, PublicKeyToken=2afdc9349188e576" />
    </DbProviderFactories>
  </system.data>
</configuration>

SQLCipher with .NET 4

SQLCipher's ADO.NET provides full support for .NET 4. When using the ADO.NET provider DLL with .NET 4 and VS2012, it is necessary to make a small change to the app.config to set useLegacyV2RuntimeActivationPolicy="true" as shown here:

  
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
  </startup>
</configuration>

This can also be set at runtime, using this procedure.

Get SQLCipher for ADO.NET

SQLCipher for ADO.NET can be purchased directly from the Zetetic Store and licensed software is delivered immediately upon payment.

Buy SQLCipher for ADO.NET Now »