当前位置:网站首页>Android SQLite database encryption
Android SQLite database encryption
2022-07-05 09:46:00 【Black Mountain demon 2018】
Android sqlite Database encryption
Demand background
Android System native SQlite Is plaintext storage , Encryption is not supported , In this way, once the third party gets the device db file , It is equivalent to exposing everything , In this context , Have to encrypt the database .
Solution
The project uses greendao Database framework , and greendao It supports database encryption , It is open source SQLCipher To encrypt .
Yes sqlite Encrypt the database
1. because greendao I'm already dependent on SQLCipher, therefore app No additional dependency is required
2. The program needs to be loaded during initialization SQLCipher The required SO library
SQLiteDatabase.loadLibs(context);
3. Encrypt database
private void encrypt(String password, File fileDir) {
try {
File dbFile = new File(fileDir, DB_NAME);
File encryptDbFile = new File(fileDir, DB_NAME_ENCRYPT);
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(dbFile.getAbsolutePath(), "", null);
//SQLiteDatabase database = SQLiteDatabase.openDatabase(dbFile.getAbsolutePath(), "", null, SQLiteDatabase.OPEN_READWRITE);
// Connect to the encrypted database , And set the password
database.rawExecSQL(String.format("ATTACH DATABASE '%s' as encrypted KEY '" + password + "';", encryptDbFile.getAbsolutePath()));
// Output the database table and data to be encrypted to the encrypted database file
database.rawExecSQL("SELECT sqlcipher_export('encrypted');");
// Disconnect from the encrypted database
database.rawExecSQL("DETACH DATABASE encrypted;");
SQLiteDatabase decrypteddatabase = SQLiteDatabase.openDatabase(encryptDbFile.getAbsolutePath(), password, null, SQLiteDatabase.OPEN_READWRITE);
decrypteddatabase.setVersion(database.getVersion());
decrypteddatabase.close();
database.close();
LogPrint.i("DbCore", "end encrypt");
} catch (Exception e) {
LogPrint.i("DbCore", e.getMessage());
e.printStackTrace();
}
4. Decrypt database
public void decrypt(String password, File fileDir) {
LogPrint.i("DbCore", "start decrypt");
try {
File dbFile = new File(fileDir, DB_NAME);
File encryptDbFile = new File(fileDir, DB_NAME_ENCRYPT);
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(encryptDbFile.getAbsolutePath(), password, null);
//SQLiteDatabase database = SQLiteDatabase.openDatabase(encryptDbFile.getAbsolutePath(), password, null, SQLiteDatabase.OPEN_READWRITE);
// Connect to the encrypted database , And set the password
database.rawExecSQL(String.format("ATTACH DATABASE '%s' as encrypted KEY '';", dbFile.getAbsolutePath()));
// Output the database table and data to be encrypted to the encrypted database file
database.rawExecSQL("SELECT sqlcipher_export('encrypted');");
// Disconnect from the encrypted database
database.rawExecSQL("DETACH DATABASE encrypted;");
SQLiteDatabase decrypteddatabase = SQLiteDatabase.openDatabase(dbFile.getAbsolutePath(), "", null, SQLiteDatabase.OPEN_READWRITE);
decrypteddatabase.setVersion(database.getVersion());
decrypteddatabase.close();
database.close();
LogPrint.i("DbCore", "end decrypt");
} catch (Exception e) {
LogPrint.i("DbCore", e.getMessage());
e.printStackTrace();
}
}
5. Get the read-write database object with password
helper.getEncryptedWritableDb(DB_PASSWORD)
边栏推荐
- Tutorial on building a framework for middle office business system
- OpenGL - Model Loading
- An article takes you into the world of cookies, sessions, and tokens
- From "chemist" to developer, from Oracle to tdengine, two important choices in my life
- How to implement complex SQL such as distributed database sub query and join?
- MySQL installation configuration and creation of databases and tables
- [two objects merged into one object]
- Tdengine already supports the industrial Intel edge insight package
- 基于模板配置的数据可视化平台
- Unity skframework framework (XXIII), minimap small map tool
猜你喜欢

干货整理!ERP在制造业的发展趋势如何,看这一篇就够了

SQL learning - case when then else
![[ManageEngine] how to make good use of the report function of OpManager](/img/15/dc15e638ae86d6cf1d5b989fe56611.jpg)
[ManageEngine] how to make good use of the report function of OpManager

The writing speed is increased by dozens of times, and the application of tdengine in tostar intelligent factory solution

Observation cloud and tdengine have reached in-depth cooperation to optimize the cloud experience of enterprises

一文读懂TDengine的窗口查询功能

What should we pay attention to when entering the community e-commerce business?

TDengine 连接器上线 Google Data Studio 应用商店

Go 语言使用 MySQL 的常见故障分析和应对方法

项目实战 | Excel导出功能
随机推荐
Tdengine already supports the industrial Intel edge insight package
【两个对象合并成一个对象】
干货整理!ERP在制造业的发展趋势如何,看这一篇就够了
Solve liquibase – waiting for changelog lock Cause database deadlock
Greendao reported an error in qigsaw, could not init daoconfig
项目实战 | Excel导出功能
Lepton 无损压缩原理及性能分析
为什么不建议你用 MongoDB 这类产品替代时序数据库?
【sourceTree配置SSH及使用】
LeetCode 556. 下一个更大元素 III
代码语言的魅力
From "chemist" to developer, from Oracle to tdengine, two important choices in my life
基于宽表的数据建模应用
Talking about the difference between unittest and pytest
About getfragmentmanager () and getchildfragmentmanager ()
如何正确的评测视频画质
[technical live broadcast] how to rewrite tdengine code from 0 to 1 with vscode
Mobile heterogeneous computing technology GPU OpenCL programming (Advanced)
LeetCode 31. Next spread
Small program startup performance optimization practice