@Test public void generateAESKeyFromPasswordTest() { for (AES_KEYLENGTH keyLength : AES_KEYLENGTH.values()) { for (int i = 0; i < 3; i++) { String randomPW = randomString(20); String randomPIN = randomString(6); logger.debug("Testing {}-bit AES key generation from user password and PIN:", keyLength.value()); logger.debug("Random PW: {}.", randomPW); logger.debug("Random PIN: {}.", randomPIN); SecretKey[] aesKey = new SecretKey[3]; for (int j = 0; j < aesKey.length; j++) { aesKey[j] = PasswordUtil.generateAESKeyFromPassword(randomPW, randomPIN, keyLength); assertNotNull(aesKey[j]); assertNotNull(aesKey[j].getEncoded()); assertTrue(aesKey[j].getEncoded().length == keyLength.value() / 8); logger.debug("Generated {}-bit AES key: {}.", keyLength.value(), EncryptionUtil.byteToHex(aesKey[j].getEncoded())); for (int k = 0; k < j; k++) { assertTrue(Arrays.equals(aesKey[k].getEncoded(), aesKey[j].getEncoded())); } } } } }
public static SecretKey generateAESKeyFromPassword(String password, String pin, AES_KEYLENGTH keyLength) { byte[] pinEnlargementSalt = generateFixedSalt(pin.getBytes()); byte[] enlargedPin = getPBKDF2Hash(pin.toCharArray(), pinEnlargementSalt, SALT_BIT_SIZE); byte[] secretKeyEncoded = getPBKDF2Hash(password.toCharArray(), enlargedPin, keyLength.value()); return new SecretKeySpec(secretKeyEncoded, "AES"); }
PasswordUtil { public static SecretKey generateAESKeyFromPassword(String password, String pin, AES_KEYLENGTH keyLength) { byte[] pinEnlargementSalt = generateFixedSalt(pin.getBytes()); byte[] enlargedPin = getPBKDF2Hash(pin.toCharArray(), pinEnlargementSalt, SALT_BIT_SIZE); byte[] secretKeyEncoded = getPBKDF2Hash(password.toCharArray(), enlargedPin, keyLength.value()); return new SecretKeySpec(secretKeyEncoded, "AES"); } }
PasswordUtil { public static SecretKey generateAESKeyFromPassword(String password, String pin, AES_KEYLENGTH keyLength) { byte[] pinEnlargementSalt = generateFixedSalt(pin.getBytes()); byte[] enlargedPin = getPBKDF2Hash(pin.toCharArray(), pinEnlargementSalt, SALT_BIT_SIZE); byte[] secretKeyEncoded = getPBKDF2Hash(password.toCharArray(), enlargedPin, keyLength.value()); return new SecretKeySpec(secretKeyEncoded, "AES"); } private  PasswordUtil(); }
PasswordUtil { public static SecretKey generateAESKeyFromPassword(String password, String pin, AES_KEYLENGTH keyLength) { byte[] pinEnlargementSalt = generateFixedSalt(pin.getBytes()); byte[] enlargedPin = getPBKDF2Hash(pin.toCharArray(), pinEnlargementSalt, SALT_BIT_SIZE); byte[] secretKeyEncoded = getPBKDF2Hash(password.toCharArray(), enlargedPin, keyLength.value()); return new SecretKeySpec(secretKeyEncoded, "AES"); } private  PasswordUtil(); static byte[] generateRandomSalt(); static byte[] generateFixedSalt(byte[] input); static byte[] generateHash(char[] password, byte[] salt); static SecretKey generateAESKeyFromPassword(String password, String pin, AES_KEYLENGTH keyLength); static boolean validatePassword(char[] password, byte[] salt, byte[] correctHash); }
PasswordUtil { public static SecretKey generateAESKeyFromPassword(String password, String pin, AES_KEYLENGTH keyLength) { byte[] pinEnlargementSalt = generateFixedSalt(pin.getBytes()); byte[] enlargedPin = getPBKDF2Hash(pin.toCharArray(), pinEnlargementSalt, SALT_BIT_SIZE); byte[] secretKeyEncoded = getPBKDF2Hash(password.toCharArray(), enlargedPin, keyLength.value()); return new SecretKeySpec(secretKeyEncoded, "AES"); } private  PasswordUtil(); static byte[] generateRandomSalt(); static byte[] generateFixedSalt(byte[] input); static byte[] generateHash(char[] password, byte[] salt); static SecretKey generateAESKeyFromPassword(String password, String pin, AES_KEYLENGTH keyLength); static boolean validatePassword(char[] password, byte[] salt, byte[] correctHash); static final int HASH_BIT_SIZE; static final int SALT_BIT_SIZE; }