我从http://www.ravenblast.com/index.php/blog/android-password-text-encryption/得到了这个代码,尽管它有效,但我越来越怀疑它不够安全。根据其他来源,似乎没有任何初始化向量。
public static String encrypt(String toEncrypt, byte[ ] key) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[ ] encryptedBytes = cipher.doFinal(toEncrypt.getBytes());
String encrypted = Base64.encodeBytes(encryptedBytes);
return encrypted;
}
public static String decrypt(String encryptedText, byte[ ] key) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] toDecrypt = Base64.decode(encryptedText);
byte[] encrypted = cipher.doFinal(toDecrypt);
return new String(encrypted);
}