Android SQlite password encryption?
So I had an idea for an Android App (just for learning) since im just starting out. it would basically 开发者_JAVA技巧be an app that lets your "store/vault" your passwords you need to remember. But it would encrypt/decrypt them through SQlite (which would be the storage median). What types of encryption can Android/SQlite3 do?
I use apache commons Base64 for encoding the encrypted password. You end up storing the password in the db as a Blob.
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import org.apache.commons.net.util.Base64;
private static SecretKey key;
try {
byte[] secretBytes = "secret key".getBytes("UTF8");
DESKeySpec keySpec = new DESKeySpec(secretBytes);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
key = keyFactory.generateSecret(keySpec);
} catch (Exception e) {
Log.e(Flashum.LOG_TAG, "DatabaseHelper " + e.toString());
}
public byte[] encryptPassword(String userpw) {
try {
byte[] cleartext = userpw.getBytes("UTF8");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] clearBytes = cipher.doFinal(cleartext);
byte[] encryptedPwd = Base64.encodeBase64(clearBytes);
return encryptedPwd;
} catch (Exception e) {
Log.e(Flashum.LOG_TAG, "DatabaseHelper " + e.toString());
}
return null;
}
public String decryptPassword(byte[] userpw) {
String pw = "";
try {
byte[] encrypedPwdBytes = Base64.decodeBase64(userpw);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] plainTextPwdBytes = cipher.doFinal(encrypedPwdBytes);
pw = new String(plainTextPwdBytes, "UTF8");
} catch (Exception e) {
Log.e(Flashum.LOG_TAG, "DatabaseHelper " + e.toString());
}
return pw;
}
You probably want to use Android's javax.crypto package and then store the encrypted data in sqlite3 rows.
This provides symmetric encryption, allowing your user to enter a password which would unlock content in the database that was encrypted with that password.
精彩评论