Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updating to sqlcipher-android from android-database-sqlcipher file is not a database (code 26) #34

Open
xuhongchang opened this issue Mar 22, 2024 · 14 comments

Comments

@xuhongchang
Copy link

from android-database-sqlcipher 4.5.4 to sqlcipher-android4.5.6
receive some error
If it is a new installation, it is not a problem, only the overlay installation is a problem
here is log:
Failed to open database
android.database.sqlite.SQLiteException: file is not a database (code 26): , while compiling: SELECT COUNT(*) FROM sqlite_schema;
at net.zetetic.database.sqlcipher.SQLiteConnection.nativePrepareStatement(Native Method)
at net.zetetic.database.sqlcipher.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:973)
at net.zetetic.database.sqlcipher.SQLiteConnection.executeForLong(SQLiteConnection.java:628)
at net.zetetic.database.sqlcipher.SQLiteConnection.open(SQLiteConnection.java:240)
at net.zetetic.database.sqlcipher.SQLiteConnection.open(SQLiteConnection.java:202)
at net.zetetic.database.sqlcipher.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:475)
at net.zetetic.database.sqlcipher.SQLiteConnectionPool.open(SQLiteConnectionPool.java:189)
at net.zetetic.database.sqlcipher.SQLiteConnectionPool.open(SQLiteConnectionPool.java:181)
at net.zetetic.database.sqlcipher.SQLiteDatabase.openInner(SQLiteDatabase.java:1028)
at net.zetetic.database.sqlcipher.SQLiteDatabase.open(SQLiteDatabase.java:1013)
at net.zetetic.database.sqlcipher.SQLiteDatabase.openDatabase(SQLiteDatabase.java:840)
at net.zetetic.database.sqlcipher.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:359)
at net.zetetic.database.sqlcipher.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:278)

@xuhongchang
Copy link
Author

sqlcipher-android4.5.6 version
use code :
public SQLiteOpenHelper(Context context, String name, byte[] password, CursorFactory factory,
int version, int minimumSupportedVersion,
DatabaseErrorHandler errorHandler, SQLiteDatabaseHook databaseHook,
boolean enableWriteAheadLogging)

android-database-sqlcipher 4.5.4 version
use code:
public SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, SQLiteDatabaseHook hook) {
this(context, name, factory, version, hook, new DefaultDatabaseErrorHandler());
}

public synchronized SQLiteDatabase getWritableDatabase(String password) {
    return this.getWritableDatabase(password == null ? null : password.toCharArray());
}

@developernotes
Copy link
Member

Hi @xuhongchang,

What happens when you retrieve your existing database file off the Android device and attempt to access it within DB Browser for SQLite 1?

Footnotes

  1. https://nightlies.sqlitebrowser.org/latest/

@xuhongchang
Copy link
Author

how to upgrade android-database-sqlcipher 4.5.4 to sqlcipher-android4.5.6 ? Android system.
I referenced the migration documentation:https://www.zetetic.net/sqlcipher/sqlcipher-for-android-migration/

If it is a new installation, it is not a problem, only the overlay installation is a problem

here is log:
Failed to open database
android.database.sqlite.SQLiteException: file is not a database (code 26): , while compiling: SELECT COUNT(*) FROM sqlite_schema;
at net.zetetic.database.sqlcipher.SQLiteConnection.nativePrepareStatement(Native Method)
at net.zetetic.database.sqlcipher.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:973)
at net.zetetic.database.sqlcipher.SQLiteConnection.executeForLong(SQLiteConnection.java:628)
at net.zetetic.database.sqlcipher.SQLiteConnection.open(SQLiteConnection.java:240)
at net.zetetic.database.sqlcipher.SQLiteConnection.open(SQLiteConnection.java:202)
at net.zetetic.database.sqlcipher.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:475)
at net.zetetic.database.sqlcipher.SQLiteConnectionPool.open(SQLiteConnectionPool.java:189)
at net.zetetic.database.sqlcipher.SQLiteConnectionPool.open(SQLiteConnectionPool.java:181)
at net.zetetic.database.sqlcipher.SQLiteDatabase.openInner(SQLiteDatabase.java:1028)
at net.zetetic.database.sqlcipher.SQLiteDatabase.open(SQLiteDatabase.java:1013)
at net.zetetic.database.sqlcipher.SQLiteDatabase.openDatabase(SQLiteDatabase.java:840)
at net.zetetic.database.sqlcipher.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:359)
at net.zetetic.database.sqlcipher.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:278)

@developernotes
Copy link
Member

Hi @xuhongchang,

What happens when you pull the existing database off the Android device and access it with the password using DB Browser for SQLite 1?

Footnotes

  1. https://nightlies.sqlitebrowser.org/latest/

@andzhangs
Copy link

andzhangs commented Jul 22, 2024

我结合Room数据库使用时,也遇到了同样的问题,你最后是怎么解决的了?
android.database.sqlite.SQLiteException: file is not a database (code 26): , while compiling: SELECT COUNT(*) FROM sqlite_schema; at net.zetetic.database.sqlcipher.SQLiteConnection.nativePrepareStatement(Native Method) at net.zetetic.database.sqlcipher.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:973) at net.zetetic.database.sqlcipher.SQLiteConnection.executeForLong(SQLiteConnection.java:628) at net.zetetic.database.sqlcipher.SQLiteConnection.open(SQLiteConnection.java:240) at net.zetetic.database.sqlcipher.SQLiteConnection.open(SQLiteConnection.java:202) at net.zetetic.database.sqlcipher.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:475) at net.zetetic.database.sqlcipher.SQLiteConnectionPool.open(SQLiteConnectionPool.java:189) at net.zetetic.database.sqlcipher.SQLiteConnectionPool.open(SQLiteConnectionPool.java:181) at net.zetetic.database.sqlcipher.SQLiteDatabase.openInner(SQLiteDatabase.java:1028) at net.zetetic.database.sqlcipher.SQLiteDatabase.open(SQLiteDatabase.java:1013) at net.zetetic.database.sqlcipher.SQLiteDatabase.openDatabase(SQLiteDatabase.java:840) at net.zetetic.database.sqlcipher.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:359) at net.zetetic.database.sqlcipher.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:278) at net.zetetic.database.sqlcipher.SupportHelper.getWritableDatabase(SupportHelper.java:60) at androidx.room.RoomDatabase.inTransaction(RoomDatabase.kt:638) at androidx.room.RoomDatabase.assertNotSuspendingTransaction(RoomDatabase.kt:457) at androidx.room.RoomDatabase.compileStatement(RoomDatabase.kt:502)

@xuhongchang
Copy link
Author

@andzhangs 后来我没升级库了 从 android-database-sqlcipher 4.5.4 到sqlcipher-android4.5.6
继续使用的
android-database-sqlciphe 只是升级了一下版本
这是两个库

@EpariNigam
Copy link

EpariNigam commented Aug 23, 2024

Hi I am also facing same issue. I tried opening with DB browser, I am able to open though.

I tried migrating from net.zetetic:android-database-sqlcipher:4.5.4 to net.zetetic:sqlcipher-android:4.5.4.

Here is a sample repo, I have created

https://github.com/EpariNigam/SQLCipher

@developernotes
Copy link
Member

Hi @EpariNigam,

Your demo application does not call System.loadLibrary("sqlcipher");.

@EpariNigam
Copy link

Hey @developernotes

I created 2 branches the main branch uses the old library, the new branch sql_cipher_migration uses the new library.

I request you to run the app from main branch, do click insert and fetch. See, entries got created.

Now from the sql_cipher_migration just click fetch, you will be able to reproduce the issue.

@EpariNigam
Copy link

Hi @developernotes hope with the mentioned provided steps, you are able to reproduce this issue.

@developernotes
Copy link
Member

developernotes commented Aug 26, 2024

Hi @EpariNigam,

In your branch sql_cipher_migration, your implementation of getBytes is incorrect. You are converting the string, to a char array, then converting the char array to a string (which no longer represents the original string), then requesting the bytes for that string 1. SQLCipher is not able to open an existing encrypted database with invalid password material.

If you change the EncryptedAppDatabase.kt file to define the passphrase as follows it works properly:

val passPhrase = "mydatabasekey".toByteArray(Charsets.UTF_8)

Footnotes

  1. https://github.com/EpariNigam/SQLCipher/blob/sql_cipher_migration/app/src/main/kotlin/com/nigam/dbsqlcipher/db/EncryptedAppDatabase.kt#L53-L56

@EpariNigam
Copy link

Thanks @developernotes I will give it a try and let you know.

@EpariNigam
Copy link

EpariNigam commented Aug 27, 2024

Thanks @developernotes, it worked. I though toString() will just give the output of String. But, forgot that it is an array and will give hashcode 🤦‍♂️.

@developernotes
Copy link
Member

Hi @EpariNigam,

No problem at all, glad the issue is resolved for you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants