有人成功使用 Jetpack Security 通过两个单独的主密钥进行加密吗?用例是使用“常规”密钥加密一组数据,另一组使用具有额外生物特征保护的密钥加密。
仅使用两个具有相同规格的单独键尝试以下操作,仅用于测试:
class MainActivity : AppCompatActivity() {
private val firstMasterKey by lazy {
MasterKeys.getOrCreate(provideKeyGenParameters("aliasFirst"))
}
private val secondMasterKey by lazy {
MasterKeys.getOrCreate(provideKeyGenParameters("aliasSecond"))
}
private val firstEncryptedFile by lazy {
EncryptedFile.Builder(
File(filesDir, "firstFile"),
this,
firstMasterKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
)
.build()
}
private val secondEncryptedFile by lazy {
EncryptedFile.Builder(
File(filesDir, "secondFile"),
this,
secondMasterKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
)
.build()
}
private fun provideKeyGenParameters(alias: String) =
KeyGenParameterSpec.Builder(
alias,
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT
)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setKeySize(256)
.build()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
saveFiles()
}
private fun saveFiles() {
firstEncryptedFile.openFileOutput().use {
it.write(ByteArray(1) { Byte.MIN_VALUE })
}
// if I comment this save it works fine
// as soon as I uncomment it the app crashes
secondEncryptedFile.openFileOutput().use {
it.write(ByteArray(1) { Byte.MIN_VALUE })
}
}
}
但是InvalidProtocolBufferException: Protocol message contained an invalid tag (zero)
一旦使用另一个密钥就得到了。这发生在稳定版本和最新的 alpha03 上。
我向 Google 发布了关于https://issuetracker.google.com/issues/192470296的错误报告,但我没有希望,因为基于其他报告的错误,他们根本不关心。无论如何,如果您遇到问题,请星标,也许有人会注意到。