0

I encrypted a SQLite database from Linux command line using the following way.

$ ./sqlcipher plaintext.db
sqlite> ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'MyPass123';
sqlite> SELECT sqlcipher_export('encrypted');
sqlite> DETACH DATABASE encrypted;

I did put the encrypted database in assets folder of my Android project and copying it in the following way.

InputStream mInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer)) > 0) {
    mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();

But when I am trying to open this database, I am getting error "File is encrypted or is not a database". I am using same key to open that I used for encrypting it on command line.

Opening the db in this way.

String mPath = DB_PATH + DB_NAME;   

mDataBase = SQLiteDatabase.openDatabase(mPath, "MyPass123", null, SQLiteDatabase.CREATE_IF_NECESSARY);

Am I missing anything?

Ravi Ranjan Singh
  • 809
  • 1
  • 6
  • 19
  • Confirm that you can open the database from the command line using your `sqlcipher` tool. Also, make `mOutput` be a `FileOutputStream` and call `getFD().sync()` after `flush()` and before `close()`, to ensure all bytes are written to disk before you start trying to use SQLCipher to access the database. Also, do not hardcode paths (if `DB_PATH` is hardcoded) and do not use string concatenation to create paths. – CommonsWare Mar 27 '16 at 20:12
  • Yes I was able to open the file using my command line sqlcipher tool :) – Ravi Ranjan Singh Mar 27 '16 at 21:25

0 Answers0