Estoy usando el siguiente código para almacenar información cifrada en mi aplicación.
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 )Dado que la clase MasterKeys está en desuso en Android, debería usar la clase MasterKey y no puedo averiguar cuál es el método correcto para definir el mismo dominio.
¿Alguien podría mostrar la coincidencia exacta con las clases MasterKey y MasterKey.Builder disponibles?
La siguiente solución funcionó así:
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);Hoy tuve exactamente el mismo problema. Vea a continuación la solución/solución alternativa (el ejemplo está en el código Java, pero puede hacer lo mismo fácilmente en Kotlin)
Use MasterKey.Builder para crear MasterKey (en lugar de MasterKeys). Constrúyalo con KeyGenParameterSpec creado "manualmente":
// 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();Cree EncryptedSharedPreferences usando una versión ligeramente diferente del método "crear":
EncryptedSharedPreferences.create( MainActivity.this, "your-app-preferences-name", masterKey, // masterKey created above EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM);Eso debería hacer el truco :)
Referencia y más detalles: https://devmainapps.blogspot.com/2020/06/android-masterkeys-deprecated-how-to.html
prueba este
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);puedes usar cualquiera de las dos formas
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();o
MasterKey masterKey = new MasterKey.Builder(context,MasterKey.DEFAULT_MASTER_KEY_ALIAS). setKeyScheme(MasterKey.KeyScheme.AES256_GCM).build();MasterKey.KeyScheme.AES256_GCM utiliza internamente la misma especificación de generación de claves que se indicó anteriormente.
Puedes usarlo así a continuación
//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()Mi versión para obtener una preferencia secreta compartida:
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 ) }