将 AES 密钥存储在 Android

Store AES key in Android

我想将 AES 密钥存储在 pre-M 设备上的 AndroidKeyStore 中

我尝试使用 KeyGenerator

生成的密钥
KeyGenerator keyGen = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES);
keyGen.init(256);
SecretKey secretKey = keyGen.generateKey();

但我无法从 KeyStore 访问该密钥,后来我尝试使用 KeyPairGenerator

KeyPairGenerator kpg = KeyPairGenerator.getInstance(
                KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
kpg.initialize(new KeyPairGeneratorSpec.Builder(this)
                .setAlias("alias")
                .build());
KeyPair kp = kpg.genKeyPair();

但是

java.security.NoSuchAlgorithmException: KeyPairGenerator AES implementation not found

Android 密钥库自 API 级别 23 起仅支持 AES(参见 https://developer.android.com/training/articles/keystore.html#SupportedAlgorithms)。在旧平台上,您可以使用 Android Keystore RSA 密钥包装 AES 密钥。但是,这意味着 AES 密钥的密钥 material 将在您的应用进程中可用,这会消除使用 Android 密钥库的许多安全优势。