Esempio n. 1
0
 /**
  * Derive secret key by PBKDF2 algorithm (1000 iterations of SHA256)
  *
  * @param fileKey
  * @param version
  * @return
  * @throws UnsupportedEncodingException
  * @throws NoSuchAlgorithmException
  */
 private static String deriveKey(@NonNull byte[] fileKey, int version)
     throws UnsupportedEncodingException, NoSuchAlgorithmException {
   PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA256Digest());
   gen.init(fileKey, salt, ITERATION_COUNT);
   byte[] keyBytes =
       ((KeyParameter)
               gen.generateDerivedMacParameters(
                   version == 2 ? KEY_LENGTH * 8 : KEY_LENGTH_SHORT * 8))
           .getKey();
   return toHex(keyBytes);
 }
Esempio n. 2
0
 /**
  * Derive secret key by PBKDF2 algorithm (1000 iterations of SHA256)
  *
  * @param password
  * @param version
  * @return
  * @throws UnsupportedEncodingException
  * @throws NoSuchAlgorithmException
  */
 private static byte[] deriveKey(@NonNull String password, int version)
     throws UnsupportedEncodingException, NoSuchAlgorithmException {
   PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA256Digest());
   gen.init(
       PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password.toCharArray()),
       salt,
       ITERATION_COUNT);
   return ((KeyParameter)
           gen.generateDerivedMacParameters(version == 2 ? KEY_LENGTH * 8 : KEY_LENGTH_SHORT * 8))
       .getKey();
 }
Esempio n. 3
0
 /**
  * Derive initial vector by PBKDF2 algorithm (10 iterations of SHA256)
  *
  * @param key
  * @return
  * @throws UnsupportedEncodingException
  */
 private static byte[] deriveIv(@NonNull byte[] key) throws UnsupportedEncodingException {
   PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA256Digest());
   gen.init(key, salt, 10);
   return ((KeyParameter) gen.generateDerivedMacParameters(KEY_LENGTH_SHORT * 8)).getKey();
 }