SQLite加密(SQLchiper 謹慎使用,贊資源,大概3/40M)

sqlite數據庫加密

1.sqlite加密介紹

開源項目:sqlcihper

基於的解決方案:https://www.zetetic.net/sqlcipher/sqlcipher-for-android/

sqlite:關係型數據庫、版本3.0開源的,雖然沒有加密功能,但是作者已經提供了加密接口(收費版可用

sqlite官方:http://sqlite.org/

開源作者提供加密解決方案,下載地址:https://s3.amazonaws.com/sqlcipher/SQLCipher+for+Android+3.1.0.zip

2.加密實現

  • 1.導入jar包(Libs)、so庫(jniLibs)、資源(assets),並加載so庫

 

  • 2.繼承net.sqlcipher.database.SQLiteOpenHelper

 

  • 3.創建表

    語法和android原生沒任何區別:
    db.execSQL("CREATE TABLE username(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)");
    
  • 4.插入:需要密碼

    String name = et1.getText().toString();
    if(TextUtils.isEmpty(name)) return;
    CipherSqliteOpenHelper openHelper = new CipherSqliteOpenHelper(context);
    //不同之處在於:需要設置密碼
    SQLiteDatabase db = openHelper.getWritableDatabase("123456");
    ContentValues values = new ContentValues();
    values.put("name", name);
    long username = db.insert("username", null, values);
    Toast.makeText(context, "插入成功", Toast.LENGTH_SHORT).show();
    et1.setText("");
    db.close();
    
  • 5.查詢:也需要密碼

    CipherSqliteOpenHelper openHelper = new CipherSqliteOpenHelper(context);
    SQLiteDatabase db = openHelper.getReadableDatabase("123456");//密碼
    Cursor cursor = db.query("username", new String[]{"id", "name"}, null, null, null, null, null);
    if(cursor != null){
        while (cursor.moveToNext()){
            int id = cursor.getInt(0);
            String name = cursor.getString(1);
            Log.i("result", "id="+id+",name="+name);
        }
        cursor.close();
    }
    db.close();
    
  • 6.加密後打不開

 

  • 7.查詢沒問題

殺死adb 和重啓adb

 

SDK工具包,打開ddms可以看到SQLite數據

代碼:

  1. package com.example.sqliteciher;
  2. import android.content.ContentValues;
  3. import android.os.Bundle;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. import com.example.sqliteciher.db.MySQLiteHelper;
  9. import net.sqlcipher.database.SQLiteDatabase;
  10. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  11. private EditText mEt;
  12. private Button mBtn;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. initView();
  18. //初始化so庫
  19. SQLiteDatabase.loadLibs(this);
  20. initData();
  21. initListener();
  22. }
  23. /**
  24. * 初始化視圖
  25. * */
  26. public void initView(){
  27. mEt = (EditText) findViewById(R.id.et);
  28. mBtn = (Button) findViewById(R.id.btn);
  29. }
  30. /**
  31. * 初始化數據
  32. * */
  33. public void initData(){
  34. }
  35. /**
  36. * 初始化監聽
  37. * */
  38. public void initListener(){
  39. mBtn.setOnClickListener(this);
  40. }
  41. @Override
  42. public void onClick(View view) {
  43. String name = mEt.getText().toString().trim();
  44. if (name.isEmpty())
  45. return;
  46. try {//這裏要包裹異常,
  47. MySQLiteHelper helper = new MySQLiteHelper(this);
  48. SQLiteDatabase db = helper.getWritableDatabase("123456");
  49. ContentValues values = new ContentValues();
  50. values.put("name",name);
  51. db.insert("username",null,values);
  52. db.close();
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. }

  1. package com.example.sqliteciher.db;
  2. import android.content.Context;
  3. import net.sqlcipher.database.SQLiteDatabase;
  4. import net.sqlcipher.database.SQLiteOpenHelper;
  5. /**
  6. * @項目名: SQLiteCiher2
  7. * @包名: com.example.sqliteciher.db
  8. * @文件名: MySQLiteHelper
  9. * @創建者: Administrator
  10. * @創建時間: 2017/1/28 20:03
  11. * @描述: TODO
  12. */
  13. public class MySQLiteHelper extends SQLiteOpenHelper {
  14. private static final String TAG = "MySQLiteHelper";
  15. public MySQLiteHelper(Context context) {
  16. super(context, "sqlite.db", null, 1);
  17. }
  18. @Override
  19. public void onCreate(SQLiteDatabase sqLiteDatabase) {
  20. sqLiteDatabase.execSQL("CREATE TABLE username(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)");
  21. }
  22. @Override
  23. public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
  24. }
  25. }
發佈了38 篇原創文章 · 獲贊 5 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章