Skip to content

Instantly share code, notes, and snippets.

@skyisle
Created November 16, 2012 02:14
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save skyisle/4083319 to your computer and use it in GitHub Desktop.
getSecureRawKey
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment