Android Utililty Methods AES Encrypt

List of utility methods to do AES Encrypt

Description

The list of methods to do AES Encrypt are organized into topic(s).

Method

Stringencrypt(String seed, String clearText)
To encrypt a string.
byte[] result = null;
try {
    byte[] rawkey = getRawKey(seed.getBytes());
    result = encrypt(rawkey, clearText.getBytes());
} catch (Exception e) {
    e.printStackTrace();
    return null;
if (result == null) {
    return null;
String content = toHex(result);
return content;
Stringencrypt(String seed, String cleartext)
encrypt
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
Stringencrypt(String src)
encrypt
try {
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, makeKey(), makeIv());
    return Base64.encodeToString(cipher.doFinal(src.getBytes()),
            Base64.NO_WRAP);
} catch (Exception e) {
    throw new RuntimeException(e);
Stringencrypt(String src)
encrypt
return encrypt(DEFAULT_KEY, src);
byte[]encrypt(String text, String key)
encrypt
byte[] keyBytes = key.getBytes("UTF-8");
byte[] textBytes = text.getBytes("UTF-8");
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = null;
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
return cipher.doFinal(textBytes);
...
byte[]encrypt(byte[] content, String key)
encrypt
try {
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec ivs = new IvParameterSpec(defaultIV);
    cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(key), ivs);
    return cipher.doFinal(content);
} catch (Exception e) {
    e.printStackTrace();
return null;
byte[]encrypt(byte[] content, String password)
encrypt
try {
    SecretKeySpec key = createKey(password);
    Cipher cipher = Cipher.getInstance(CipherMode);
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] result = cipher.doFinal(content);
    return result;
} catch (Exception e) {
    e.printStackTrace();
...
byte[]encrypt(byte[] content, String password)
encrypt
byte[] result = null;
try {
    SecretKeySpec key = new SecretKeySpec(
            getRawKey(password.getBytes()), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    result = cipher.doFinal(content);
} catch (Exception e) {
...
byte[]encrypt(byte[] data, Key key)
encrypt
return encrypt(data, key, DEFAULT_CIPHER_ALGORITHM);
byte[]encrypt(byte[] data, Key key, String cipherAlgorithm)
encrypt
Cipher cipher = Cipher.getInstance(cipherAlgorithm);
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(data);