在 Android 中弃用 MasterKeys 后如何创建 masterKey [英] How to create masterKey after MasterKeys deprecated in Android
问题描述
我正在使用以下代码在我的应用程序中存储一些加密的信息.
I am using the following code to store some information encrypted in my app.
val masterKey = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
val sharedPreferences = EncryptedSharedPreferences.create(
"secret_shared_prefs",
masterKey,
this,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
由于 Android 中不推荐使用 MasterKeys 类,我应该使用 MasterKey 类,但我不知道什么是正确的方法来定义相同的掌握.
Since the MasterKeys class deprecated in Android, I should use the MasterKey class and but I cannot figure out what is the right method to get the same mastery defined.
有人可以显示与可用的 MasterKey 和 MasterKey.Builder 类的完全匹配吗?
Could somebody show the exact match with the available MasterKey and MasterKey.Builder classes?
下面的解决方案是这样工作的:
The below solution worked like this:
val spec = KeyGenParameterSpec.Builder(
"_androidx_security_master_key_",
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT
)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setKeySize(256)
.build()
val masterKey: MasterKey = MasterKey.Builder(this)
.setKeyGenParameterSpec(spec)
.build()
val sharedPreferences = EncryptedSharedPreferences.create(
this,
"secret_shared_prefs",
masterKey, // masterKey created above
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM);
推荐答案
我今天遇到了完全相同的问题.请参阅下面的修复/解决方法(示例在 Java 代码中,但您可以在 Kotlin 中轻松执行相同的操作)
I had exactly the same problem today. See below for fix/workaround (example is in Java code but you can easily do the same in Kotlin)
使用 MasterKey.Builder 创建 MasterKey(而不是 MasterKeys).使用手动"构建它创建的 KeyGenParameterSpec:
Use MasterKey.Builder to create MasterKey (instead of MasterKeys). Build it with "manually" created KeyGenParameterSpec:
// this is equivalent to using deprecated MasterKeys.AES256_GCM_SPEC
KeyGenParameterSpec spec = new KeyGenParameterSpec.Builder(
MASTER_KEY_ALIAS,
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setKeySize(KEY_SIZE)
.build();
MasterKey masterKey = new MasterKey.Builder(MainActivity.this)
.setKeyGenParameterSpec(spec)
.build();
使用略有不同版本的create"创建 EncryptedSharedPreferences方法:
Create EncryptedSharedPreferences using slightly different version of "create" method:
EncryptedSharedPreferences.create(
MainActivity.this,
"your-app-preferences-name",
masterKey, // masterKey created above
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM);
那应该可以解决问题:)
That should do the trick :)
参考和更多细节:https://devmainapps.blogspot.com/2020/06/android-masterkeys-deprecated-how-to.html
这篇关于在 Android 中弃用 MasterKeys 后如何创建 masterKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!