你好,我仍然是Android场景的新手,我目前正在打一堵墙。我正在制作一个应用程序来保存我的所有在线帐户和密码,只是简单的事情,我想加密数据库。我目前正在尝试使用SQLCipher并开始阅读https://guardianproject.info/code/sqlcipher/,但无论出于何种原因,我都无法正确实现它。
我有.jar和.so的,我确实将.jar文件添加到库中。但是“import info.guardianproject.sqlite.SQLiteDatabase;”声明不起作用。
我现在已经在这个应用程序上工作了好几个小时而且已经烧坏了,可能是成功的一半。
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;
}
}
这是我用来处理数据库而不是活动的所有类。
编辑**
这个应用程序有一个带有密码的登录,该密码是我打算用来解密的。如果忘记此密码,您可以通过电子邮件将其发送到登录电子邮件。
答案 0 :(得分:1)
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;
}
}