检查用户是否在Android中更改了生物特征/指纹
我正在寻找一种方法,以获得通知,如果用户更改了他/她的指纹。 我看到了这个答案,但不清楚在这个场景中如何使用“setAllowedAuthenticators” 如果有人能帮忙,我将不胜感激 [更新]更新后的代码: 1-生成密钥检查用户是否在Android中更改了生物特征/指纹,android,android-biometric-prompt,Android,Android Biometric Prompt,我正在寻找一种方法,以获得通知,如果用户更改了他/她的指纹。 我看到了这个答案,但不清楚在这个场景中如何使用“setAllowedAuthenticators” 如果有人能帮忙,我将不胜感激 [更新]更新后的代码: 1-生成密钥 generateSecretKey(new KeyGenParameterSpec.Builder( KEY_NAME, KeyProperties.PURPOSE_ENCR
generateSecretKey(new KeyGenParameterSpec.Builder(
KEY_NAME,
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
.setUserAuthenticationRequired(true)
// Invalidate the keys if the user has registered a new biometric
// credential, such as a new fingerprint. Can call this method only
// on Android 7.0 (API level 24) or higher. The variable
.setInvalidatedByBiometricEnrollment(true)
.build());
2-生成密码
Cipher cipher = getCipher();
SecretKey secretKey = getSecretKey();
try {
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
} catch (KeyPermanentlyInvalidatedException e) {
System.out.print("key has changed");
} catch (InvalidKeyException e) {
e.printStackTrace();
}
3-验证
biometricPrompt.authenticate(new CancellationSignal(), excutor, new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result) {
}
});
}
});
}
});
错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.fingerprint_poc, PID: 9523
java.lang.IllegalArgumentException: keystoreAlias must not be empty
at android.security.keystore.KeyGenParameterSpec$Builder.<init>(KeyGenParameterSpec.java:760)
at com.example.fingerprint_poc.task$5.onClick(task.java:153)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) I/Process: Sending signal. PID: 9523 SIG: 9
E/AndroidRuntime:致命异常:主
进程:com.example.fingerprint_poc,PID:9523
java.lang.IllegalArgumentException:keystoreAlias不能为空
在android.security.keystore.KeyGenParameterSpec$Builder上。(KeyGenParameterSpec.java:760)
位于com.example.fingerprint\u poc.task$5.onClick(task.java:153)
在android.view.view.performClick上(view.java:6597)
在android.view.view.performClickInternal(view.java:6574)
在android.view.view.access$3100(view.java:778)
在android.view.view$PerformClick.run(view.java:25885)
位于android.os.Handler.handleCallback(Handler.java:873)
位于android.os.Handler.dispatchMessage(Handler.java:99)
位于android.os.Looper.loop(Looper.java:193)
位于android.app.ActivityThread.main(ActivityThread.java:6669)
位于java.lang.reflect.Method.invoke(本机方法)
位于com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)上,I/Process:发送信号。PID:9523信号:9
使用此处的函数和
在身份验证之前添加此代码
Cipher cipher = getCipher();
SecretKey secretKey = getSecretKey();
if (getSecretKey() == null){
generateSecretKey(new KeyGenParameterSpec.Builder(
KEY_NAME,
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
.setUserAuthenticationRequired(true)
// Invalidate the keys if the user has registered a new biometric
// credential, such as a new fingerprint. Can call this method only
// on Android 7.0 (API level 24) or higher. The variable
.setInvalidatedByBiometricEnrollment(true)
.build());
}
try {
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
} catch (KeyPermanentlyInvalidatedException e) {
System.out.print("key has changed");
Toast.makeText(task.this, "changed", Toast.LENGTH_LONG).show();
generateSecretKey(new KeyGenParameterSpec.Builder(
KEY_NAME,
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
.setUserAuthenticationRequired(true)
// Invalidate the keys if the user has registered a new biometric
// credential, such as a new fingerprint. Can call this method only
// on Android 7.0 (API level 24) or higher. The variable
.setInvalidatedByBiometricEnrollment(true)
.build());
} catch (InvalidKeyException e) {
e.printStackTrace();
}
我不确定
setAllowedAuthenticator
与此(?)有什么关系。当您尝试init
您的Cipher
时,将抛出KeyPermanentlyInvalidatedException
,您甚至在启动身份验证之前都会这样做。如何初始化密码?您能详细说明一下吗?这需要您在某个时间点(我们称之为A)生成一个需要用户身份验证的加密密钥(请参阅和)。如果在稍后的某个点(B)您尝试为您的密钥初始化一个密码
,如链接的示例所示,并且用户在时间a和B之间添加了新指纹,那么您应该会得到一个KeyPermanentlyInvalidatedException
。我在验证函数和我的应用缓存之前尝试过这样做。我在帖子中添加了代码。你能检查一下吗?你还需要添加来自崩溃的stacktrace。代码和stacktrace都应该添加为文本,而不是屏幕截图。