当前位置:网站首页>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)
边栏推荐
- oracle 多行数据合并成一行数据
- 植物大战僵尸Scratch
- 一篇文章带你走进cookie,session,Token的世界
- Tutorial on building a framework for middle office business system
- 解决Navicat激活、注册时候出现No All Pattern Found的问题
- [ctfhub] Title cookie:hello guest only admin can get flag. (cookie spoofing, authentication, forgery)
- Talking about the difference between unittest and pytest
- TDengine 已经支持工业英特尔 边缘洞见软件包
- LeetCode 31. Next spread
- Android 隐私沙盒开发者预览版 3: 隐私安全和个性化体验全都要
猜你喜欢
7 月 2 日邀你来TD Hero 线上发布会
移动端异构运算技术-GPU OpenCL编程(进阶篇)
High performance spark_ Transformation performance
TDengine × Intel edge insight software package accelerates the digital transformation of traditional industries
项目实战 | Excel导出功能
OpenGL - Model Loading
22-07-04 Xi'an Shanghao housing project experience summary (01)
解决Navicat激活、注册时候出现No All Pattern Found的问题
[ManageEngine] how to make good use of the report function of OpManager
Tdengine already supports the industrial Intel edge insight package
随机推荐
H. 265 introduction to coding principles
On July 2, I invite you to TD Hero online press conference
90%的人都不懂的泛型,泛型的缺陷和应用场景
分布式数据库下子查询和 Join 等复杂 SQL 如何实现?
How to implement complex SQL such as distributed database sub query and join?
LeetCode 556. 下一个更大元素 III
移动端异构运算技术-GPU OpenCL编程(进阶篇)
From "chemist" to developer, from Oracle to tdengine, two important choices in my life
Alibaba's ten-year test brings you into the world of APP testing
OpenGL - Coordinate Systems
LeetCode 31. 下一个排列
How to empty uploaded attachments with components encapsulated by El upload
Unity SKFramework框架(二十三)、MiniMap 小地图工具
观测云与 TDengine 达成深度合作,优化企业上云体验
What are the advantages of the live teaching system to improve learning quickly?
Tdengine connector goes online Google Data Studio app store
Solve liquibase – waiting for changelog lock Cause database deadlock
Small program startup performance optimization practice
What about wechat mall? 5 tips to clear your mind
干货整理!ERP在制造业的发展趋势如何,看这一篇就够了