Proper use sqlcipher for Android

 android-database-sqlcipher based database encryption SQLCipher the frame, to support android4 android9, often used to encrypt the android SqlLite, now supports Gradle integration, if you want to support androidx, Room frame may be used, may be used with acoustic SQLiteOpenHelper .

First, add a dependency in your build.gradle in:

implementation 'net.zetetic:android-database-sqlcipher:4.2.0'  
If you want to make the program compatible with Room androidx github page reference frame used.

Here are how and native API used in conjunction with:

1, all android.database.sqlite * introducing change all net.sqlcipher.database *, for example android.database.sqlite.SQLiteDatabase changed net.sqlcipher.database.SQLiteDatabase.;

2, in your oncreate method of application which calls SQLiteDatabase.loadLibs (); application context passed as a parameter;

3, you get a database object when using SQLiteDatabase.g (), passing the password, supports String, byte [], char [] as a password;

Here's a guide to use as a page Introduction https://www.zetetic.net/sqlcipher/sqlcipher-for-android/

 

Here to talk about the problems that may occur during use

  •  SQLiteDatabase mSQLiteDatabase = helper.getWritableDatabase(GlobalConst.DB_PASSWORD);  

Although the guide pages are database objects created in onCreate years, but the actual implementation of this code takes very seriously, usually about 0.8s-1.5s, unlike the use of native Caton is not fast, it is recommended to maintain a global singleton handle, you can set the application to start loading pages asynchronously load the database object for the application of global variables or every time you need to obtain db objects when they are using asynchronous operation, and if you use the singleton pattern, at the same time to ensure that each db object database operation is no longer the same thread, so all data manipulation recommendations with the asynchronous operation.

  • The password is correct, but not open data, if you have a number of similar objects using one inherited from DBHelper, the encryption key is the same also can not operate, either merge these classes, or to separate the database

Guess you like

Origin www.cnblogs.com/yjpjy/p/11359595.html