如何在 android 密钥库中设置带密码的密钥条目

how to set key entry with password in android keystore

我使用 KeyPairGenerator 生成了一个密钥对。如何为我的私钥设置密码,让知道密码的人只能访问私钥。

Android KeyStore 不能用密码保护。

请求和附加密码对使用移动设备的用户不友好。可以保护密钥,要求用户在使用密钥之前解锁设备。然后只有设备的所有者才能使用它,因为 he/she 已经配置了 PIN/pattern 甚至指纹。如果你需要额外的保护级别,那么你可以定义一些密钥只能与指纹认证一起使用

The Android Keystore system lets you store cryptographic keys in a container to make it more difficult to extract from the device. Once keys are in the keystore, they can be used for cryptographic operations with the key material remaining non-exportable. Moreover, it offers facilities to restrict when and how keys can be used, such as requiring user authentication for key use or restricting keys to be used only in certain cryptographic modes.

例如:

KeyPairGenerator kpg = KeyPairGenerator.getInstance(
            KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore");

kpg.initialize(new KeyGenParameterSpec.Builder(
            alias,
            KeyProperties.PURPOSE_SIGN | KeyProperties.PURPOSE_VERIFY)
            .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
            .setKeySize(keySize)
            .setUserAuthenticationRequired()
            .build());

 KeyPair keyPair = kpg.generateKeyPair();

如果您指的是保护您的密钥不被其他应用程序使用(您的问题中不清楚),请不要担心,Android密钥只能由创建它们的应用程序使用