Created
November 16, 2012 02:14
-
-
Save skyisle/4083319 to your computer and use it in GitHub Desktop.
getSecureRawKey
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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