Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.1K
Views
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 answers
Answer question

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 Report

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 Report

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!