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 Dmitrenko 2025-02-28 22:57:41 +07:00 committed by GitHub
parent fefddb3b5a
commit dce8502165
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 29 additions and 5 deletions

View file

@ -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> {

View file

@ -1471,6 +1471,7 @@
<!-- DatabaseErrorView.kt -->
<string name="wrong_passphrase">Wrong database passphrase</string>
<string name="error_reading_passphrase">Error reading database passphrase</string>
<string name="encrypted_database">Encrypted database</string>
<string name="database_error">Database error</string>
<string name="keychain_error">Keychain error</string>
@ -1492,7 +1493,9 @@
<string name="restore_database_alert_desc">Please enter the previous password after restoring database backup. This action can not be undone.</string>
<string name="restore_database_alert_confirm">Restore</string>
<string name="database_restore_error">Restore database error</string>
<string name="restore_passphrase_not_found_desc">Passphrase not found in Keystore, please enter it manually. This may have happened if you restored the app\'s data using a backup tool. If it\'s not the case, please, contact developers.</string>
<string name="restore_passphrase_not_found_desc">Passphrase not found in Keystore, please enter it manually. This may have happened if you restored the app\'s data using a backup tool. If it\'s not the case, please contact developers.</string>
<string name="restore_passphrase_can_not_be_read_desc">Passphrase in Keystore can\'t be read. This may have happened after system update incompatible with the app. If it\'s not the case, please contact developers.</string>
<string name="restore_passphrase_can_not_be_read_enter_manually_desc">Passphrase in Keystore can\'t be read, please enter it manually. This may have happened after system update incompatible with the app. If it\'s not the case, please contact developers.</string>
<string name="database_upgrade">Database upgrade</string>
<string name="database_downgrade">Database downgrade</string>
<string name="incompatible_database_version">Incompatible database version</string>