2013-10-01 99 views
2

我開始開發一個使用sqlite數據庫的android應用程序。過了一段時間我的大部分想要的功能都實現了,我決定插入數據庫加密。爲了達到這個目的,我使用sqlcipher和cacheword結合來存儲和管理加密密鑰。備份和恢復加密數據庫(sqlcipher,cacheword)?

備份和恢復我的數據庫我使用未加密數據庫的簡單方法來將mydb.db文件複製到SD卡,反之亦然。通過加密,這兩種方法首先在沒有錯誤消息的情況下完成了他們的工作,但在恢復應用程序後,無法使用我的數據庫

的備份方法:

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(); 
    } 
} 

的恢復方法:

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(); 
    } 
} 

我的錯誤消息的一部分:

10-01 22:52:58.050: I/Database(4223): sqlite returned: error code = 26, msg = file is encrypted or is not a database 
10-01 22:52:58.050: E/Database(4223): CREATE TABLE android_metadata failed 
10-01 22:52:58.060: E/Database(4223): Failed to setLocale() when constructing, closing the database 
10-01 22:52:58.060: E/Database(4223): net.sqlcipher.database.SQLiteException: file is encrypted or is not a database 
10-01 22:52:58.060: E/Database(4223): at net.sqlcipher.database.SQLiteDatabase.native_setLocale(Native Method) 
10-01 22:52:58.060: E/Database(4223): at net.sqlcipher.database.SQLiteDatabase.setLocale(SQLiteDatabase.java:2102) 
10-01 22:52:58.060: E/Database(4223): at net.sqlcipher.database.SQLiteDatabase.<init>(SQLiteDatabase.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.openOrCreateDatabase(SQLiteDatabase.java:944) 
10-01 22:52:58.060: E/Database(4223): at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:107) 
10-01 22:52:58.060: E/Database(4223): at info.guardianproject.cacheword.SQLCipherOpenHelper.getWritableDatabase(SQLCipherOpenHelper.java:53) 

我如何能實現使用的副本加密數據庫作爲備份?


使用sqlcipher_export有助於對我的數據庫進行備份。但是現在我遇到了數據庫還原的新問題。

我嘗試下面的代碼:

SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(backupFile, backuppassword, null); 
db.execSQL("ATTACH DATABASE '" + dbFile + "' AS encrypted KEY '" + mCacheWord.getEncryptionKey() + "';"); 
... 

下面是錯誤信息:

10-30 00:56:42.845: I/Database(14407): sqlite returned: error code = 26, msg = statement aborts at 5: [ATTACH DATABASE '/data/data/.../databases/database.db' AS encrypted KEY '[[email protected]';] file is encrypted or is not a database 
10-30 00:56:42.845: E/Database(14407): Failure 26 (file is encrypted or is not a database) on 0x63bdedb0 when executing 'ATTACH DATABASE '/data/data/.../databases/database.db' AS encrypted KEY '[[email protected]';' 

我不知道如何解決這個問題。你可以幫我嗎?

回答

0

您應該使用sql_export函數來備份,解密和加密您的數據庫。對於上面的錯誤解決方案是在標誌中使用NO_LOCALIZED_COLLATORS,但我不確定你在哪裏得到上述錯誤。您應該在嘗試使用SQLiteDatabase類連接到數據庫的地方獲取上述錯誤。你可以發佈你的代碼連接到你的數據庫後恢復