Mono.Data.Sqlcipher.SqliteExceptionFile opened that is not a database file file is encrypted or is not a database

I have integrated sqlcipher in xamarin iOS app.
I continuously getting error “Mono.Data.Sqlcipher.SqliteExceptionFile opened that is not a database file file is encrypted or is not a database”

Refer My Code Snippet

if (me.connection == null) {

            me.connection = new SqliteConnection ("Data Source=" + dbLocation);

            SqliteConnection.SetConfig (SQLiteConfig.Serialized); 

            me.connection.SetPassword ("");

            me.connection.SetPassword (Context.KeychainKey); // Password stored in keychain

            me.connection.Open ();

        } else if (me.connection.State == System.Data.ConnectionState.Closed) {

            me.connection = new SqliteConnection ("Data Source=" + dbLocation);

            SqliteConnection.SetConfig (SQLiteConfig.Serialized); 

            me.connection.SetPassword ("");

            me.connection.SetPassword (Context.KeychainKey);

            me.connection.Open ();

        }

at Mono.Data.Sqlcipher.SQLite3.Prepare (Mono.Data.Sqlcipher.SqliteConnection cnn, System.String strSql, Mono.Data.Sqlcipher.SqliteStatement previous, UInt32 timeoutMS, System.String& strRemain) [0x00000] in :0
at Mono.Data.Sqlcipher.SqliteCommand.BuildNextCommand () [0x00000] in :0

Any Help Highly appreciated.

Hello @amolgaikwad

A couple of thought based on the code you submitted. You should not need to call SqliteConnection.SetConfig(SQLiteConfig.Serialized); as the internal SQLCipher library itself is compiled with SQLITE_THREADSAFE=1 which sets the threading mode to serialized. Also, you should only be calling SetPassword(…); once with an actual key value, not an empty string followed by the keychain key. If your application is opening and closing the connection, I would advise against that as the keying process that occurs every time you open a new connection is intentionally slow.

I would recommend making the changes above, next I would validate that the key you are providing is in fact correct.

Thanks @developernotes

Through Out the application I have opened SQLite connection only once & I used same connection for multiple actions. So Do i need to close connection for every transaction & need to reopen if required ?

First i will implement the changes as per your suggestions.

Regards,
Amol G

Hello @amolgaikwad

No, you should attempt to open a single connection making it available to your application, closing it only when your application closes or needs to terminate database access to minimize the number of times your application performs key derivation.