dimanche 27 mars 2016

File is encrypted or is not a database: SQLCipher in Android

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?

Aucun commentaire:

Enregistrer un commentaire