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 cleartext)
encrypt
byte[] rawKey = getRawKey(theRawKey.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
String fromHex = toHex(result);
String base64 = new String(Base64.encodeToString(
        fromHex.getBytes(), 0));
return base64;
Stringencrypt(String content, String password)
encrypt
byte[] data = null;
try {
    data = content.getBytes("UTF-8");
} catch (Exception e) {
    e.printStackTrace();
data = encrypt(data, password);
String result = byte2hex(data);
...
byte[]encrypt(String content, String password)
encrypt
try {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128, new SecureRandom(password.getBytes()));
    SecretKey secretKey = kgen.generateKey();
    byte[] enCodeFormat = secretKey.getEncoded();
    SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    byte[] byteContent = content.getBytes("utf-8");
...
byte[]encrypt(String content, String password)
encrypt
try {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128, new SecureRandom(password.getBytes()));
    SecretKey secretKey = kgen.generateKey();
    byte[] enCodeFormat = secretKey.getEncoded();
    SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    byte[] byteContent = content.getBytes("utf-8");
...
byte[]encrypt(String content, String password)
encrypt
try {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128, new SecureRandom(password.getBytes()));
    SecretKey secretKey = kgen.generateKey();
    byte[] enCodeFormat = secretKey.getEncoded();
    SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    byte[] byteContent = content.getBytes("utf-8");
...
Stringencrypt(String data)
encrypt
try {
    SecretKeySpec sks = null;
    byte[] encodedBytes = null;
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    sr.setSeed(seed.getBytes());
    KeyGenerator kg = KeyGenerator.getInstance("AES");
    kg.init(128, sr);
    sks = new SecretKeySpec((kg.generateKey()).getEncoded(), "AES");
...
Stringencrypt(String dataPassword, String cleartext)
encrypt
IvParameterSpec zeroIv = new IvParameterSpec(ENCRYPT_IV);
SecretKeySpec key = new SecretKeySpec(dataPassword.getBytes(),
        "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
byte[] encryptedData = cipher.doFinal(cleartext.getBytes(CHARSET));
return new String(Base64.encodeBase64(encryptedData));
Stringencrypt(String key, String src)
encrypt
byte[] keyByte = key.getBytes();
SecretKeySpec keySpec = new SecretKeySpec(keyByte, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
byte[] srcByte = src.getBytes();
byte[] encrypted = cipher.doFinal(srcByte);
String result = Base64.encodeToString(encrypted, Base64.DEFAULT);
...
Stringencrypt(String key, String src)
encrypt
byte[] rawKey = getRawKey(key.getBytes());
byte[] result = encrypt(rawKey, src.getBytes());
return toHex(result);
Stringencrypt(String seed, String clearText)
encrypt
byte[] result = null;
try {
    byte[] rawkey = getRawKey(seed.getBytes());
    result = encrypt(rawkey, clearText.getBytes());
} catch (Exception e) {
    e.printStackTrace();
String content = toHex(result);
...