Results 1 to 2 of 2

Thread: Android-backup and restore of encrypted database (sqlcipher, cacheword)?

  1. #1
    Senior Member
    Join Date
    Mar 2013
    Posts
    416,482

    Arrow Android-backup and restore of encrypted database (sqlcipher, cacheword)?

    I started to develop an android app using a sqlite database. After a while most of my wanted functions were implemented and I decided to insert database encryption. To achieve this I used sqlcipher in combination with cacheword to store and manage the key for encryption.
    To backup and restore my database I used in case of unencrypted database simple methods to make a copy of mydb.db file to sdcard and vice versa. With encryption both methods first did their job without an error message, but after restore the app could not use my database.
    The backup method:

    public static void BackupDatabase() throws IOException { boolean success = true; File file = null; file = new File(Environment.getExternalStorageDirectory() +"/myapp"); if(file.exists()) { success = true; } else { success = file.mkdir(); } if(success) { String inFileName = "/data/data/de.my.app/databases/mydb.db"; File dbFile = new File(inFileName); FileInputStream fis = new FileInputStream(dbFile); String outFileName = Environment.getExternalStorageDirectory()+"/myapp/mydb.db.backup"; // Open the empty db as the output stream OutputStream output = new FileOutputStream(outFileName); //transfer bytes from the inputfile to the outputfile byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer))>0) { output.write(buffer, 0, length); } output.flush(); output.close(); fis.close(); }}The restore method:

    public static void RestoreDatabase(Context context) throws IOException { try { // Set the folder on the SDcard File directory = new File(Environment.getExternalStorageDirectory()+"/myapp/"); boolean directoryB = new File(Environment.getExternalStorageDirectory()+"/myapp/", "/mydb.db.backup").exists(); if (directoryB == true) { OutputStream myOutput; myOutput = new FileOutputStream("/data/data/de.my.app/databases/mydb.db"); // Set the input file stream up: InputStream myInputs = new FileInputStream(directory.getPath()+ "/mydb.db.backup"); // Transfer bytes from the input file to the output file byte[] buffer = new byte[1024]; int length; while ((length = myInputs.read(buffer))>0) { myOutput.write(buffer, 0, length); } // Close and clear the streams myOutput.flush(); myOutput.close(); myInputs.close(); } else { Toast.makeText(context, "Wiederherstellung gescheitert! Datei nicht gefunden! Ordner/Datei existiert nicht?", Toast.LENGTH_SHORT).show(); } } catch (FileNotFoundException e) { Toast.makeText(context, "Wiederherstellung gescheitert! Datei nicht gefunden! Ordner/Datei existiert nicht?", Toast.LENGTH_LONG).show(); // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { Toast.makeText(context, "Wiederherstellung gescheitert!", Toast.LENGTH_SHORT).show(); // TODO Auto-generated catch block e.printStackTrace(); }}A part of my error messages:

    10-01 22:52:58.050: I/Database(4223): sqlite returned: error code = 26, msg = file is encrypted or is not a database10-01 22:52:58.050: E/Database(4223): CREATE TABLE android_metadata failed10-01 22:52:58.060: E/Database(4223): Failed to setLocale() when constructing, closing the database10-01 22:52:58.060: E/Database(4223): net.sqlcipher.database.SQLiteException: file is encrypted or is not a database10-01 22:52:58.060: E/Database(4223): at net.sqlcipher.database.SQLiteDatabase.native_setLo cale(Native Method)10-01 22:52:58.060: E/Database(4223): at net.sqlcipher.database.SQLiteDatabase.setLocale(SQ LiteDatabase.java:2102)10-01 22:52:58.060: E/Database(4223): at net.sqlcipher.database.SQLiteDatabase.(SQLiteDatab ase.java:1968)10-01 22:52:58.060: E/Database(4223): at net.sqlcipher.database.SQLiteDatabase.openDatabase (SQLiteDatabase.java:901)10-01 22:52:58.060: E/Database(4223): at net.sqlcipher.database.SQLiteDatabase.openOrCreate Database(SQLiteDatabase.java:944)10-01 22:52:58.060: E/Database(4223): at net.sqlcipher.database.SQLiteOpenHelper.getWritabl eDatabase(SQLiteOpenHelper.java:107)10-01 22:52:58.060: E/Database(4223): at info.guardianproject.cacheword.SQLCipherOpenHelper .getWritableDatabase(SQLCipherOpenHelper.java:53)H ow can I achieve to use a copy of an encrypted database as backup?


    Check Solution

  2. #2
    Junior Member
    Join Date
    Sep 2015
    Posts
    6
    Thanks for your post, the android device data transfer was a long question, i think it's very convenient to use a mobile phone file transfer and backup app to help. Just few simple steps to transfer everything from your phone to computer. how to transfer text messages from android to computer
    Last edited by eveeve; 09-18-2016 at 09:06 AM.

Similar Threads

  1. Replies: 0
    Last Post: 09-27-2013, 10:30 AM
  2. Replies: 0
    Last Post: 08-22-2013, 08:40 PM
  3. Replies: 0
    Last Post: 08-22-2013, 06:40 AM
  4. Replies: 0
    Last Post: 08-11-2013, 11:23 AM
  5. Replies: 0
    Last Post: 04-23-2013, 02:11 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •