Issue with decrypting an encrypted database in Xamarin

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’);”);

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

Hello @sri

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.

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.

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?

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,