訪問github連接:https://github.com/sqlcipher/android-database-sqlcipher
java
訪問 http://sqlcipher.net/sqlcipher-for-android/ 查看官方介紹, 並從官方介紹中找出編譯成功以後的zip壓縮包進行下載
https://s3.amazonaws.com/sqlcipher/SQLCipher+for+Android+v3.1.0.zip
android
普通數據庫git
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(); } }
通過加密的數據庫github
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(); } }
發現導出來的數據庫文件,一個看到數據,一個看不到數據sql