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

byte[]crypt(byte[] text, byte[] key, int mode)
crypt
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
sr.setSeed(key);
kgen.init(256, sr);
SecretKey sk = kgen.generateKey();
SecretKeySpec k = new SecretKeySpec(sk.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(mode, k);
...
StringaesEncode(String seed, String cleartext)
aes Encode
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
byte[]encrypt(byte[] raw, byte[] clear)
encrypt
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
Stringencrypt(Context context, String text)
encrypt
byte[] message = Base64.encode(text.getBytes(), Base64.DEFAULT);
SecretKey skeySpec = generateKey(getSeed(context),
        "edu.uoc.esquelet.app".getBytes());
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(message);
return Base64.encodeToString(encrypted, Base64.DEFAULT);
Stringencrypt(Context context, String text)
encrypt
byte[] message = Base64.encode(text.getBytes(), Base64.DEFAULT);
SecretKey skeySpec = generateKey(getSeed(context),
        "edu.uoc.skeleton".getBytes());
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(message);
String result = Base64.encodeToString(encrypted, Base64.DEFAULT);
return result;
...
StringencryptedData(String userkey, String userData)
encrypted Data
String encryptedUserData = "";
try {
    String key = getAppPassCode(userkey);
    Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, aesKey);
    byte[] encrypted = cipher.doFinal(userData.getBytes());
    encryptedUserData = new String(encrypted, ENCODING);
...
StringencryptedPassword(String key, String userData)
encrypted Password
String encryptedUserData = "";
try {
    Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, aesKey);
    byte[] encrypted = cipher.doFinal(userData.getBytes());
    encryptedUserData = new String(encrypted, ENCODING);
} catch (Exception e) {
...
byte[]generateEncryptionSecret()
generate Encryption Secret
try {
    KeyGenerator generator = KeyGenerator.getInstance("AES");
    generator.init(128);
    SecretKey key = generator.generateKey();
    return key.getEncoded();
} catch (NoSuchAlgorithmException ex) {
    Log.w("keyutil", ex);
    return null;
...
Stringencode(String seed, String cleartext)
encode
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);