我尝试使用SQLCipher来加密/解密我的数据库。它似乎有效,但我想确定我正确地做到了。 这是我的代码:
public class MainActivity extends Activity {
private DataBaseCategory dbc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//this.deleteDatabase("category");
dbc = new DataBaseCate gory(this);
try {
dbc.openToWriting();
dbc.createCategory("TEST4");
dbc.close();
}
catch(Throwable ex) {
Log.e("ABC","abc",ex);
}
dbc.openToReading();
List<Category> list = dbc.getAllCategories();
System.out.println("Size : "+list.size());
dbc.close();
}
public class DataBaseCategory {
private SQLiteDatabase database;
private Context context;
private DataBaseCategoryHelper dbCHelper;
private String password = "password";
private String[] allColumns = { DataBaseCategoryHelper.COLUMN_ID,
DataBaseCategoryHelper.COLUMN_CATEGORY_NAME };
public DataBaseCategory(Context context) {
this.context = context;
SQLiteDatabase.loadLibs(context);
dbCHelper = new DataBaseCategoryHelper(this.context);
}
public void openToWriting() throws SQLException{
database = dbCHelper.getWritableDatabase(password);
}
public void openToReading() throws SQLException {
database = dbCHelper.getReadableDatabase(password);
}
public void close() {
dbCHelper.close();
}
/**
* Not important part of code
*/
}
public class DataBaseCategoryHelper extends SQLiteOpenHelper{
public static final String TABLE_COMMENTS = "category";
public static final String COLUMN_ID = "id";
public static final String COLUMN_CATEGORY_NAME = "category_name";
private static final String DATABASE_NAME = "category";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table "
+ TABLE_COMMENTS + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_CATEGORY_NAME
+ " text not null);";
public DataBaseCategoryHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
try {
database.execSQL(DATABASE_CREATE);
}
catch(Exception ex ) {
Log.e("ErroeDBC ",ex.toString());
}
}
public class MainActivity extends Activity {
private DataBaseCategory dbc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//this.deleteDatabase("category");
dbc = new DataBaseCate gory(this);
try {
dbc.openToWriting();
dbc.createCategory("TEST4");
dbc.close();
}
catch(Throwable ex) {
Log.e("ABC","abc",ex);
}
dbc.openToReading();
List<Category> list = dbc.getAllCategories();
System.out.println("Size : "+list.size());
dbc.close();
}
public class DataBaseCategory {
private SQLiteDatabase database;
private Context context;
private DataBaseCategoryHelper dbCHelper;
private String password = "password";
private String[] allColumns = { DataBaseCategoryHelper.COLUMN_ID,
DataBaseCategoryHelper.COLUMN_CATEGORY_NAME };
public DataBaseCategory(Context context) {
this.context = context;
SQLiteDatabase.loadLibs(context);
dbCHelper = new DataBaseCategoryHelper(this.context);
}
public void openToWriting() throws SQLException{
database = dbCHelper.getWritableDatabase(password);
}
public void openToReading() throws SQLException {
database = dbCHelper.getReadableDatabase(password);
}
public void close() {
dbCHelper.close();
}
/**
* Not important part of code
*/
}
public class DataBaseCategoryHelper extends SQLiteOpenHelper{
public static final String TABLE_COMMENTS = "category";
public static final String COLUMN_ID = "id";
public static final String COLUMN_CATEGORY_NAME = "category_name";
private static final String DATABASE_NAME = "category";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table "
+ TABLE_COMMENTS + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_CATEGORY_NAME
+ " text not null);";
public DataBaseCategoryHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
try {
database.execSQL(DATABASE_CREATE);
}
catch(Exception ex ) {
Log.e("ErroeDBC ",ex.toString());
}
}
这是否真的需要加密/解密数据库? 我错过了什么吗?
答案 0 :(得分:2)
除了从用户那里收集密码,而不是硬编码,这就是你所需要的。 Android的SQLCipher与使用普通的SQLite只是逐渐不同:只需在使用它之前调用loadLibs()
,然后提供密码getReadableDatabase()
,getWriteableDatabase()
等。
随着时间的推移,您可能会遇到更多问题,例如当SQLCipher更改其数据库格式并且您需要执行升级时,从SQLCipher 2.x到3.0.x需要。但这些并不常见。