SQLCipher for Xamarin.iOS and Xamarin.Android

SQLCipher for Xamarin provides full database encryption for Microsoft Xamarin.iOS and Xamarin.Android. This library 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. No special knowledge of cryptography is required, and the library includes a lightweight ORM based on SQLite.Net-PCL making code sharing easy in cross-platform solutions. SQLCipher takes care of data security behind the scenes with a small footprint and great performance that makes it well suited for protecting application databases on mobile platforms. SQLCipher for Xamarin is only available through our Commercial Edition licensing via the link below, or through the SQLCipher Enterprise program.

Note: Microsoft has recently announced that the Xamarin Component Store will be shut down, and .XAM component packages are no longer supported. SQLCipher for Xamarin is now available exclusively through Zetetic's licensing, but we are offering former Component Store users enrollment into CipherCare to access NuGet based packaging. More information can be found in the related discussion post. Please contact us for details.

Buy SQLCipher for Xamarin Now »

Project Setup

SQLCipher for Xamarin is delivered directly via NuGet packages that may be installed from a local NuGet project source. Before using the Xamarin package(s) be sure to remove any prior references in your project (including to other legacy SQLCipher .xam packages).

There are several ways to setup local NuGet repositories, including file shares, global Visual Studio settings, and even running a NuGet web repository. However, one simple, flexible, and recommended approach is to store the SQLCipher NuGet packages alongside the project itself. This approach ensures that the dependencies are stored in version control alongisde the application, that the packages are instantly available to all developers and build tools, and that the dependencies can be fulfilled even offline. Note that this approach works both with Visual Studio on Mac and Windows, though the former is used here for demonstration purposes.

Start by copying the appropriate .nupkg file into a folder under the root of the application. It is even possible to place the package(s) in the top level folder of the application sources itself alongside the Solution (.sln) file. Next, create a simple nuget.config file in the same directory as the .sln and .nupkg files, that points a package source to the directory where the .nupkg file(s) are located.

nuget.config file and package in solution folder

In the following example, the the package source points to the current directory where the nuget.config and .sln file are located:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="Local" value="." />
  </packageSources>
</configuration>
      

Finally, in Visual Studio, add the nuget.config file at the solution level, save the solution, and restart the IDE.

add nuget.config file to solution

The nuget.config should now show up as a Solution Item.

nuget.config file as a solution item

Integration

Once the NuGet configuration is complete you will be able bring up the NuGet package manager and select the Local source to install the appropriate SQLCipher package at the project level.

add nuget package to project

Once the appropriate project-specific package is installed, bring in the appropirate namespaces, e.g.

  using SQLite.Net;
  using SQLite.Net.Platform.SQLCipher.XamarinIOS;
  using SQLite.Net.Platform.SQLCipher.XamarinAndroid;

Code

The integration API is SQLite-Net compatible, and based on the SQLite.Net-PCL library.

Example

The following example provides an abbreviated code sample for using SQLCipher in a Xamarin application. Note that a fully functional referece application is included with each SQLCipher package to demonstrate library usage.

using SQLite.Net;
using SQLite.Net.Attributes;
using SQLite.Net.Platform.SQLCipher.XamarinIOS;

...

public class Model
{
  [PrimaryKey,AutoIncrement]
  public int Id { get; set; }
  public string Content { get; set; }
}

...

var documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
var libraryPath = Path.Combine (documentsPath, "..", "Library");
var databasePath = Path.Combine(libraryPath, "sqlcipher.db");
using(var conn = new SQLiteConnection(new SQLitePlatformIOS(password), databasePath)) // SQLitePlatformAndroid(password) for Android!
{
  // with the release of SQLCipher 3.4.2, a license code is necessary to interface with an encrypted database. 
  conn.ExecuteScalar(String.Format("PRAGMA cipher_license = '{0}';", licenseKey));
  
  conn.CreateTable();

  conn.InsertOrReplace( 
    new Model() {Id = 0, Content = "Hello, SQLCipher!"});

  var models = conn.Query (
  "SELECT * FROM Model WHERE Id = ?", 0);

  foreach (var model in models)
  {
      Console.WriteLine ("id:{0} content:{1}", model.Id, model.Content);
  }

}

Cross-platform Projects

Applications that are simultaneously targeting Xamarin.iOS and Xamarin.Android (or potentially Windows UAP), should place their shared data access code in a separate project that uses the SQLite-Net.Core-PCL 3.1.1 NuGet package. Then, while using SQLite.Net in the data access layer, add dependencies in each of the target-specific application projects to the appropriate SQLCipher package.

Async Support

Applications wishing to support Async operations should include the SQLite.Net.Async-PCL v3.1.1 NuGet package.

Xamarin.Forms Support

Application using Xamarin.Forms may use the Xamarin Forms DependencyService dependency resolver by creating a wrapper for this specific integration, as shown in the following example:

public interface IDataService {
  SQLiteConnection GetConnection(String key);
}
...
public class User {
  public String Id {get; set;}
  public String Name {get; set;}
}
...
[assembly: Xamarin.Forms.Dependency (typeof (IOSDataService))]
public class IOSDataService : IDataService {
  public SQLiteConnection GetConnection(String key) {
  var documentsFolder = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
  var databasePath = Path.Combine(documentsFolder, "sqlcipher.db");
  return new SQLiteConnection(new SQLitePlatformIOS(key), databasePath);
  }
}

Then, to consume the service:

var id = "123";
var dataService = DependencyService.Get();
var connection = dataService.GetConnection(this.key);
return connection.Table().Where(t => t.Id == id);

Additional Questions

If you have addition comments or questions please contact us.

Buy SQLCipher for Xamarin Now »