Skip to content

Instantly share code, notes, and snippets.

@flschweiger
Created October 29, 2016 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flschweiger/fc0718bc2463a58f7a4fdad9d2354a1b to your computer and use it in GitHub Desktop.
Save flschweiger/fc0718bc2463a58f7a4fdad9d2354a1b to your computer and use it in GitHub Desktop.
Decrypt super secret message with authentication.
try {
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
SecretKey secretKey =
(SecretKey) keyStore.getKey(SecurityConstants.KEY_AES, null);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
// The next line may throw a UserNotAuthenticatedException and
// we need to prompt the user to authenticate again
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
byte[] decryptedBytes = cipher.doFinal(cipherBytes);
} catch (UserNotAuthenticatedException e) {
// User is not authenticated, let's authenticate with
// device credentials!
showAuthenticationScreen();
return;
} catch { … }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment