【发布时间】:2018-02-15 13:14:15
【问题描述】:
我有一个生成加密/解密密钥的应用程序,它工作得很好。我将密钥存储在 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;
}
}
感谢大家的帮助。
【问题讨论】:
-
我认为您正在寻找 Content Providers 以便在您的应用程序之间安全地共享数据
-
感谢您的回答。问题是我的任务需要使用 KeyStore 和 KeyChain。我有一个内容提供者的实现,但正如我所说,我需要调查这些其他选项:S
-
我认为您应该能够通过在应用清单中定义sharedUserId 来共享
Keystore的访问权限(您还需要使用相同的证书)。但我还没有尝试过,而且它似乎不是最好的解决方案。 -
感谢您的想法。你知道关于在 KeyChain 中存储密钥的任何事情吗?有可能吗?
-
没问题!很抱歉,我没有在 Android 中使用 KeyChain 的经验,所以我无法回答这个问题:\
标签: android encryption android-keystore