当前位置:网站首页>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)
边栏推荐
- The research trend of map based comparative learning (gnn+cl) in the top paper
- [hungry dynamic table]
- Community group buying exploded overnight. How should this new model of e-commerce operate?
- [sorting of object array]
- Principle and performance analysis of lepton lossless compression
- 【饿了么动态表格】
- How to choose the right chain management software?
- Lepton 无损压缩原理及性能分析
- 【组队 PK 赛】本周任务已开启 | 答题挑战,夯实商品详情知识
- From "chemist" to developer, from Oracle to tdengine, two important choices in my life
猜你喜欢
OpenGL - Model Loading
Unity skframework framework (XXIII), minimap small map tool
[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details
LeetCode 503. 下一个更大元素 II
[technical live broadcast] how to rewrite tdengine code from 0 to 1 with vscode
What about wechat mall? 5 tips to clear your mind
LeetCode 556. 下一个更大元素 III
Android privacy sandbox developer preview 3: privacy, security and personalized experience
百度交易中台之钱包系统架构浅析
Node-RED系列(二九):使用slider与chart节点来实现双折线时间序列图
随机推荐
为什么不建议你用 MongoDB 这类产品替代时序数据库?
[how to disable El table]
Principle and performance analysis of lepton lossless compression
搞数据库是不是越老越吃香?
C language - input array two-dimensional array a from the keyboard, and put 3 in a × 5. The elements in the third column of the matrix are moved to the left to the 0 column, and the element rows in ea
TDengine 已经支持工业英特尔 边缘洞见软件包
TDengine 离线升级流程
植物大战僵尸Scratch
Kotlin introductory notes (VIII) collection and traversal
SMT32H7系列DMA和DMAMUX的一点理解
正式上架!TDengine 插件入驻 Grafana 官网
Unity SKFramework框架(二十四)、Avatar Controller 第三人称控制
Mobile heterogeneous computing technology GPU OpenCL programming (Advanced)
Dry goods sorting! How about the development trend of ERP in the manufacturing industry? It's enough to read this article
Oracle combines multiple rows of data into one row of data
A detailed explanation of the general process and the latest research trends of map comparative learning (gnn+cl)
项目实战 | Excel导出功能
uni-app---uni.navigateTo跳转传参使用
Community group buying has triggered heated discussion. How does this model work?
LeetCode 556. Next bigger element III