gpt4 book ai didi

android - 如何只要求用户身份验证用于解密而不用于加密

转载 作者:行者123 更新时间:2023-11-29 23:05:57 26 4
gpt4 key购买 nike

我在 AndroidKeyStore 中有一个公钥/私钥对,我生成如下:

val spec = KeyGenParameterSpec.Builder(alias(username), KeyProperties.PURPOSE_DECRYPT or KeyProperties.PURPOSE_ENCRYPT)
.setKeySize(keySize)
.setUserAuthenticationRequired(true)
.setBlockModes(ablockMode)
.setEncryptionPaddings(apaddingMode)
.setCertificateSubject(X500Principal("CN=Itsami Mario, OU=Adventure Unit, O=Plumber Bros, C=US"))
.setKeyValidityStart(Date())
.setKeyValidityEnd(Date(Date().time + 1000 * 60 * 60 * 24 * 7))
.setCertificateSerialNumber(BigInteger(64, SecureRandom()))
.setDigests(digest)
.build()

keyPairGen.initialize(spec)
return keyPairGen.genKeyPair()

我想在每次使用私钥时都要求进行生物识别身份验证,但我不想在使用公钥加密时要求生物识别提示。但是,当我在 KeyGeneratior 中使用 setUserAuthenticationRequired(true) 然后我尝试加密而不先显示 BiometricPrompt 时,我得到一个 android.security.KeyStoreException 消息: 关键用户未通过身份验证

如何要求对解密加密而不是加密加密进行身份验证?

最佳答案

您必须在运行 Android 6 的设备 Marshmallow 上进行测试。这是 known issue在该版本中,已在 Android 7 中修复。

要解决此问题,您可以提取公钥的编码并从中创建一个新的 PublicKey 对象,如下所示:

PublicKey publicKey = keyPair.getPublicKey();
PublicKey unrestrictedPublicKey =
KeyFactory.getInstance(publicKey.getAlgorithm()).generatePublic(
new X509EncodedKeySpec(publicKey.getEncoded()));

这适用于所有版本。

请注意,还可以创建在解密时需要身份验证但在加密时不需要身份验证的 AES key ,这非常酷(AES 比 RSA 快得多)。诀窍是在 AndroidKeyStore 之外生成 key ,然后将其导入两次,一次使用 PURPOSE_ENCRYPT,一次使用 PURPOSE_DECRYPT,使用两个不同的别名,并在解密版本。像这样的东西:

// Note that we do *not* specify "AndroidKeyStore" when we call getInstance()
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey secretKey = keyGen.generateKey();

// This time we do specify "AndroidKeyStore".
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);

// Now we import the encryption key, with no authentication requirements.
keyStore.setEntry(
"encrypt_key",
new KeyStore.SecretKeyEntry(secretKey),
new KeyProtection.Builder(KeyProperties.PURPOSE_ENCRYPT)
.setBlockMode(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.build());

// And the decryption key, this time requiring user authentication.
keyStore.setEntry(
"decrypt_key",
new KeyStore.SecretKeyEntry(secretKey),
new KeyProtection.Builder(KeyProperties.PURPOSE_DECRYPT)
.setBlockMode(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setUserAuthentication(true)
.build());

现在,您可以随时使用 key 别名“encrypt_key”加密,无需用户身份验证,并且您可以使用 key 别名“decrypt_key”解密,但只有在执行BiometricPrompt

这样做的缺点是 secret 会短暂地存在于非安全内存中。实际上,只有当攻击者在创建 key 时已经破坏了设备时,这才重要,在这种情况下,您很可能已经丢失了。

关于android - 如何只要求用户身份验证用于解密而不用于加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56564833/

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