Example usage for android.security.keystore KeyProperties PURPOSE_ENCRYPT

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

Introduction

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

Prototype

int PURPOSE_ENCRYPT

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

Click Source Link

Document

Purpose of key: encryption.

Usage

From source file:de.niklasmerz.cordova.fingerprint.Fingerprint.java

/**
 * Creates a symmetric key in the Android Key Store which can only be used after the user has
 * authenticated with fingerprint./*from w w  w . j a  v a 2 s .c om*/
 */
public static boolean createKey() {
    String errorMessage = "";
    String createKeyExceptionErrorPrefix = "Failed to create key: ";
    boolean isKeyCreated = false;
    // 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 {
        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
        mKeyGenerator.init(new KeyGenParameterSpec.Builder(mClientId,
                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).build());
        mKeyGenerator.generateKey();
        isKeyCreated = true;
    } catch (NoSuchAlgorithmException e) {
        errorMessage = createKeyExceptionErrorPrefix + "NoSuchAlgorithmException: " + e.toString();
        ;
    } catch (InvalidAlgorithmParameterException e) {
        errorMessage = createKeyExceptionErrorPrefix + "InvalidAlgorithmParameterException: " + e.toString();
        ;
    } catch (CertificateException e) {
        errorMessage = createKeyExceptionErrorPrefix + "CertificateException: " + e.toString();
        ;
    } catch (IOException e) {
        errorMessage = createKeyExceptionErrorPrefix + "IOException: " + e.toString();
        ;
    }
    if (!isKeyCreated) {
        Log.e(TAG, errorMessage);
        setPluginResultError(errorMessage);
    }
    return isKeyCreated;
}

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 w  w  .  ja  v a 2  s . c o m*/
        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);
    }
}