KeyStore 与 KeyChain

Med*_*iha 8 encryption android android-keystore

我有一个生成加密/解密密钥的应用程序,它运行良好。我将我的密钥存储在 KeyStore 和 IV 中,作为保存在外部存储上的加密文件中的第一个 12B。当我想解密文件时,我从外部存储中获取文件(因此我得到了 IV)和 KeyStore 中的密钥,并且我能够获取原始内容。我的第二个应用程序 App2 可以访问外部存储中的文件(因此它可以获取 IV),但无法从 App1 KeyStore 获取密钥。我正在阅读有关 KeyChain 的内容,它在官方文档中说它不是应用程序私有的(当您需要系统范围的凭据时,请使用 KeyChain API)。我可以以某种方式将我的密钥存储在这个 KeyChain 或其他地方,以便我的 App2 可以获取它(经过一些用户批准或类似的东西)。这是我用来在 App1 中创建和存储密钥的代码。

 private static SecretKey createAndStoreKey() {
        KeyGenerator keyGen;
        try {
            // Generate 256-bit key
            keyGen = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, KEY_STORE_NAME);

            final KeyGenParameterSpec keyGenParameterSpec = new KeyGenParameterSpec.Builder(KEY_ALIAS,
                    KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                    .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
                    .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
                    .build();

            keyGen.init(keyGenParameterSpec);

            SecretKey secretKey = keyGen.generateKey();
            if(secretKey != null)
                return secretKey;
            else
                return null;
        }
        catch (NoSuchProviderException e){
            e.printStackTrace();
            return null;
        }
        catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
        catch (InvalidAlgorithmParameterException e){
            e.printStackTrace();
            return null;
        }
    }
Run Code Online (Sandbox Code Playgroud)

谢谢大家的帮助。

nic*_*e j 9

当您需要系统范围的凭据时,请使用 KeyChain API。当应用程序通过 KeyChain API 请求使用任何凭证时,用户可以通过系统提供的 UI 选择应用程序可以访问哪些已安装的凭证。这允许多个应用程序在用户同意的情况下使用同一组凭据。

使用 Android Keystore 提供程序让单个应用程序存储自己的凭据,只有应用程序本身可以访问。这为应用程序提供了一种管理只能由其自身使用的凭据的方法,同时提供与 KeyChain API 为系统范围凭据提供的相同安全优势。此方法不需要用户交互来选择凭据。 参考