Example usage for android.security.keystore KeyProperties BLOCK_MODE_CBC

List of usage examples for android.security.keystore KeyProperties BLOCK_MODE_CBC

Introduction

In this page you can find the example usage for android.security.keystore KeyProperties BLOCK_MODE_CBC.

Prototype

String BLOCK_MODE_CBC

To view the source code for android.security.keystore KeyProperties BLOCK_MODE_CBC.

Click Source Link

Document

Cipher Block Chaining (CBC) block mode.

Usage

From source file:com.rnd.snapsplit.view.OwedFragment.java

public void createKey(String keyName, boolean invalidatedByBiometricEnrollment) {
    // The enrolling flow for fingerprint. This is where you ask the user to set up fingerprint
    // for your flow. Use of keys is necessary if you need to know if the set of
    // enrolled fingerprints has changed.
    try {/*from w ww .  j a  va  2  s  . com*/
        mKeyStore.load(null);
        // Set the alias of the entry in Android KeyStore where the key will appear
        // and the constrains (purposes) in the constructor of the Builder

        KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(keyName,
                KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                        .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                        // Require the user to authenticate with a fingerprint to authorize every use
                        // of the key
                        .setUserAuthenticationRequired(true)
                        .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7);

        // This is a workaround to avoid crashes on devices whose API level is < 24
        // because KeyGenParameterSpec.Builder#setInvalidatedByBiometricEnrollment is only
        // visible on API level +24.
        // Ideally there should be a compat library for KeyGenParameterSpec.Builder but
        // which isn't available yet.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            builder.setInvalidatedByBiometricEnrollment(invalidatedByBiometricEnrollment);
        }
        mKeyGenerator.init(builder.build());
        mKeyGenerator.generateKey();
    } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | CertificateException
            | IOException e) {
        throw new RuntimeException(e);
    }
}