public
Last active

getSecureRawKey

  • Download Gist
gistfile1.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
static byte[] getSecureRawKey(String seed) {
/*
* check http://www.motorola.com/sites/motodev/library/using_aes_in_android.html
* for detail information
*/
char[] humanPassphrase = seed.toCharArray();
byte[] salt = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF
}; // must save this!
final int HASH_ITERATIONS = 10000;
final int KEY_LENGTH = 128;
PBEKeySpec mykeyspec = new PBEKeySpec(humanPassphrase, salt, HASH_ITERATIONS, KEY_LENGTH);
 
SecretKeyFactory keyfactory;
try {
keyfactory = SecretKeyFactory.getInstance("PBEWITHSHAANDTWOFISH-CBC");
SecretKey sk = keyfactory.generateSecret(mykeyspec);
byte[] skAsByteArray = sk.getEncoded();
SecretKey skforAES = new SecretKeySpec(skAsByteArray, "AES");
byte[] raw = skforAES.getEncoded();
return raw;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
 
return null;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.