Android中的指纹识别

电脑杂谈  发布时间:2020-07-02 06:02:01  来源:网络整理

安卓第一个指纹识别_华为电脑开机识别不了指纹_魅蓝5s识别指纹怎么识别不了

火影忍者

最近的项目需要使用指纹识别功能. 在查阅了相关信息之后,将其整理到本文中.

指纹识别是Android 6.0之后的一项新功能,因此在使用它时,您需要首先确定用户手机的系统版本是否支持指纹识别. 此外,在实际的开发方案中,有两种主要的使用指纹的方案:

由于指纹识别功能需要加密的对象(CryptoObject),因此通常通过对称加密或非对称加密获得该对象. 上面两个开发方案的实现是相似的,主要的区别在于加密过程中密钥的创建和使用. 一般来说,纯本地指纹识别功能只需要对称加密即可;与背景互动时需要非对称加密: 私钥用于本地指纹识别. 识别成功后,将加密的信息传递给后台,后台开发人员使用公钥解密以获取用户信息.

下面是对对称加密和非对称加密的概念的简要介绍,然后将分别说明这两种开发方法的实现.

在使用指纹识别功能之前,有必要了解对称加密和非对称加密的内容.

从上面可以看到,对称加密和非对称加密的特征如下:

使用指纹识别的对称加密功能的主要流程如下:

使用KeyGenerator创建对称密钥并将其存储在KeyStore中. 将KeyGenParameterSpec.Builder.setUserAuthenticationRequired()设置为true,使用创建的对称密钥初始化Cipher对象,然后使用该对象调用FingerprintManager.authenticate()方法以启动指纹传感器并开始. 重写FingerprintManager.AuthenticationCallback的几种回调方法,以处理指纹识别成功(onAuthenticationSucceeded()),失败(onAuthenticationFailed()和onAuthenticationError())等等.

华为电脑开机识别不了指纹_魅蓝5s识别指纹怎么识别不了_安卓第一个指纹识别

创建密钥涉及两个类: KeyStore和KeyGenerator.

KeyStore是用于存储和获取密钥的容器. 获取KeyStore的方法如下:

try {
    mKeyStore = KeyStore.getInstance("AndroidKeyStore");
} catch (KeyStoreException e) {
    throw new RuntimeException("Failed to get an instance of KeyStore", e);
}

要生成密钥,如果它是对称加密,则需要KeyGenerator类. 获取KeyGenerator对象相对简单,如下所示:

// 对称加密, 创建 KeyGenerator 对象
try {
    mKeyGenerator = KeyGenerator
            .getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
    throw new RuntimeException("Failed to get an instance of KeyGenerator", e);
}

获取KeyGenerator对象后,可以生成一个Key:

 try {
    keyStore.load(null);
    KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(defaultKeyName,
            KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
            .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
            .setUserAuthenticationRequired(true)
            .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        builder.setInvalidatedByBiometricEnrollment(true);
    }
    keyGenerator.init(builder.build());
    keyGenerator.generateKey();
} catch (CertificateException | NoSuchAlgorithmException | IOException | InvalidAlgorithmParameterException e) {
    e.printStackTrace();
}

密码对象是根据某些加密规则对数据进行加密的对象. 调用指纹识别功能需要该对象. 创建密码对象很简单,如以下代码所示:

Cipher defaultCipher;
try {
    defaultCipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
            + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
    throw new RuntimeException("创建Cipher对象失败", e);
}

然后使用刚创建的密钥初始化Cipher对象:

华为电脑开机识别不了指纹_魅蓝5s识别指纹怎么识别不了_安卓第一个指纹识别

 try {
    keyStore.load(null);
    SecretKey key = (SecretKey) keyStore.getKey(keyName, null);
    cipher.init(Cipher.ENCRYPT_MODE, key);
    return true;
} catch (IOException | NoSuchAlgorithmException | CertificateException | UnrecoverableKeyException | KeyStoreException | InvalidKeyException e) {
    throw new RuntimeException("初始化 cipher 失败", e);
}

当您真正使用指纹识别功能时安卓第一个指纹识别,您会发现它实际上非常简单,只需调用FingerprintManager类的authenticate()方法,然后系统就会向我们提供相应的回调反馈,该方法是如下:

public void authenticate(CryptoObject crypto, CancellationSignal cancel, int flags, AuthenticationCallback callback, Handler handler) 

该方法的几个参数解释如下:

完成指纹识别后,请记住关闭AuthenticationCallback:

public void stopListening() {
    if (cancellationSignal != null) {
        selfCancelled = true;
        cancellationSignal.cancel();
        cancellationSignal = null;
    }
}

在调用authenticate()方法之后,系统将启动指纹传感器并开始扫描. 此时,根据扫描结果,将通过FingerprintManager.AuthenticationCallback类返回几种回调方法:

// 成功
onAuthenticationSucceeded()
// 失败
onAuthenticationFaile()
// 错误
onAuthenticationError()

通常,我们需要重写这些方法才能实现我们的功能. onAuthenticationFaile()和onAuthenticationError()之间的区别将在后面讨论.

实际上,该过程类似于以上过程:

安卓第一个指纹识别_魅蓝5s识别指纹怎么识别不了_华为电脑开机识别不了指纹

使用KeyPairGenerator创建一个非对称密钥. 使用创建的私钥进行签名,使用签名创建加密的对象,然后将该对象用作FingerprintManager.authenticate()方法的参数以启动指纹传感器并开始. 重写FingerprintManager.AuthenticationCallback类的几种回调方法,以处理指纹识别成功(onAuthenticationSucceeded()),失败(onAuthenticationFailed()和onAuthenticationError())等等.

可以看出,非对称加密方法和指纹识别的对称加密方法的实现过程是相似的,两者之间最明显的区别是密钥的生成和使用.

在这里,我们要使用KeyPairGenerator创建一组非对称密钥. 第一种是获取KeyPairGenerator对象:

// 非对称加密,创建 KeyPairGenerator 对象
try {
    mKeyPairGenerator =  KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore");
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
    throw new RuntimeException("Failed to get an instance of KeyPairGenerator", e);
}

获取KeyPairGenerator对象后,您可以创建一个KeyPair(密钥对):

try {
    // Set the alias of the entry in Android KeyStore where the key will appear
    // and the constrains (purposes) in the constructor of the Builder
    mKeyPairGenerator.initialize(
            new KeyGenParameterSpec.Builder(KEY_NAME,
                    KeyProperties.PURPOSE_SIGN)
                    .setDigests(KeyProperties.DIGEST_SHA256)
                    .setAlgorithmParameterSpec(new ECGenParameterSpec("secp256r1"))
                    // Require the user to authenticate with a fingerprint to authorize
                    // every use of the private key
                    .setUserAuthenticationRequired(true)
                    .build());
    mKeyPairGenerator.generateKeyPair();
} catch (InvalidAlgorithmParameterException e) {
    throw new RuntimeException(e);
}


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/shouji/article-265423-1.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...