# How to Encrypt an sqlitedb file in Xamarin World?

**URL:** <https://discuss.zetetic.net/t/how-to-encrypt-an-sqlitedb-file-in-xamarin-world/474>\
**Category:** SQLCipher\
**Created:** [March 17, 2015, 3:50pm UTC](https://discuss.zetetic.net/t/how-to-encrypt-an-sqlitedb-file-in-xamarin-world/474 "2015-03-17T15:50:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![larry](https://avatars.discourse-cdn.com/v4/letter/l/47e85d/32.png) [@larry](https://discuss.zetetic.net/u/larry)\
**Post date:** [March 17, 2015, 3:50pm UTC](https://discuss.zetetic.net/t/how-to-encrypt-an-sqlitedb-file-in-xamarin-world/474/1 "2015-03-17T15:50:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [March 17, 2015, 4:16pm UTC](https://discuss.zetetic.net/t/how-to-encrypt-an-sqlitedb-file-in-xamarin-world/474/2 "2015-03-17T16:16:17Z")

</div>

Hello @larry

To encrypt a plain text SQLite database using SQLCipher, you will want to integrate the usage of the [`sqlcipher_export(…)`](https://www.zetetic.net/sqlcipher/sqlcipher-api/#sqlcipher_export) into your application, specifically you will want to review example #1.

---

<div class="post-metadata">

**Author:** ![larry](https://avatars.discourse-cdn.com/v4/letter/l/47e85d/32.png) [@larry](https://discuss.zetetic.net/u/larry)\
**Post date:** [March 17, 2015, 6:31pm UTC](https://discuss.zetetic.net/t/how-to-encrypt-an-sqlitedb-file-in-xamarin-world/474/3 "2015-03-17T18:31:07Z")

</div>

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](http://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](http://SQLite.net) syntax is all the CancellationToken parameters are removed.

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [March 17, 2015, 7:44pm UTC](https://discuss.zetetic.net/t/how-to-encrypt-an-sqlitedb-file-in-xamarin-world/474/4 "2015-03-17T19:44:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![larry](https://avatars.discourse-cdn.com/v4/letter/l/47e85d/32.png) [@larry](https://discuss.zetetic.net/u/larry)\
**Post date:** [May 6, 2015, 3:50pm UTC](https://discuss.zetetic.net/t/how-to-encrypt-an-sqlitedb-file-in-xamarin-world/474/5 "2015-05-06T15:50:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![larry](https://avatars.discourse-cdn.com/v4/letter/l/47e85d/32.png) [@larry](https://discuss.zetetic.net/u/larry)\
**Post date:** [May 6, 2015, 4:10pm UTC](https://discuss.zetetic.net/t/how-to-encrypt-an-sqlitedb-file-in-xamarin-world/474/6 "2015-05-06T16:10:42Z")

</div>

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

Sorry.
