当前位置:网站首页>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)
边栏推荐
- E-commerce apps are becoming more and more popular. What are the advantages of being an app?
- LeetCode 31. Next spread
- idea用debug调试出现com.intellij.rt.debugger.agent.CaptureAgent,导致无法进行调试
- [two objects merged into one object]
- Lepton 无损压缩原理及性能分析
- 使用el-upload封装得组件怎么清空已上传附件
- Kotlin introductory notes (VI) interface and function visibility modifiers
- [Yugong series] go teaching course 003-ide installation and basic use in July 2022
- H.265编码原理入门
- TDengine 离线升级流程
猜你喜欢

代码语言的魅力

Uncover the practice of Baidu intelligent testing in the field of automatic test execution

【数组的中的某个属性的监听】

Why do offline stores need cashier software?

idea用debug调试出现com.intellij.rt.debugger.agent.CaptureAgent,导致无法进行调试

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

Dry goods sorting! How about the development trend of ERP in the manufacturing industry? It's enough to read this article

Unity SKFramework框架(二十四)、Avatar Controller 第三人称控制

解决Navicat激活、注册时候出现No All Pattern Found的问题
随机推荐
TDengine可通过数据同步工具 DataX读写
Cloud computing technology hotspot
TDengine 离线升级流程
How do enterprises choose the appropriate three-level distribution system?
[NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution
7 月 2 日邀你来TD Hero 线上发布会
How to choose the right chain management software?
Mobile heterogeneous computing technology GPU OpenCL programming (Advanced)
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
百度评论中台的设计与探索
90%的人都不懂的泛型,泛型的缺陷和应用场景
LeetCode 503. Next bigger Element II
干货整理!ERP在制造业的发展趋势如何,看这一篇就够了
Why don't you recommend using products like mongodb to replace time series databases?
uni-app---uni. Navigateto jump parameter use
From "chemist" to developer, from Oracle to tdengine, two important choices in my life
解决Navicat激活、注册时候出现No All Pattern Found的问题
SQL learning group by multi table grouping scenario
Principle and performance analysis of lepton lossless compression
【两个对象合并成一个对象】