@@ -22,6 +22,7 @@ import org.apache.commons.io.comparator.LastModifiedFileComparator
2222import java.io.*
2323import java.text.SimpleDateFormat
2424import java.util.*
25+ import javax.crypto.BadPaddingException
2526
2627
2728/* *
@@ -371,7 +372,7 @@ class RoomBackup(var context: Context) : FragmentActivity() {
371372 // Close the database
372373 roomDatabase!! .close()
373374 if (backupIsEncrypted) {
374- val encryptedBytes = encryptBackup()
375+ val encryptedBytes = encryptBackup() ? : return
375376 val bos = BufferedOutputStream (FileOutputStream (destination, false ))
376377 bos.write(encryptedBytes)
377378 bos.flush()
@@ -400,7 +401,7 @@ class RoomBackup(var context: Context) : FragmentActivity() {
400401 // Close the database
401402 roomDatabase!! .close()
402403 if (backupIsEncrypted) {
403- val encryptedBytes = encryptBackup()
404+ val encryptedBytes = encryptBackup() ? : return
404405 destination.write(encryptedBytes)
405406 } else {
406407 // Copy current database to save location (/files dir)
@@ -536,7 +537,7 @@ class RoomBackup(var context: Context) : FragmentActivity() {
536537 val fileExtension = source.extension
537538 if (backupIsEncrypted) {
538539 copy(source, TEMP_BACKUP_FILE )
539- val decryptedBytes = decryptBackup()
540+ val decryptedBytes = decryptBackup() ? : return
540541 val bos = BufferedOutputStream (FileOutputStream (DATABASE_FILE , false ))
541542 bos.write(decryptedBytes)
542543 bos.flush()
@@ -561,8 +562,6 @@ class RoomBackup(var context: Context) : FragmentActivity() {
561562 * @param source InputStream
562563 */
563564 private fun doRestore (source : InputStream ) {
564- // Close the database
565- roomDatabase!! .close()
566565 if (backupIsEncrypted) {
567566 // Save inputstream to temp file
568567 source.use { input ->
@@ -571,12 +570,19 @@ class RoomBackup(var context: Context) : FragmentActivity() {
571570 }
572571 }
573572 // Decrypt tempfile and write to database file
574- val decryptedBytes = decryptBackup()
573+ val decryptedBytes = decryptBackup() ? : return
574+
575+ // Close the database if decryption is succesfull
576+ roomDatabase!! .close()
577+
575578 val bos = BufferedOutputStream (FileOutputStream (DATABASE_FILE , false ))
576579 bos.write(decryptedBytes)
577580 bos.flush()
578581 bos.close()
579582 } else {
583+ // Close the database
584+ roomDatabase!! .close()
585+
580586 // Copy back database and replace current database
581587 source.use { input ->
582588 DATABASE_FILE .outputStream().use { output ->
@@ -621,18 +627,28 @@ class RoomBackup(var context: Context) : FragmentActivity() {
621627 val fileData = encryptDecryptBackup.readFile(TEMP_BACKUP_FILE )
622628
623629 val aesEncryptionManager = AESEncryptionManager ()
624- val decryptedBytes = aesEncryptionManager.decryptData(sharedPreferences, encryptPassword, fileData)
630+ val decryptedBytes =
631+ aesEncryptionManager.decryptData(sharedPreferences, encryptPassword, fileData)
625632
626633 // Delete tem file
627634 TEMP_BACKUP_FILE .delete()
628635
629636 return decryptedBytes
630637
638+ } catch (e: BadPaddingException ) {
639+ if (enableLogDebug) Log .d(TAG , " error during decryption (wrong password): ${e.message} " )
640+ onCompleteListener?.onComplete(
641+ false ,
642+ " error during decryption (wrong password) see Log for more details (if enabled)"
643+ )
644+ return null
631645 } catch (e: Exception ) {
632646 if (enableLogDebug) Log .d(TAG , " error during decryption: ${e.message} " )
633- onCompleteListener?.onComplete(false , " error during decryption (maybe wrong password) see Log for more details (if enabled)" )
647+ onCompleteListener?.onComplete(
648+ false ,
649+ " error during decryption see Log for more details (if enabled)"
650+ )
634651 return null
635- // throw Exception("error during decryption: $e")
636652 }
637653 }
638654
0 commit comments