数据库开源框架之sqlcipher加密数据库

时间:2021-08-14 02:04:47

访问github链接:https://github.com/sqlcipher/android-database-sqlcipher


访问 http://sqlcipher.net/sqlcipher-for-android/ 查看官方介绍,
并从官方介绍中找出编译成功之后的zip压缩包进行下载
https://s3.amazonaws.com/sqlcipher/SQLCipher+for+Android+v3.1.0.zip


普通数据库

public interface MyDB {

    String name = "my.db";
int version = 1; public interface MyTable {
String name = "test";
String column_id = "_id";
String column_name = "name";
String create_sql = "create table " + name + "(" + column_id
+ " integer primary key autoincrement, " + column_name
+ " text)";
}
}

public class NativeOpenHelper extends SQLiteOpenHelper {

    public NativeOpenHelper(Context ctx) {
super(ctx, MyDB.name, null, MyDB.version);
} @Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(MyTable.create_sql);
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
}

public class NativeDao {

    private NativeOpenHelper mHelper;

    public NativeDao(Context ctx) {
mHelper = new NativeOpenHelper(ctx);
} public void add(String name) {
SQLiteDatabase database = mHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MyTable.column_name, name);
database.insert(MyTable.name, null, values);
database.close();
}
}

经过加密的数据库

public interface SafeDB {

    String name = "safe.db";
int version = 1; public interface SafeTable {
String name = "test";
String column_id = "_id";
String column_name = "name";
String create_sql = "create table " + name + "(" + column_id
+ " integer primary key autoincrement, " + column_name
+ " text)";
} }

public class SafeDao {

    private SafeOpenHelper mHelper;

    public SafeDao(Context ctx) {
mHelper = new SafeOpenHelper(ctx);
} public void add(String name) {
SQLiteDatabase database = mHelper.getWritableDatabase("123456");
ContentValues values = new ContentValues();
values.put(SafeTable.column_name, name);
database.insert(SafeTable.name, null, values);
database.close();
} public void query() {
SQLiteDatabase database = mHelper.getWritableDatabase("123456");
Cursor cursor = database.query(SafeTable.name,
new String[] { SafeTable.column_name }, null, null, null, null, null); if (cursor != null) {
while (cursor.moveToNext()) {
String name = cursor.getString(0);
System.out.println(name);
} cursor.close();
} database.close();
}
}

import android.content.Context;
import com.loaderman.sqlitedemo.SafeDB.SafeTable; public class SafeDBHelper extends net.sqlcipher.database.SQLiteOpenHelper { public SafeDBHelper(Context ctx) {
super(ctx, SafeDB.name, null, SafeDB.version);
} @Override
public void onCreate(net.sqlcipher.database.SQLiteDatabase db) {
db.execSQL(SafeTable.create_sql);
} @Override
public void onUpgrade(net.sqlcipher.database.SQLiteDatabase arg0, int arg1,
int arg2) { } }

public class MainActivity extends Activity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //加载本地库
SQLiteDatabase.loadLibs(this);
} public void normalDB(View view) {
NativeDao dao = new NativeDao(this);
dao.add("hahahhaha");
} public void safeDB(View view) {
SafeDao dao = new SafeDao(this);
dao.add("hehehheheheh");
dao.query();
}
}

发现导出来的数据库文件,一个看到数据,一个看不到数据