New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
集成WCDB后希望能实现解密数据库 #36
Comments
这个跟原来一样的,执行这些SQL语句就能解密 |
你好,我尝试解密数据库还是失败,烦请帮忙看一下原因,多谢! 打开wcdb加密数据库代码如下: 创建临时文件暂存代码: 数据迁移代码: 异常日志,如下: 06-19 16:17:22.214 7185-7185/com.wlx.debug W/wlx: main:SqliteCipherLogic [LOGIN_READLY]: CipherDB::onItemClick: common_new 06-19 16:17:22.215 7185-7185/com.wlx.debug I/WCDB.SQLiteConnectionPool: Max connection pool size is 1. 06-19 16:17:22.254 7185-7185/com.wlx.debug W/WCDB.SQLite: [SQLite ErrCode: 5] statement aborts at 1: [PRAGMA journal_mode=PERSIST] database is locked 06-19 16:17:23.852 7185-7185/com.wlx.debug I/WCDB.SQLiteConnection: executeForChangedRowCount took 1584ms - failed, sql="SELECT sqlcipher_export('encrypted')", exception="unknown error (code 0, errno -1): Queries can be performed using SQLiteDatabase query or rawQuery methods only.", changedRows=0 06-19 16:17:23.852 7185-7185/com.wlx.debug E/wlx: main:SqliteCipherLogic [LOGIN_READLY]: CipherDB::startDataBaseMigration OMG ...DataMigration has exception. unknown error (code 0, errno -1): Queries can be performed using SQLiteDatabase query or rawQuery methods only. |
@weilixin 你可以参看以下 WCDB的 WCDB接口和限制跟 Android Framework 一致,和 SQLCipher Android 在限制上有一点点不同,但原理是一样的(包括 kdf_iter 等参数),SQL语句不用改,可能要稍微改一下调用代码。 |
@John-He-928 感谢帮助,还是有些问题。 PS:失败原因应该是密码不对,但实际密码没问题,肯定正确,传入字符串还是byte数组呢? 异常日志,如下: (密码肯定正确,58位数字大小写英文混合) |
你可以打开加密的,然后attach没加密的,然后export |
@John-He-928 感谢支持! 失败日志如下: |
首先你需要明白几点:
DatabaseUtils.stringForQuery(db, "SELECT sqlcipher_export('main', 'old');", null); 它的意思是查询并返回一个字符串,实际上 sqlcipher_export 并不会返回字符串,但不需要理会,忽略返回值即可,里面的语句已经执行了。 所以我觉得你可以这么操作: // 先open了加密DB,得到 "db" 对象
// 将非加密DB挂载到 "db"
String sql = String.format("ATTACH DATABASE %s AS old KEY '';",
DatabaseUtils.sqlEscapeString(oldDbFile.getPath()));
db.execSQL(sql);
// 将数据从 "main"(加密db) 迁移到 "old"(非加密db)
db.beginTransaction();
DatabaseUtils.stringForQuery(db, "SELECT sqlcipher_export('old', 'main');", null);
db.setTransactionSuccessful();
db.endTransaction();
// 将old脱离
db.execSQL("DETACH DATABASE old;"); 有没有觉得很像例子?其实就是将sqlcipher_export两个参数调换一下。 |
可以了,感谢John细致耐心讲解。赞!!! |
可以通过 // 先open了加密DB,得到 "db" 对象 // 将加密DB (old)挂载到 "db" // 将数据从 "main"(加密db) 迁移到 "old"(加密db) // 将old脱离 |
你好,我最近也开始使用WCDB,在集成时出现了个问题,所以向您请教下。 this.dealTempDB() private fun dealTempDB() { |
你好,我们应用原来使用cipher android加密,现在集成了wcdb。为了方便调试问题,我们在debug版本中支持解密已经加密的数据库,请问wcdb如何将已经加密的数据库转为非加密数据库呢?
原来的方法,如下:
1.打开已经加密的数据库,创建临时文件
2.将数据迁移到临时文件(newFile就是临时文件)
// 开始数据迁移
try {
// db.rawExecSQL("PRAGMA cipher_default_kdf_iter = 4000;");
db.rawExecSQL(String.format("ATTACH DATABASE '%s' AS encrypted KEY '%s';",
newFile.getAbsolutePath(), (isDecode ? ""/* 无密 */ : password)));
db.rawExecSQL("SELECT sqlcipher_export('encrypted')");
db.rawExecSQL("DETACH DATABASE encrypted;");
db.close();
} catch (Exception e) {
LogUtil.e(TAG, "CipherDB::startDataBaseMigration OMG ...DataMigration has exception. "
+ e.getMessage());
throw new StopRequestException(DATA_TRANSFER_DATA_MIGRATION_ERROR, e);
}
3.临时文件生成新数据库
The text was updated successfully, but these errors were encountered: