jeudi 7 mai 2015

How to encrypt a SQLite Database

Hello I am still kinda new to the Android scene and I am currently hitting a wall. I am making an app to hold all my online accounts and passwords, just something simple, and I want to encrypt the database. I am currently trying to work with SQLCipher and started reading http://ift.tt/1jbGBDR but for whatever reason I can't get it to implement correctly.

I have the .jar in and the .so's, I did add the .jar file to the library. but the "import info.guardianproject.sqlite.SQLiteDatabase;" statement to work.

I also have been at this app for hours now and am getting burned out, might be half the battle.

import info.guardianproject.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;

public class PasswordDatabaseHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
public static String TABLE;

public static final String COLUMN_ID = "_id";
public static final String COLUMN_WEBSITE  = "website";
public static final String COLUMN_ACCOUNT = "account";
public static final String COLUMN_PASS = "password";



public PasswordDatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name + ".db", factory, DATABASE_VERSION);
    TABLE = name;
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_TABLE = "CREATE TABLE " + TABLE + "(" + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_WEBSITE + " TEXT," + COLUMN_ACCOUNT + " TEXT," + COLUMN_PASS + " TEXT" + ")";

    db.execSQL(CREATE_TABLE);
}

//ADD IN CHECK FOR USER

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
                      int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE);
    onCreate(db);

}

public boolean checkAccount(String website, String account) {
    String query = "Select * FROM " + TABLE + " WHERE " + COLUMN_WEBSITE + " = \"" + website + "\"";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    boolean flag = false;

    if (cursor.moveToFirst()) {
        cursor.moveToFirst();
        if(cursor.getString(1).equals(website) && cursor.getString(2).equals(account))
            flag = true;
    } else {
        flag = false;
    }
    cursor.close();
    db.close();
    return flag;
}

public void addAccount(String website, String account, String pass) {

    ContentValues values = new ContentValues();
    values.put(COLUMN_WEBSITE, website);
    values.put(COLUMN_ACCOUNT, account);
    values.put(COLUMN_PASS, pass);

    SQLiteDatabase db = this.getWritableDatabase();
    db.insert(TABLE, null, values);
    db.close();

}

public String lookupAccount(String website, String account) {
    String query = "Select * FROM " + TABLE + " WHERE " + COLUMN_WEBSITE + " = \"" + website + "\"";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    String info = "";

    if (cursor.moveToFirst()) {
        cursor.moveToFirst();
        if(cursor.getString(2).equals(account)) {
            info += website + " Account: " + account + " Password: " + cursor.getString(3) + "\n";
            cursor.close();
        }
        else{
            info = null;
        }
    }
    else {
        info = null;
    }
    cursor.close();
    db.close();
    return info;
}

public boolean removeAccount(String website, String account) {

    boolean result = false;

    String query = "Select * FROM " + TABLE + " WHERE " + COLUMN_WEBSITE + " = \"" + website + "\"";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    if (cursor.moveToFirst()) {
        if(cursor.getString(2).equals(account)) {
            db.delete(TABLE, COLUMN_ID + " = ?", new String[]{String.valueOf(Integer.parseInt(cursor.getString(0)))});
            cursor.close();
            result = true;
        }
    }
    cursor.close();
    db.close();
    return result;
}

public boolean updateAccount(String website, String account, String pass) {  // KYLE - Find the Contact object set quantity and return object
    //        adds find and new contact functions together

    String clCommand = "Select * FROM " + TABLE + " WHERE " + COLUMN_WEBSITE + " = \"" + website + "\"";

    SQLiteDatabase dataWriter = this.getWritableDatabase();

    Cursor cursor =  dataWriter.rawQuery(clCommand, null);

    boolean updateOK = false;

    if (cursor.moveToFirst()) {
        cursor.moveToFirst();

        ContentValues values = new ContentValues();
        values.put(COLUMN_WEBSITE, website);
        values.put(COLUMN_ACCOUNT, account);
        values.put(COLUMN_PASS, pass);

        dataWriter.update(TABLE, values, COLUMN_ID + " = ?", new String[] { String.valueOf(Integer.parseInt(cursor.getString(0))) });
        updateOK = true;
    } else {
        updateOK = false;
    }
    dataWriter.close();
    cursor.close();
    return updateOK;

}

}

This is a class I made to handle everything to do with the database not an activity.

Aucun commentaire:

Enregistrer un commentaire