Android Utililty Methods AES Key Get

List of utility methods to do AES Key Get

Description

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

Method

SecretKeySpeccreateKey(String password)
create Key
byte[] data = null;
if (password == null) {
    password = "";
StringBuffer sb = new StringBuffer(32);
sb.append(password);
while (sb.length() < 32) {
    sb.append("0");
...
byte[]getKeyBytes(String key)
convert key from string to 256-bits (32-bytes) bytes
MessageDigest md5 = MessageDigest.getInstance("md5");
byte[] k1 = md5.digest(key.substring(0, key.length() / 2)
        .getBytes());
byte[] k2 = md5.digest(key.substring(key.length() / 2).getBytes());
byte[] k = new byte[32];
for (int i = 0; i < 16; i++) {
    k[i] = k1[i];
    k[16 + i] = k2[i];
...
byte[]getRawKey(String key)
get Raw Key
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(key.getBytes());
kgen.init(256, sr);
SecretKey skey = kgen.generateKey();
return skey.getEncoded();
byte[]getRawKey(byte[] seed)
get Raw Key
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(128, sr); 
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
byte[]getRawKey(byte[] seed)
get Raw Key
KeyGenerator kgen = KeyGenerator.getInstance("AES"); 
SecureRandom sr = null;
if (android.os.Build.VERSION.SDK_INT >= JELLY_BEAN_4_2) {
    sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
} else {
    sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
...
byte[]getRawKey(byte[] seed)
get Raw Key
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
sr.setSeed(seed);
kgen.init(128, sr);
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
byte[]getRawKey(byte[] seed)
get Raw Key
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = null;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
} else if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
} else {
    sr = SecureRandom.getInstance("SHA1PRNG");
...
byte[]getRawKey(byte[] seed)
get Raw Key
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(128, sr);
SecretKey sKey = kgen.generateKey();
byte[] raw = sKey.getEncoded();
return raw;
byte[]getRawKey(byte[] seed)
To get a raw key.
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
sr.setSeed(seed);
kgen.init(128, sr);
SecretKey sKey = kgen.generateKey();
byte[] raw = sKey.getEncoded();
return raw;
SecretKeySpecgetSecretKey(String key)
get Secret Key
MessageDigest digest;
try {
    digest = MessageDigest.getInstance("md5");
    SecretKeySpec keySpec = new SecretKeySpec(digest.digest(key
            .getBytes(UTF_8)), "AES");
    return keySpec;
} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
...