Source:
final synchronized private SecretKey getSecretKeyFromAndroidKeyStore() throws IOException, GeneralSecurityException { if (sSecretKeyFromAndroidKeyStore != null) { return sSecretKeyFromAndroidKeyStore; } File keyFile = new File(mContext.getDir(mContext.getPackageName(), Context.MODE_PRIVATE), ADALKS); if (mKeyPair == null) { mKeyPair = getKeyPairFromAndroidKeyStore(); Logger.v(TAG, "Retrived keypair from androidKeyStore"); } Cipher wrapCipher = Cipher.getInstance(WRAP_ALGORITHM); if (!keyFile.exists()) { Logger.v(TAG, "Key file does not exists"); final SecretKey key = generateSecretKey(); Logger.v(TAG, "Wrapping SecretKey"); final byte[] keyWrapped = wrap(wrapCipher, key); Logger.v(TAG, "Writing SecretKey"); writeKeyData(keyFile, keyWrapped); Logger.v(TAG, "Finished writing SecretKey"); } Logger.v(TAG, "Reading SecretKey"); try { final byte[] encryptedKey = readKeyData(keyFile); if (encryptedKey == null || encryptedKey.length == 0) { throw new UnrecoverableKeyException("Couldn't find encrypted key in file"); } if (mKeyPair.getPrivate() == null || mKeyPair.getPrivate().getEncoded().length == 0) { throw new UnrecoverableKeyException("Retrieved private key is empty."); } sSecretKeyFromAndroidKeyStore = unwrap(wrapCipher, encryptedKey); Logger.v(TAG, "Finished reading SecretKey"); } catch (GeneralSecurityException | IOException ex) { Logger.e(TAG, "Unwrap failed for AndroidKeyStore", "", ADALError.ANDROIDKEYSTORE_FAILED, ex); mKeyPair = null; sSecretKeyFromAndroidKeyStore = null; deleteKeyFile(); resetKeyPairFromAndroidKeyStore(); Logger.v(TAG, "Removed previous key pair info."); throw ex; } return sSecretKeyFromAndroidKeyStore; }

Target:
final synchronized private SecretKey getSecretKeyFromAndroidKeyStore() throws IOException, GeneralSecurityException { if (sSecretKeyFromAndroidKeyStore != null) { return sSecretKeyFromAndroidKeyStore; } File keyFile = new File(mContext.getDir(mContext.getPackageName(), Context.MODE_PRIVATE), ADALKS); if (mKeyPair == null) { mKeyPair = getKeyPairFromAndroidKeyStore(); Logger.v(TAG, "Retrived keypair from androidKeyStore"); } Cipher wrapCipher = Cipher.getInstance(WRAP_ALGORITHM); if (!keyFile.exists()) { Logger.v(TAG, "Key file does not exists"); final SecretKey key = generateSecretKey(); Logger.v(TAG, "Wrapping SecretKey"); final byte[] keyWrapped = wrap(wrapCipher, key); Logger.v(TAG, "Writing SecretKey"); writeKeyData(keyFile, keyWrapped); Logger.v(TAG, "Finished writing SecretKey"); } Logger.v(TAG, "Reading SecretKey"); try { final byte[] encryptedKey = readKeyData(keyFile); if (encryptedKey == null || encryptedKey.length == 0) { throw new UnrecoverableKeyException("Couldn't find encrypted key in file"); } final PrivateKey privateKey = mKeyPair.getPrivate(); if (privateKey == null || privateKey.getEncoded() == null || privateKey.getEncoded().length == 0) { throw new UnrecoverableKeyException("Retrieved private key is empty."); } sSecretKeyFromAndroidKeyStore = unwrap(wrapCipher, encryptedKey); Logger.v(TAG, "Finished reading SecretKey"); } catch (GeneralSecurityException | IOException ex) { Logger.e(TAG, "Unwrap failed for AndroidKeyStore", "", ADALError.ANDROIDKEYSTORE_FAILED, ex); mKeyPair = null; sSecretKeyFromAndroidKeyStore = null; deleteKeyFile(); resetKeyPairFromAndroidKeyStore(); Logger.v(TAG, "Removed previous key pair info."); throw ex; } return sSecretKeyFromAndroidKeyStore; }