Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.1K
Vistas
Cómo crear masterKey después de que MasterKeys quedara en desuso en Android

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);
over 4 years ago · Hanz Gallego
5 Respuestas
Responde la pregunta

0

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)

  1. 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();
  2. 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

over 4 years ago · Hanz Gallego Denunciar

0

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);
over 4 years ago · Hanz Gallego Denunciar

0

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.

over 4 years ago · Hanz Gallego Denunciar

0

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()
over 4 years ago · Hanz Gallego Denunciar

0

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 ) }
over 4 years ago · Hanz Gallego Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda