title: UserNotAuthenticatedException during FingerprintManager.authenticate()
body: <p>I have an encrypted password stored in the Android KeyStore.</p>
  
  <p>I want to decrypt that password by authenticating the user using the fingerprint API.</p>
  
  <p>As far as I understand, I have to call the <code>FingerprintManager.authenticate(CryptoObject cryptoObject)</code> method to start listening for the fingerprint result. The CryptoObject parameter is created like this:</p>
  
  <pre><code>public static Cipher getDecryptionCipher(Context context) throws KeyStoreException {
      try {
          Cipher cipher = Cipher.getInstance(TRANSFORMATION);
          SecretKey secretKey = getKeyFromKeyStore();
          final IvParameterSpec ivParameterSpec = getIvParameterSpec(context);
  
          cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
          return cipher;
  
      } catch (NoSuchAlgorithmException | NoSuchPaddingException | IOException | UnrecoverableKeyException | CertificateException | InvalidAlgorithmParameterException | InvalidKeyException e) {
          e.printStackTrace();
  
      }
  
      return null;
  }
  
  Cipher cipher = FingerprintCryptoHelper.getDecryptionCipher(getContext());
  FingerprintManager.CryptoObject cryptoObject = new FingerprintManager.CryptoObject(cipher);
  fingerprintManager.authenticate(cryptoObject, ...);
  </code></pre>
  
  <p>The method <code>getDecryptionCipher()</code> works correctly until the <code>cipher.init()</code>call. On this call I get an <code>UserNotAuthenticatedException</code>, because the user is not authenticated for this secretKey. Which makes sense somehow. But isn't this a loop, impossible to fulfill:</p>
  
  <ul>
  <li><strong>To authenticate the user, I want to use his/her fingerprint</strong></li>
  <li><strong>To listen for his/her fingerprint, I need to init the Cipher, which in return needs an authenticated user</strong></li>
  </ul>
  
  <p>What's wrong here??</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I work with the emulator (Nexus 4, API 23).</p>
  
  <p>Here's the code I use to create the key.</p>
  
  <pre><code>private SecretKey createKey() {
      try {
          KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEY_STORE);
          keyGenerator.init(new KeyGenParameterSpec.Builder(
                  KEY_NAME,
                  KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT
          )
                  .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                  .setUserAuthenticationRequired(true)
                  .setUserAuthenticationValidityDurationSeconds(AUTHENTICATION_DURATION_SECONDS)
                  .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
                  .build());
          return keyGenerator.generateKey();
      } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) {
          throw new RuntimeException("Failed to create a symmetric key", e);
      }
  }
  </code></pre>