我有一个指纹身份验证的实现,一直运行正常,直到我在HTC M9 +(Android 6.0)上运行它。
我在AndroidKeyStore中生成了一个密钥:
mKeyPairGenerator.initialize(
new KeyGenParameterSpec.Builder(KEY_ALIAS, KeyProperties.PURPOSE_DECRYPT | KeyProperties.PURPOSE_ENCRYPT)
.setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP)
.setUserAuthenticationRequired(true)
.build());
mKeyPairGenerator.generateKeyPair();
我尝试使用它来初始化指纹auth调用的密码,而在运行6.0 Android的HTC上我收到错误:
Crypto primitive not backed by AndroidKeyStore provider
现在我知道API 23中存在一个错误,它不允许在没有身份验证的情况下使用公钥(即使它应该),所以我实现了代码从AndroidKeyStore项中提取密钥然后初始化它(Per suggestion)在Android文档中)。这很好用。
但我无法使用提取的公钥密码/加密对象进行指纹验证调用:
m_fingerprintManager.authenticate(m_cryptoObject, m_cancellationSignal, 0, this, null)
如果我没有提取init的公钥并直接从AndroidKeyStore使用它,我会收到一条错误,指出用户未经过身份验证。
以下是initCipher提取公钥的代码:
PublicKey key = mKeyStore.getCertificate(KEY_ALIAS).getPublicKey();
PublicKey unrestricted = KeyFactory.getInstance(key.getAlgorithm())
.generatePublic(new X509EncodedKeySpec(key.getEncoded()));
OAEPParameterSpec spec = new OAEPParameterSpec(
"SHA-256", "MGF1", MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT);
mCipher.init(opmode, unrestricted, spec);
以下是直接从密钥库中获取的代码:
PrivateKey key = (PrivateKey) mKeyStore.getKey(KEY_ALIAS, null);
mCipher.init(opmode, key);
使用解压缩的public返回AndroidKeyStore不支持的错误。
直接从商店使用密钥会返回用户未经过身份验证的错误。
那么,我该如何让它发挥作用?如上所述直接使用密钥库中的密钥在其他设备上运行良好(三星S7 Edge与Android M和N,Nexus 6P与N,三星S6与M(6.0.1),等)