Android Utililty Methods Key Create

List of utility methods to do Key Create

Description

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

Method

KeygenerateKey(String key)
generate Key
try {
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
    return keySpec;
} catch (Exception e) {
    e.printStackTrace();
    throw e;
byte[]generateKey(byte[] randomSeed)
generate Key
KeyGenerator gen = KeyGenerator.getInstance(CIPHER_ALGORITHM);
SecureRandom rand = SecureRandom.getInstance(RANDOM_GENERATOR_TYPE);
rand.setSeed(randomSeed);
gen.init(RANDOM_KEY_SIZE, rand);
SecretKey myKey = gen.generateKey();
return myKey.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 = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(128, sr); 
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
StringcreateRandomKey(int size)
create Random Key
char[] c = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < size; i++) {
    sb.append(c[Math.abs(random.nextInt()) % c.length]);
return sb.toString();
byte[]generateKey(byte[] seed)
generate Key
KeyGenerator keyGenerator = KeyGenerator
        .getInstance(CIPHER_ALGORITHM);
SecureRandom secureRandom = SecureRandom
        .getInstance(RANDOM_GENERATOR_ALGORITHM);
secureRandom.setSeed(seed);
keyGenerator.init(RANDOM_KEY_SIZE, secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
return secretKey.getEncoded();
...
SecretKeygenerateKey(char[] passphraseOrPin, byte[] salt)
generate Key
final int iterations = 1000;
final int outputKeyLength = 128;
SecretKeyFactory secretKeyFactory = SecretKeyFactory
        .getInstance("PBKDF2WithHmacSHA1");
KeySpec keySpec = new PBEKeySpec(passphraseOrPin, salt, iterations,
        outputKeyLength);
return secretKeyFactory.generateSecret(keySpec);
SecretKeygenerateKey(char[] passphraseOrPin, byte[] salt)
generate Key
final int iterations = 1000;
final int outputKeyLength = 128;
SecretKeyFactory secretKeyFactory = SecretKeyFactory
        .getInstance("PBKDF2WithHmacSHA1");
KeySpec keySpec = new PBEKeySpec(passphraseOrPin, salt, iterations,
        outputKeyLength);
SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
return secretKey;
...
byte[]generateMacSecret()
generate Mac Secret
try {
    KeyGenerator generator = KeyGenerator.getInstance("HmacSHA1");
    return generator.generateKey().getEncoded();
} catch (NoSuchAlgorithmException e) {
    Log.w("keyutil", e);
    return null;
byte[]getKey(String keyRule)
get Key
Key key = null;
byte[] keyByte = keyRule.getBytes();
byte[] byteTemp = new byte[8];
for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {
    byteTemp[i] = keyByte[i];
key = new SecretKeySpec(byteTemp, "DES");
return key.getEncoded();
...