Como cifrar uma base de dados SQLite

Olá, ainda sou novo na cena do Andróide e estou a bater numa parede. Estou fazendo um aplicativo para manter todas as minhas contas online e senhas, apenas algo simples, e eu quero criptografar a base de dados. Estou a tentar trabalhar com o SQLCipher e comecei a ler https://guardianproject.info/code/sqlcipher mas, por qualquer razão, não consigo pô-lo a funcionar correctamente.

eu tenho o .jar in and the. so's, I did add the .jar file para a biblioteca. mas o "importar informação.projecto Guardian.sqlite.SQLiteDatabase; " a declaração não funciona.

Também estou nesta aplicação há horas e estou a ficar exausto, pode ser metade da batalha.

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);
}

@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) {

    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;

}

}
Esta é uma aula que fiz para lidar com tudo o que tem a ver com a base de dados e não com uma actividade.

Editar * *

Há um login para este aplicativo com um número pin, esse número pin é o que eu ia usar para descriptografar. Se este pin for esquecido, você pode enviá-lo por e-mail para o e-mail de login.

Author: Kyle, 2015-05-08

1 answers

Que tal AES128?
package com.test.util;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AesUtil {

    public static String key = "0000000000000090";

    /**
     * hex to byte[] : 16dd
     * @param hex    hex string
     * @return
     */
    public static byte[] hexToByteArray(String hex) {
        if (hex == null || hex.length() == 0) {
            return null;
        }

        byte[] ba = new byte[hex.length() / 2];
        for (int i = 0; i < ba.length; i++) {
            ba[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
        }
        return ba;
    }

    /**
     * byte[] to hex : unsigned byte
     *
     * @param ba        byte[]
     * @return
     */
    public static String byteArrayToHex(byte[] ba) {
        if (ba == null || ba.length == 0) {
            return null;
        }

        StringBuffer sb = new StringBuffer(ba.length * 2);
        String hexNumber;
        for (int x = 0; x < ba.length; x++) {
            hexNumber = "0" + Integer.toHexString(0xff & ba[x]);

            sb.append(hexNumber.substring(hexNumber.length() - 2));
        }
        return sb.toString();
    }

    /**
     * AES 
     *
     * @param message
     * @return
     * @throws Exception
     */
    public static String encrypt(String message) throws Exception {
        //KeyGenerator kgen = KeyGenerator.getInstance("AES");
        //kgen.init(128);
        // use key coss2
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");

        // Instantiate the cipher
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

        byte[] encrypted = cipher.doFinal(message.getBytes());
        return byteArrayToHex(encrypted);
    }

    /**
     * AES 
     *
     * @param message
     * @return
     * @throws Exception
     */
    public static String decrypt(String encrypted) throws Exception {
        //KeyGenerator kgen = KeyGenerator.getInstance("AES");
        //kgen.init(128);
        // use key coss2
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");

        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        byte[] original = cipher.doFinal(hexToByteArray(encrypted));
        String originalString = new String(original);
        return originalString;
    }
}
 1
Author: DonisYim, 2015-05-08 02:19:58