Kotlin Android中主密钥不推荐使用后如何创建主密钥
我使用以下代码在我的应用程序中存储一些加密的信息Kotlin Android中主密钥不推荐使用后如何创建主密钥,kotlin,encrypted-shared-preference,Kotlin,Encrypted Shared Preference,我使用以下代码在我的应用程序中存储一些加密的信息 val masterKey = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC) val sharedPreferences = EncryptedSharedPreferences.create( "secret_shared_prefs", masterKey, this, Encrypte
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中不推荐使用MasterKey类,我应该使用MasterKey类,但我无法找出正确的方法来定义相同的MasterKey
是否有人可以显示与可用的MasterKey和MasterKey.Builder类的精确匹配
下面的解决方案是这样工作的:
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中轻松地执行相同的操作)
// 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();
MasterKey masterKey = new MasterKey.Builder(context, MasterKey.DEFAULT_MASTER_KEY_ALIAS)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build();
SharedPreferences sharedPreferences = EncryptedSharedPreferences.create(
context,
SHARED_PREF_NAME,
masterKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM);
你可以用这两种方法中的任何一种
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();
或
MasterKey.KeyScheme.AES256U GCM内部使用与上述相同的密钥生成器。您可以像下面这样使用它
//Creating MasterKey
val masterKey = MasterKey.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()
val fileToRead = "your_file_name.txt"
val encryptedFile = EncryptedFile.Builder(context,
File(context.filesDir, fileToRead),
masterKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
).build()
获取秘密共享首选项的我的版本:
private fun getSecretSharedPref(context: Context): SharedPreferences {
val masterKey = MasterKey.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()
return EncryptedSharedPreferences.create(context,
"secret_shared_prefs",
masterKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
}
谢谢,先生!我很接近,但文件对此并不清楚。:)我在问题中添加了代码。这很好,谢谢。如何返回现有的主密钥?这需要min API 23的
Key\u SIZE
?@xproph对于Master\u Key\u ALIAS
和Key\u SIZE
,我们是否可以使用DEFAULT\u Master\u Key\u ALIAS
和DEFAULT\u AES\u GCM\u Master\u Key\u SIZE
来代替硬编码的Maste密钥别名和密钥大小256,您应该使用MasterKey.DEFAULT\u MASTER\u KEY\u ALIAS
和MasterKey.DEFAULT\u AES\u GCM\u MASTER\u KEY\u SIZE
为什么我会在日志中看到“找不到密钥集,将生成一个新的密钥集”的警告?androiddeveloper开发人员对此警告有任何解决方案吗?Ameer我认为可以忽略这些警告。
//Creating MasterKey
val masterKey = MasterKey.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()
val fileToRead = "your_file_name.txt"
val encryptedFile = EncryptedFile.Builder(context,
File(context.filesDir, fileToRead),
masterKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
).build()
private fun getSecretSharedPref(context: Context): SharedPreferences {
val masterKey = MasterKey.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()
return EncryptedSharedPreferences.create(context,
"secret_shared_prefs",
masterKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
}