如何在Android API 23下生成Key和Chiper启动?

时间:2016-11-19 06:11:15

标签: android android-fingerprint-api

我正在使用App的指纹身份验证。想要支持以下API 23。为此,使用 FingerprintManagerCompat 。我不知道如何在Pre-Android API 23中生成密钥 Chiper启动

以下代码用于API 23 - 生成密钥

protected void generateKey() {
    try {
        keyStore = KeyStore.getInstance("AndroidKeyStore");
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
    } catch (NoSuchAlgorithmException |
            NoSuchProviderException e) {
        throw new RuntimeException("Failed to get KeyGenerator instance", e);
    }

    try {
        keyStore.load(null);
        keyGenerator.init(new
                KeyGenParameterSpec.Builder(KEY_NAME,
                KeyProperties.PURPOSE_ENCRYPT |
                        KeyProperties.PURPOSE_DECRYPT)
                .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                .setUserAuthenticationRequired(true)
                .setEncryptionPaddings(
                        KeyProperties.ENCRYPTION_PADDING_PKCS7)
                .build());
        keyGenerator.generateKey();
    } catch (NoSuchAlgorithmException |
            InvalidAlgorithmParameterException
            | CertificateException | IOException e) {
        throw new RuntimeException(e);
    }
}

以下代码用于API 23 - Chiper启动

public boolean cipherInit() {
    try {
        cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
    } catch (NoSuchAlgorithmException |
            NoSuchPaddingException e) {
        throw new RuntimeException("Failed to get Cipher", e);
    }

    try {
        keyStore.load(null);
        SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME, null);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return true;
    } catch (KeyStoreException | CertificateException
            | UnrecoverableKeyException | IOException
            | NoSuchAlgorithmException | InvalidKeyException e) {
        throw new RuntimeException("Failed to init Cipher", e);
    }
}

我不知道如何在Pre API 23中启动这两件事来访问 FingerprintManagerCompat ,帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

在棉花糖前设备中不提供FingerprintManager。他们在Marshamallow中添加了此API,该API指定为here

答案 1 :(得分:0)

不可以。您无法在API 23(Marshmallow 6.0)下生成密钥和密码。

  

有些Android设备在API 21下面有指纹传感器,但是android只支持API 23及以上版本。你必须使用他们的sdk进行指纹认证.Samsung提供自己的sdk用于指纹认证,即Pass Sdk。

您可以看到此link。指纹身份验证的示例项目here