gpt4 book ai didi

java - BiometricPrompt.AuthenticationResult 中的 CryptoObject 始终为 null

转载 作者:行者123 更新时间:2023-11-30 05:49:27 25 4
gpt4 key购买 nike

我正在使用生物识别身份验证对话框,但我的 cryptoObject 始终为空。我有一个 fragment ,但我也直接从 Activity 中尝试过。

这是我的代码,

private Handler biometricPromptHandler = new Handler();

private Executor executor = command -> biometricPromptHandler.post(command);
private void showBiometricPrompt( String title, String description,
BiometricsCompatCallback compatCallback) {

BiometricPrompt.PromptInfo promptInfo =
new BiometricPrompt.PromptInfo.Builder()
.setTitle(title)
.setSubtitle(description)
.setNegativeButtonText("Cancel")
.build();

BiometricPrompt biometricPrompt = new BiometricPrompt((FragmentActivity) context,
executor, new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode,
@NonNull CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
compatCallback.onAuthenticationError(errorCode, errString);
Log.d("onAuthenticationError", ": ");
}

@Override
public void onAuthenticationSucceeded(
@NonNull BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
Log.d("result", ": "+(result.getCryptoObject()));
BiometricPrompt.CryptoObject authenticatedCryptoObject =
result.getCryptoObject();
Log.d("onAuthentionSucceeded", ": "+(authenticatedCryptoObject==null));
if (authenticatedCryptoObject != null) {
cipher = authenticatedCryptoObject.getCipher();
Log.d("onAuthentionSucceeded", ": ");
compatCallback.onAuthenticationSuccessful(cipher);
}else {
Log.d("cipher", "onAuthenticationSucceeded: ");
}

}

@Override
public void onAuthenticationFailed() {
Log.d("onAuthenticationFailed", ": ");
super.onAuthenticationFailed();
compatCallback.onAuthenticationFailed();
}
});


biometricPrompt.authenticate(promptInfo);



}

有人知道我做错了什么吗?

最佳答案

您需要在authenticate 方法的第二个参数中提供一个Cipher,否则它将始终返回null

使用

authenticate(PromptInfo info, CryptoObject crypto)

代替

authenticate(PromptInfo info)

查看this this repository 中的教程和示例项目.你可以使用这样的东西:

BiometricPrompt.CryptoObject cryptoObject = new BiometricPrompt.CryptoObject(getEncryptCipher(createKey()));
biometricPrompt.authenticate(promptInfo, cryptoObject);

使用(取决于你想要的加密):

private SecretKey createKey() throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
String algorithm = KeyProperties.KEY_ALGORITHM_AES;
String provider = "AndroidKeyStore";
KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm, provider);
KeyGenParameterSpec keyGenParameterSpec = new KeyGenParameterSpec.Builder("MY_KEY", KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
.setUserAuthenticationRequired(true)
.build();

keyGenerator.init(keyGenParameterSpec);
return keyGenerator.generateKey();
}

private Cipher getEncryptCipher(Key key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException {
String algorithm = KeyProperties.KEY_ALGORITHM_AES;
String blockMode = KeyProperties.BLOCK_MODE_CBC;
String padding = KeyProperties.ENCRYPTION_PADDING_PKCS7;
Cipher cipher = Cipher.getInstance(algorithm+"/"+blockMode+"/"+padding);
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher;
}

关于java - BiometricPrompt.AuthenticationResult 中的 CryptoObject 始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59040132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com