android: allow to enter passphrase in case of error reading it (#5683)

* android: allow to enter passphrase in case of error reading it

* change

* refactor

* strings

---------

Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com>
This commit is contained in:
Stanislav DmitrenkoandEvgeny Poberezkin authored and GitHub committed 2025-02-28 15:57:41 +00:00
1 parent fefddb3b5a
commit dce8502165
2 files changed
+29 -5

No files matched your search

@@ -30,10 +30,31 @@ internal class Cryptor: CryptorInterface {
}
return null
}
val cipher: Cipher = Cipher.getInstance(TRANSFORMATION)
val spec = GCMParameterSpec(128, iv)
cipher.init(Cipher.DECRYPT_MODE, secretKey, spec)
return runCatching { String(cipher.doFinal(data))}.onFailure { Log.e(TAG, "doFinal: ${it.stackTraceToString()}") }.getOrNull()
try {
val cipher: Cipher = Cipher.getInstance(TRANSFORMATION)
val spec = GCMParameterSpec(128, iv)
cipher.init(Cipher.DECRYPT_MODE, secretKey, spec)
return String(cipher.doFinal(data))
} catch (e: Throwable) {
Log.e(TAG, "cipher.init: ${e.stackTraceToString()}")
val randomPassphrase = appPreferences.initialRandomDBPassphrase.get()
AlertManager.shared.showAlertMsg(
title = generalGetString(MR.strings.error_reading_passphrase),
text = generalGetString(if (randomPassphrase) {
MR.strings.restore_passphrase_can_not_be_read_desc
} else {
MR.strings.restore_passphrase_can_not_be_read_enter_manually_desc
}
)
.plus("\n\n").plus(e.stackTraceToString())
)
if (randomPassphrase) {
// do not allow to override initial random passphrase in case of such error
throw e
}
return null
}
}
override fun encryptText(text: String, alias: String): Pair<ByteArray, ByteArray> {