net.sqlcipher.database.SQLiteException: not authorized: , while compiling: select count(*) from sqlite_master

SQLCipher version - 3.5.9, Android OS - Android 6.0

  • We are already loading the library using - SQLiteDatabase.loadLibs(context); at the start of the app before calling any database related api.

  • Database password is also plain text without any encryption.

While calling getWritableDatabase(getPassword()), sqlcipher is throwing following exception -

net.sqlcipher.database.SQLiteException: not authorized: , while compiling: select count(*) from sqlite_master;
                                                              at net.sqlcipher.database.SQLiteCompiledSql.native_compile(Native Method)
                                                              at net.sqlcipher.database.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
                                                              at net.sqlcipher.database.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
                                                              at net.sqlcipher.database.SQLiteProgram.<init>(SQLiteProgram.java:89)
                                                              at net.sqlcipher.database.SQLiteQuery.<init>(SQLiteQuery.java:48)
                                                              at net.sqlcipher.database.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:60)
                                                              at net.sqlcipher.database.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1867)
                                                              at net.sqlcipher.database.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1785)
                                                              at net.sqlcipher.database.SQLiteDatabase.keyDatabase(SQLiteDatabase.java:2486)
                                                              at net.sqlcipher.database.SQLiteDatabase.openDatabaseInternal(SQLiteDatabase.java:2415)
                                                              at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1149)
                                                              at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1212)
                                                              at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:162)
                                                              at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:129)
                                                              at com.cropin.smartfarm.core.db.DBHelper.getDb(DBHelper.java:93)

Hello @CropIn_Developer - You will receive a not authorized error if you are using SQLCipher Commercial Edition and your license key is invalid or has expired (i.e. if you are using a trial version and the trial period is over).

good afternoon

in android studio, in which section I have to enter the password they gave me.

PRAGMA cipher_license = trial license

@Ichigo_Kurosaki

You can apply it in either the preKey or postKey hook.

private void InitializeSQLCipher() {
    SQLiteDatabase.loadLibs(this);
    SQLiteDatabaseHook mHook = new SQLiteDatabaseHook() {
        public void preKey(SQLiteDatabase database) {
            // no-op
        }

        public void postKey(SQLiteDatabase database) {
            database.rawExecSQL("PRAGMA cipher_license='YOUR_LICENSE';");
        }
    };
    File databaseFile = getDatabasePath("demo.db");
    databaseFile.mkdirs();
    databaseFile.delete();
    SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFile, "B1GS3CR3T", null, mHook);
    database.execSQL("create table t1(a, b)");
    database.execSQL("insert into t1(a, b) values(?, ?)", new Object[]{"One for the Money",
            "Two For the Show"});
    database.close();
}
1 Like

good afternoon
I appreciate your support helped me too, I share how you implement it.

private static DBHelper instancia;
private static final int DB_version=1;
public static final String DB_name=“ppp.db”;
public static final String License_PHARSE = “my_License”;

public DBHelper(Context context,SQLiteDatabaseHook val) {

   super(context, DB_name, null, DB_version,val);
}

static public synchronized DBHelper getInstance(Context context){
    SQLiteDatabaseHook myh=new SQLiteDatabaseHook() {
        @Override
        public void preKey(SQLiteDatabase sqLiteDatabase) {

        }

        @Override
        public void postKey(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.rawExecSQL("PRAGMA cipher_license='"+License_PHARSE+"'");
        }
    };
    if(instancia == null)
        instancia = new DBHelper(context,myh);
    return instancia;
}

use SQLiteOpenHelper

1 Like