在FingerprintManager.authenticate()中的android – UserNotAuthenticatedException

在FingerprintManager.authenticate()中的android – UserNotAuthenticatedException,第1张

概述我有一个加密密码存储在 Android KeyStore. 我想通过使用指纹API验证用户来解密该密码. 据我所知,我必须调用FingerprintManager.authenticate(CryptoObject cryptoObject)方法来开始侦听指纹结果. CryptoObject参数是这样创建的: public static Cipher getDecryptionCipher(Con 我有一个加密密码存储在 Android KeyStore.

我想通过使用指纹API验证用户来解密该密码.

据我所知,我必须调用FingerprintManager.authenticate(CryptoObject cryptoObject)方法来开始侦听指纹结果. CryptoObject参数是这样创建的:

public static Cipher getDecryptionCipher(Context context) throws KeyStoreException {    try {        Cipher cipher = Cipher.getInstance(transformATION);        SecretKey secretKey = getKeyFromKeyStore();        final IvParameterSpec ivParameterSpec = getIvParameterSpec(context);        cipher.init(Cipher.DECRYPT_MODE,secretKey,ivParameterSpec);        return cipher;    } catch (NoSuchAlgorithmException | NoSuchpaddingException | IOException | UnrecoverableKeyException | CertificateException | InvalIDAlgorithmParameterException | InvalIDKeyException e) {        e.printstacktrace();    }    return null;}Cipher cipher = FingerprintCryptoHelper.getDecryptionCipher(getContext());FingerprintManager.CryptoObject cryptoObject = new FingerprintManager.CryptoObject(cipher);fingerprintManager.authenticate(cryptoObject,...);

getDecryptionCipher()方法正常工作,直到cipher.init()调用.在这个调用上,我得到一个UserNotAuthenticatedException,因为用户没有为这个secretKey进行身份验证.这是有道理的.但这不是一个循环,不可能实现:

要认证用户,我想使用他/她的指纹
>要听他/她的指纹,我需要初始化Cipher,这个返回需要一个经过身份验证的用户

这里有什么问题?

编辑:

我使用模拟器(Nexus 4,API 23).

这是我用来创建密钥的代码.

private SecretKey createKey() {    try {        KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyPropertIEs.KEY_ALGORITHM_AES,ANDROID_KEY_STORE);        keyGenerator.init(new KeyGenParameterSpec.Builder(                KEY_name,KeyPropertIEs.PURPOSE_ENCRYPT | KeyPropertIEs.PURPOSE_DECRYPT        )                .setBlockModes(KeyPropertIEs.BLOCK_MODE_CBC)                .setUserAuthenticationrequired(true)                .setUserAuthenticationValIDityDurationSeconds(AUTHENTICATION_DURATION_SECONDS)                .setEncryptionpaddings(KeyPropertIEs.ENCRYPTION_padding_PKCS7)                .build());        return keyGenerator.generateKey();    } catch (NoSuchAlgorithmException | NoSuchProvIDerException | InvalIDAlgorithmParameterException e) {        throw new RuntimeException("Failed to create a symmetric key",e);    }}
解决方法 事实证明,这个问题与KeyGenParameterSpec的一个已知问题有关,防止在没有认证的情况下使用公共密钥(这正是公钥不需要的).

一个相关的问题/答案可以在这里找到:Android Fingerprint API Encryption and Decryption

解决方法是从最初创建的密钥创建一个PublicKey,并使用这个无限制的PublicKey来初始化密码.
所以我的最终密码使用AES / CBC / PKCS7padding,并通过这种方法初始化:

public boolean initCipher(int opMode) {    try {        Key key = mKeyStore.getKey(KEY_name,null);        if (opMode == Cipher.ENCRYPT_MODE) {            final byte[] encoded = key.getEncoded();            final String algorithm = key.getAlgorithm();            final X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encoded);            PublicKey unrestricted = KeyFactory.getInstance(algorithm).generatePublic(keySpec);            mCipher.init(opMode,unrestricted);        } else {            final IvParameterSpec ivParameterSpec = getIvParameterSpec();            mCipher.init(opMode,key,ivParameterSpec);        }        return true;    } catch (KeyPermanentlyInvalIDatedException exception) {        return false;    } catch ( NoSuchAlgorithmException | InvalIDKeyException            | InvalIDKeySpecException | InvalIDAlgorithmParameterException | UnrecoverableKeyException | KeyStoreException exception) {        throw new RuntimeException("Failed to initialize Cipher or Key: ",exception);    }}@NonNullpublic IvParameterSpec getIvParameterSpec() {    // the IV is stored in the Preferences after enCoding.    String base64EncryptionIv = PreferenceHelper.getEncryptionIv(mContext);    byte[] encryptionIv = Base64.decode(base64EncryptionIv,Base64.DEFAulT);    return new IvParameterSpec(encryptionIv);}
总结

以上是内存溢出为你收集整理的在FingerprintManager.authenticate()中的android – UserNotAuthenticatedException全部内容,希望文章能够帮你解决在FingerprintManager.authenticate()中的android – UserNotAuthenticatedException所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1133999.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存