当前位置:网站首页>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)
边栏推荐
- 【数组的中的某个属性的监听】
- Small program startup performance optimization practice
- TDengine 离线升级流程
- Node の MongoDB Driver
- What should we pay attention to when entering the community e-commerce business?
- [reading notes] Figure comparative learning gnn+cl
- [object array A and object array B take out different elements of ID and assign them to the new array]
- 测试老鸟浅谈unittest和pytest的区别
- Thermometer based on STM32 single chip microcomputer (with face detection)
- Online chain offline integrated chain store e-commerce solution
猜你喜欢
Oracle combines multiple rows of data into one row of data
Unity skframework framework (24), avatar controller third person control
干货整理!ERP在制造业的发展趋势如何,看这一篇就够了
卷起来,突破35岁焦虑,动画演示CPU记录函数调用过程
Unity skframework framework (XXIII), minimap small map tool
Understanding of smt32h7 series DMA and DMAMUX
Data visualization platform based on template configuration
OpenGL - Lighting
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
Observation cloud and tdengine have reached in-depth cooperation to optimize the cloud experience of enterprises
随机推荐
单片机原理与接口技术(ESP8266/ESP32)机器人类草稿
How to correctly evaluate video image quality
百度交易中台之钱包系统架构浅析
分布式数据库下子查询和 Join 等复杂 SQL 如何实现?
百度智能小程序巡检调度方案演进之路
TDengine × Intel edge insight software package accelerates the digital transformation of traditional industries
SMT32H7系列DMA和DMAMUX的一点理解
From "chemist" to developer, from Oracle to tdengine, two important choices in my life
Analysis of eventbus source code
LeetCode 556. Next bigger element III
Unity skframework framework (XXII), runtime console runtime debugging tool
E-commerce apps are becoming more and more popular. What are the advantages of being an app?
tongweb设置gzip
观测云与 TDengine 达成深度合作,优化企业上云体验
Observation cloud and tdengine have reached in-depth cooperation to optimize the cloud experience of enterprises
Kotlin introductory notes (VIII) collection and traversal
First understanding of structure
百度APP 基于Pipeline as Code的持续集成实践
22-07-04 Xi'an Shanghao housing project experience summary (01)
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