SQLCipher for Android Tutorial, encrypt and backup SQLite database[Part3]

In Part1 , we have prepared the Database Handler Class, the User TableDefintion Class where other Tables can be also defined.
The User class ensures reading and writing data from the database.

In Part2  of this tutorial we have use the business logic to create and getting some user information from the database.

In this part, we will try to get and view the SQLite database file from the device and decrypt it using SQLiteManager for windows.

During the development you will be also not able to see the database file and open it with some SQLite Browser software, that is because your application resource folder will not be displayed in the internal device storage until you root your device or deploy the application. But there exist a way how to copy the database from the internal device to the external sdcard and from the sdcard we will be able to copy the database file to the Desktop and open it using SQLiteManager.

Hier is the copy code. We can placed anywhere in the Activity, but after creating the Database. More information  in Part 2.


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import android.os.Environment;
import android.util.Log;
import android.os.Environment;

 try {
    File sd = Environment.getExternalStorageDirectory();
    File data = Environment.getDataDirectory();

    if (sd.canWrite()) {
       	 Log.d("DatabaseHandler", "DatabaseHandler: can write in sd");
        //Replace with YOUR_PACKAGE_NAME and YOUR_DB_NAME
        String currentDBPath = "//data//{YOUR_PACKAGE_NAME}//databases//{YOUR_DB_NAME}.db";
       //Replace with YOUR_FOLDER_PATH and TARGET_DB_NAME in the SD card
        String copieDBPath = "/{YOUR_FOLDER_PATH}/{TARGET_DB_NAME}.db";
        File currentDB = new File(data, currentDBPath);
        File copieDB = new File(sd, copieDBPath);
        if (currentDB.exists()) {
       	  Log.d("DatabaseHandler", "DatabaseHandler: DB exist");
    	  @SuppressWarnings("resource")
	  FileChannel src = new FileInputStream(currentDB).getChannel();
    	  @SuppressWarnings("resource")
	  FileChannel dst = new FileOutputStream(copieDB).getChannel();
    	  dst.transferFrom(src, 0, src.size());
    	  src.close();
    	  dst.close();
    	}
    }
    } catch  (Exception e) {
        e.printStackTrace();
    }

After launching the application on the device, you will find the copied database in the defined copieDBPath.

Please note that  DB Browser for SQLite 3.5.0  software is not supporting SQLCipher decryption for Windows until i write this tutorial.

We will use SQLiteManager that support SQLCipher decryption.

Copie or move the database to your desktop and download the SQLiteManager for windows.

After executing the software open the database file, the SQLiteManager will ask you to give the database password. The Password is defined in the DatabaseHandler class

private static final String DB_PASSWD = "YOUR_SECRET_KEY_HERE";

SQLiteManager_DatabaseKey

You will be able now to see your decrypted database data.
SQLiteManager_Database

Fixes:
if you get one of this errors:
dalvikvm E ERROR: couldn't find native method
dalvikvm E Requested: Lnet/sqlcipher/database/SQLiteDatabase;.native_getDbLookaside

Be sure that you are using the latest SQLCipher for Android
and you have the ZIP file icudt46l.zip in the assets folder.