当前位置:网站首页>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)
边栏推荐
- 初识结构体
- 观测云与 TDengine 达成深度合作,优化企业上云体验
- LeetCode 503. 下一个更大元素 II
- 植物大战僵尸Scratch
- La voie de l'évolution du système intelligent d'inspection et d'ordonnancement des petites procédures de Baidu
- 百度智能小程序巡檢調度方案演進之路
- Solve liquibase – waiting for changelog lock Cause database deadlock
- Lepton 无损压缩原理及性能分析
- Android privacy sandbox developer preview 3: privacy, security and personalized experience
- OpenGL - Lighting
猜你喜欢

How to choose the right chain management software?

Deep understanding of C language pointer

Tdengine already supports the industrial Intel edge insight package

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

Mobile heterogeneous computing technology GPU OpenCL programming (Advanced)

LeetCode 496. 下一个更大元素 I

mysql安装配置以及创建数据库和表

正式上架!TDengine 插件入驻 Grafana 官网

H.265编码原理入门

The most comprehensive promotion strategy: online and offline promotion methods of E-commerce mall
随机推荐
H.265编码原理入门
Evolution of Baidu intelligent applet patrol scheduling scheme
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
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
分布式数据库下子查询和 Join 等复杂 SQL 如何实现?
Unity SKFramework框架(二十二)、Runtime Console 运行时调试工具
SQL learning alter add new field
How to correctly evaluate video image quality
Analysis of eventbus source code
基于宽表的数据建模应用
MySQL does not take effect in sorting string types
Understanding of smt32h7 series DMA and DMAMUX
Why don't you recommend using products like mongodb to replace time series databases?
从“化学家”到开发者,从甲骨文到 TDengine,我人生的两次重要抉择
从“化学家”到开发者,从甲骨文到TDengine,我人生的两次重要抉择
About getfragmentmanager () and getchildfragmentmanager ()
Oracle combines multiple rows of data into one row of data
[hungry dynamic table]
SQL learning - case when then else
Understand the window query function of tdengine in one article