Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 备份和恢复加密数据库(sqlcipher、cacheword)?_Android_Database_Encryption_Sqlcipher - Fatal编程技术网

Android 备份和恢复加密数据库(sqlcipher、cacheword)?

Android 备份和恢复加密数据库(sqlcipher、cacheword)?,android,database,encryption,sqlcipher,Android,Database,Encryption,Sqlcipher,我开始使用sqlite数据库开发android应用程序。过了一会儿,我想要的大部分函数都实现了,我决定插入数据库加密。为了实现这一点,我将sqlcipher与cacheword结合使用来存储和管理加密密钥 为了备份和恢复我的数据库,我在未加密的数据库中使用了一些简单的方法,将mydb.db文件复制到SD卡,反之亦然。使用加密,这两种方法首先在没有错误消息的情况下完成工作,但在恢复后,应用程序无法使用我的数据库 备份方法: public static void BackupDatabase() t

我开始使用sqlite数据库开发android应用程序。过了一会儿,我想要的大部分函数都实现了,我决定插入数据库加密。为了实现这一点,我将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)
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 '[B@42082da0';] 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 '[B@42082da0';'
以下是错误消息:

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)
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 '[B@42082da0';] 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 '[B@42082da0';'

我不知道如何解决这个问题。您能帮助我吗?

您应该使用sql\u导出功能来备份、解密和加密您的数据库。对于上述错误,解决方案是在标志中使用NO_LOCALIZED_collator,但我不确定您从何处获得上述错误。您应该在尝试使用SQLiteDatabase类连接到数据库的位置处出现上述错误。你能在恢复后将代码发布到连接到数据库的位置吗