Hi, I’m a bit worried this may be duplicate issue, but not any answer helped to resolve my issue, so I would like to share my code to get some idea what’s going wrong.
private static synchronized void createSession(Context context) {
try {
SQLiteDatabase.loadLibs(context);
encrypt(context, DBOpenHelper.DB_NAME, "password");
} catch (IOException e) {
e.printStackTrace();
}
DBOpenHelper helper = new DBOpenHelper(context);
db = helper.getEncryptedWritableDb("password");
}
private static void encrypt(Context context, String dbName, String passphrase) throws IOException {
File originalFile = context.getDatabasePath(dbName);
File encryptedFile = context.getDatabasePath(ENCRYPTED_DB_NAME);
if (originalFile.exists() && !encryptedFile.exists()) {
encryptedFile.mkdir();
SQLiteDatabase.openOrCreateDatabase(encryptedFile, passphrase, null);
SQLiteDatabase db = SQLiteDatabase.openDatabase(originalFile.getAbsolutePath(), "", null, SQLiteDatabase.OPEN_READWRITE);
db.rawExecSQL(String.format("ATTACH DATABASE '%s' AS encrypted KEY '%s';",
encryptedFile.getAbsolutePath(), passphrase));
db.rawExecSQL("SELECT sqlcipher_export('encrypted')");
db.rawExecSQL("DETACH DATABASE encrypted;");
int version = db.getVersion();
db.close();
db = SQLiteDatabase.openDatabase(encryptedFile.getAbsolutePath(), passphrase, null, SQLiteDatabase.OPEN_READWRITE);
db.setVersion(version);
db.close();
originalFile.delete();
encryptedFile.renameTo(originalFile);
}
}
This is my code to encrypt original local unencrypted databases whenever the application is updated. However, I’m getting error when I try to open the encrypted database even though I’m using the same passphrase(in this case, “password”). Following is the error I’m receiving.
03-22 11:37:17.599 9080-9080/com.example E/SQLiteOpenHelper: Couldn't open %my-db-name% for writing (will try read-only):
net.sqlcipher.database.SQLiteException: file is not a database: , 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.getReadableDatabase(SQLiteOpenHelper.java:227)
at net.sqlcipher.database.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:214)
at org.greenrobot.greendao.database.DatabaseOpenHelper.getEncryptedWritableDb(DatabaseOpenHelper.java:134)
There is no problem when I pass empty string when I open my database: it just opens unencrypted database.
How can I open the encrypted database with my passphrase?