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)
谢谢大家的帮助。
| 归档时间: |
|
| 查看次数: |
5368 次 |
| 最近记录: |