sqlcipher / android-database-sqlcipher

Android SQLite API based on SQLCipher
https://www.zetetic.net/sqlcipher/sqlcipher-for-android/
Other
2.73k stars 564 forks source link

Error #496

Closed JavaDeveloper08 closed 4 years ago

JavaDeveloper08 commented 4 years ago

hi, I am using sqlcipher encryption in my android application.

implementation 'net.zetetic:android-database-sqlcipher:4.3.0'

Normally, everything is ok, but in some devices we are getting error in release mode

and app crashes. In debug mode there is no problem.

But other devices run nicely for both debug mode and release mode.

Would you help me ? it is important for me. Thanks for your interest.

Fatal Exception: net.sqlcipher.database.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master; at net.sqlcipher.database.SQLiteCompiledSql.native_compile(SQLiteCompiledSql.java) at net.sqlcipher.database.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91) at net.sqlcipher.database.SQLiteCompiledSql.(SQLiteCompiledSql.java:64) at net.sqlcipher.database.SQLiteProgram.(SQLiteProgram.java:91) at net.sqlcipher.database.SQLiteQuery.(SQLiteQuery.java:48) at net.sqlcipher.database.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:60) at net.sqlcipher.database.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:2016) at net.sqlcipher.database.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1902) at net.sqlcipher.database.SQLiteDatabase.keyDatabase(SQLiteDatabase.java:2673) at net.sqlcipher.database.SQLiteDatabase.openDatabaseInternal(SQLiteDatabase.java:2603) at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1247) at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1322) at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:166) at net.sqlcipher.database.SupportHelper.getWritableDatabase(SupportHelper.java:83) at androidx.room.RoomDatabase.query(RoomDatabase.java:238) at com.ebis_mobile.database.dao.SettingsDao_Impl$4.compute(SettingsDao_Impl.java:133) at com.ebis_mobile.database.dao.SettingsDao_Impl$4.compute(SettingsDao_Impl.java:119) at androidx.lifecycle.ComputableLiveData$2.run(ComputableLiveData.java:101) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:919)

developernotes commented 4 years ago

Hello @JavaDeveloper08

This exception is commonly due to providing an invalid password to a SQLCipher database. Please try to create a reproduction case within the SQLCipher for Android test suite if you feel that is not the case and we would be happy to review further. Thanks!

JavaDeveloper08 commented 4 years ago

Thanks for your reply, I'm using same device in debug mode, there is no error, but in release mode, I am getting this error, so if I try your test suite, I think I will not get error. Isn't it ?

developernotes commented 4 years ago

Hi @JavaDeveloper08

I think I will not get error. Isn't it ?

You can build the test suite in release mode, it runs on a device.

JavaDeveloper08 commented 4 years ago

I built test suite a few minutes ago. In dashboard, we have 3 options. when I select "1-Run behavior Test Suite. ", all testings options seems OK.

My running code in Database Class;

final byte[] passphrase = SQLiteDatabase.getBytes(decryptedText.toCharArray());

        final SupportFactory factory = new SupportFactory(passphrase);
        SQLCipherUtils.State state = SQLCipherUtils.getDatabaseState(context, DATABASE_NAME);

        if (state.equals(SQLCipherUtils.State.UNENCRYPTED)) {
            try {
                SQLCipherUtils.encrypt(context, DATABASE_NAME, passphrase);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (instance == null) {
            instance = Room.databaseBuilder(context.getApplicationContext(), ABDatabase.class,
                    DATABASE_NAME).allowMainThreadQueries()
                    .addMigrations(MIGRATION_1_2)
                    .openHelperFactory(factory)
                    .build();
        }

How can I implement my scenario in Test Class.

JavaDeveloper08 commented 4 years ago

I implemented this scenario, in Android Studio I run the project and nicely created tables and execute queries. I created apk file, but it cannot be installed.

@Override public boolean execute(SQLiteDatabase database) { try { database.execSQL("create table t1(a,b);"); database.execSQL("insert into t1(a, b) values(?, ?);", new Object[]{UUID.randomUUID().toString(), "foo"}); database.execSQL("insert into t1(a, b) values(?, ?);", new Object[]{UUID.randomUUID().toString(), "bar"}); int count = 0; Cursor cursor = database.rawQuery("select * from t1 where a = ? or ? = -1;", new Object[]{-1, -1}); if(cursor != null){ while (cursor.moveToNext()){ count++; } cursor.close(); } return count == 2; }catch (Exception e){ return false; } }

JavaDeveloper08 commented 4 years ago

Do you have any suggestion about solving my problem ? Why it is run in debug mode nicely, but getting error in release mode in some devices. What is the difference in library?

I am looking forward your reply.

developernotes commented 4 years ago

Hi @JavaDeveloper08

I implemented this scenario, in Android Studio I run the project and nicely created tables and execute queries. I created apk file, but it cannot be installed.

What happens exactly when you attempt to install it? What exactly did you do that did not work? Without additional detailed information on what you are observing, we are unable to provide any concrete guidance.

JavaDeveloper08 commented 4 years ago

Hi again, First, thank you for your interest, I created test suite apk again, and run the app, Everything seems OK. Also DemoTest class is seems success. I got no error.

This morning I noticed that, I deleted local storage for my app int the device that I was getting error in release mode, app was successfully opened. I checked my code again ; I am not writing any thing to local storage at first on the device.

Also you know, I am not getting error with this device in debug mode.

May be you can help me after this information.

Thanks.

JavaDeveloper08 commented 4 years ago

I checked my local storage file again.

Before I deleted local files in release mode, only a database_name.db file is being created.

But after deleting the local files, database_name-shm and database_name-wal files are being created and app is opening successfully.

I checked in debug mode, this files are creating successfully at first.

So the question is why some devices in release mode aren't creating these 2 files adding to database_name.db file. What is the problem ?

I think we are near the solving of problem.

Thank you.

JavaDeveloper08 commented 4 years ago

Hi again, unfortunately, I couldn't solve my problem. only a few user faces with this problem in more than 1000 users in release mode.

Some phone brands that caused this error are Samsung Galaxy S7 edge (Version 8.0.0), Galaxy Grand Neo Plus, Galaxy S8, OnePlus GM1910, Huawei FIG LX1.

I say to users who getting this error, delete local data and open application again. and problem is being solved by using this way.

I have no error in debug mode and your test suite application.

Best regards.

akvashi commented 4 years ago

@developernotes I am also having this issue as well though intermittently and there doesn't seem to be a reason why. I have noticed there are couple of similar issues listed in closed issues. Just wonder if it would be helpful to leave an issue open so there it way to compile a list of scenerios or usecases where this is happening.

I know there are multiple reasons for this that definitely includes developer error. But in the case that this is hard to reproduce and is an issue with the library I would hope that a single issue might be helpful to track.

Not to mention solutions that ended up fixing the issue

akvashi commented 4 years ago

@JavaDeveloper08 its interesting that you are only seeing this on certain devices. I have QA engineers reporting the issue intermittently though I have not once reproduced.

I am on a Pixel and we have a wide range of Samsungs that seem to have the issue.

developernotes commented 4 years ago

Hi @JavaDeveloper08, @akvashi

The specific SQLiteException being reported is commonly caused by providing an incorrect password to access the database, as mentioned here. Without providing a reproduction case, or additional details, it is difficult to provide any guidance to resolve such an issue.

So the question is why some devices in release mode aren't creating these 2 files adding to database_name.db file. What is the problem ?

The presence of the -shm and -wal files suggests you are using the WAL journal mode. SQLCipher will attempt to recover the WAL file content (i.e., -wal file) when the connection is reestablished, though typically these files are removed upon successful close of the corresponding database. WAL mode is a persistent journaling mode, however, so you should review your application code to see where it is being enabled, and if any conditions prevent that from occurring.

Without reviewing the application code in full, reviewing a test case, or code sample that is producing invalid results, there isn't enough information to provide constructive guidance for addressing the issue.

stale[bot] commented 4 years ago

Hello, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as "bug", "enhancement", or "security" and I will leave it open. Thank you for your contributions.

stale[bot] commented 4 years ago

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to reopen with up-to-date information.

akvashi commented 4 years ago

@JavaDeveloper08 are you still seeing this issue? any solutions?

JavaDeveloper08 commented 4 years ago

@akvashi, unfortunateIy, still yes, I couldn't find any solution for this problem :(

AlexSheva-mason commented 2 years ago

We faced the same issue - when database works perfectly in debug mode bud crashed on some devices in release mode (seemed to be random). We have now fixed it. In our case the problem was with the Android auto-backup feature (android:allowBackup="true" in the manifest). When re-installing the app on some devices system would attempt to restore previous database which in some cases had different password / encryption key etc, hence the crash because Sqlcipher couldn't open the database. Check if this is the case in your situation guys.

sjlombardo commented 2 years ago

@AlexSheva-mason thank you for mentioning this solution!