How to Encrypt an sqlitedb file in Xamarin World?


#1

Hi,

I cannot find many document for Mono.Data.Sqlcipher.dll. I basically want to encrypt a unencrypted SQLite file (downloaded from server). in android /ios devices using Xamarin.

I have tried:

using Mono.Data.Sqlcipher.dll;

SqliteConnection connection = new SqliteConnection (string.Format(“Data Source={0}”, destination));
connection.Open();
connection.ChangePassword (password);
connection.Close ();

It went successfully but the db file is not encrypted. Any idea.

Thanks


#2

Hello @larry

To encrypt a plain text SQLite database using SQLCipher, you will want to integrate the usage of the sqlcipher_export(…) into your application, specifically you will want to review example #1.


#3

Thanks a lot. I got passed that problem.

I don’t know if this is the right place, but the current way of single Mono.Data.Sqlcipher dll package isn’t very useful when using SQLite.net syntax in cross platform Xamarin development. As Model will normally be PCL in this case and shared by all other projects. I will need at very least, have the Table/ColumnAttribute Class in a separate PCL library .

Another minor issue with SQLite.net syntax is all the CancellationToken parameters are removed.


#4

Hello @larry

I’m glad to hear you have made progress with the migration. At this time we do not have a PCL version of SQLCipher for Xamarin, however it is something we are considering in the future. Thank you for your feedback.


#5

Hi,

I encountered a very weird issue and wish you guys can help me out.

So I used the following code to encrypt a SQLite db:
public void EncrptSqliteDb(string source, string destination, string password)
{

			SqliteConnection connection = new SqliteConnection (string.Format("Data Source={0}", source));
			connection.Open();
			using (var cmd = connection.CreateCommand())
			{
				cmd.CommandText = string.Format("ATTACH DATABASE '{0}' AS encrypted KEY '{1}'", destination, password);
				
				cmd.CommandText = "SELECT sqlcipher_export('encrypted')";
				
				cmd.CommandText = "DETACH DATABASE encrypted";
				

			}
			connection.Close ();
        
		
	}

it works fine on debug version of my android application, It generates a file of size 871K and it can be late opened /queried with SqliteCommand.

if I build a release version and run it, it will generate a file of size 1.2M, and will freeze when do SqliteCommand.executeReader.(Note it gets passed the SQLiteConnection open with key stuff, which it is not totally correptted)

Any idea what might be the issue here?

Thanks


#6

Disregard the my above reply. It is my fault as the release version pulled a different version.

Sorry.