# Issue with decrypting an encrypted database in Xamarin

**URL:** <https://discuss.zetetic.net/t/issue-with-decrypting-an-encrypted-database-in-xamarin/2642>\
**Category:** SQLCipher\
**Created:** [February 7, 2018, 2:13pm UTC](https://discuss.zetetic.net/t/issue-with-decrypting-an-encrypted-database-in-xamarin/2642 "2018-02-07T14:13:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sri](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/sri/32/753_2.png) [@sri](https://discuss.zetetic.net/u/sri)\
**Post date:** [February 7, 2018, 2:13pm UTC](https://discuss.zetetic.net/t/issue-with-decrypting-an-encrypted-database-in-xamarin/2642/1 "2018-02-07T14:13:14Z")

</div>

I am facing issue exporting an encrypted database to another with a password. I am providing a encryption key which is encoded in the form for example �ԆG�C��j��Y�s�����c}��m�;' to the below.

SQLiteconnection db = new SQLiteConnection(new SQLitePlatformIOS(encryptionKey), GetOldDbPath());

Inorder for us to export the encrypted database to another, I am doing like the below which is throwing an exception saying for ‘invalid token’ at the PRAGMA command and the Attach command.

db.Execute(string.Format(“PRAGMA key = ‘{0}’;”, encryptionKey));  
db.Execute(string.Format(“ATTACH ‘{0}’ as POD KEY ‘{1}’;”, GetNewDbPath(), encryptionKey));  
db.ExecuteScalar\<\>(“SELECT sqlcipher\_export(‘POD’);”);  
db.Execute(“DETACH DATABASE POD;”);

I am ok with providing a hexadecimal key for encrypting the new database I am copying to, but in the first place, how do I provide a key to old db and attach the new one to it for copying?

When I do not provide db.Execute(string.Format(“PRAGMA key = ‘{0}’;”, encryptionKey)); and try to continue with the next steps of attaching and exporting, I was being hit with exception ‘NonDBFile’

" at SQLite.Net.SQLiteCommand.ExecuteNonQuery () \<0x1061c7060 + 0x002fc\> in \<8f2bb39aeff94a30a8628064be9c7efe#570aa0abe5ef19219c44c0c44a231a3d\>:0  
at SQLite.Net.SQLiteConnection.Execute (System.String query, System.Object[] args) \<0x1061cedb0 + 0x00133\> in \<8f2bb39aeff94a30a8628064be9c7efe#570aa0abe5ef19219c44c0c44a231a3d\>:0  
at iOS.Library.iOSSecureDatabase.CreateSqlConnection () \<0x1063ca1c0 + 0x0049b\> in \<04d03bc1b1124052b2d1ac313be2655b#570aa0abe5ef19219c44c0c44a231a3d\>:0 "

file is encrypted or is not a database

---

<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:** [February 7, 2018, 2:32pm UTC](https://discuss.zetetic.net/t/issue-with-decrypting-an-encrypted-database-in-xamarin/2642/2 "2018-02-07T14:32:40Z")

</div>

Hello @sri

> [@sri](#):
>
> SQLiteconnection db = new SQLiteConnection(new SQLitePlatformIOS(encryptionKey), GetOldDbPath());  
> db.Execute(string.Format(“PRAGMA key = ‘{0}’;”, encryptionKey));  
> db.Execute(string.Format(“ATTACH ‘{0}’ as POD KEY ‘{1}’;”, GetNewDbPath(), encryptionKey));  
> db.ExecuteScalar\<\>(“SELECT sqlcipher\_export(‘POD’);”);  
> db.Execute(“DETACH DATABASE POD;”);

Are you making any other changes to the database that you did not post here when moving from the `GetOldDbPath()` to `GetNewDbPath()`? The reason I ask is that based on your sample above, you are connecting to a database, attaching a new database file with the same encryption key and exporting it. This is effectively duplicating the file, so you could just perform a file copy operation instead if there are no other changes.

---

<div class="post-metadata">

**Author:** ![sri](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/sri/32/753_2.png) [@sri](https://discuss.zetetic.net/u/sri)\
**Post date:** [February 7, 2018, 3:17pm UTC](https://discuss.zetetic.net/t/issue-with-decrypting-an-encrypted-database-in-xamarin/2642/3 "2018-02-07T15:17:50Z")

</div>

Hi @developernotes

Yes, I have plans on having some database changes during migration. And part of it was to just export to a plaintext database as well. Hence I was following this approach of exporting all the contents over from old db to the new one. But unable to do so with the above mentioned errors of supplying the encoded key.

---

<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:** [February 7, 2018, 5:47pm UTC](https://discuss.zetetic.net/t/issue-with-decrypting-an-encrypted-database-in-xamarin/2642/4 "2018-02-07T17:47:00Z")

</div>

Hi @sri

Once you have opened the connection to the encrypted database, you do not need to specify the `PRAGMA key` operation as providing that to the constructor of the `SQLitePlatformIOS` perform that for you. What version of the SQLCipher library was used to create the database you are attempting to interface with? If you do not attempt to perform any of the export process, are you able to query any data from the database after you create your connection?

---

<div class="post-metadata">

**Author:** ![lalit](https://avatars.discourse-cdn.com/v4/letter/l/bbe5ce/32.png) [@lalit](https://discuss.zetetic.net/u/lalit)\
**Post date:** [August 12, 2019, 12:29pm UTC](https://discuss.zetetic.net/t/issue-with-decrypting-an-encrypted-database-in-xamarin/2642/5 "2019-08-12T12:29:23Z")

</div>

Hi @sri @developernotes

I am facing same issue with sqlitecipher, while doing encryption i am getting “Invalid token” issue. I am posting my code for the same.

using (var conn = new SQLiteConnection(FilePathHelper.MASTER\_DB\_SQLITE\_FILE\_PATH, true))  
{  
conn.Execute(string.Format(“PRAGMA key = ‘{0}’;”, “Password”));  
conn.Execute(string.Format(“ATTACH ‘{0}’ as POD KEY ‘{1}’;”, FilePathHelper.MASTER\_DB\_SQLITE\_FILE\_PATHEnc, “Password”));  
conn.ExecuteScalar(“SELECT sqlcipher\_export(‘POD’);”);  
conn.Execute(“DETACH DATABASE POD;”);  
}

If you can help me on this, will be appreciated,

Thanks,
