Skip to content

Commit 5f3ef8a

Browse files
committed
ADD BadPaddingException if restore pw is wrong #13
1 parent 7818e41 commit 5f3ef8a

File tree

5 files changed

+42
-16
lines changed

5 files changed

+42
-16
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
![Build](https://github.com/rafi0101/Android-Room-Database-Backup/workflows/Android%20CI/badge.svg)
55
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/de.raphaelebner/roomdatabasebackup/badge.svg)](https://maven-badges.herokuapp.com/maven-central/de.raphaelebner/roomdatabasebackup)
6+
[![Room Version](https://img.shields.io/badge/room_version-2.4.0-orange)](https://developer.android.com/jetpack/androidx/releases/room#version_240_3)
67
[![API](https://img.shields.io/badge/API-21%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=21)
78
![Language](https://img.shields.io/badge/language-Kotlin-orange.svg)
89
[![PRWelcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/rafi0101/Android-Room-Database-Backup/pulls)
@@ -63,7 +64,8 @@ implementation 'com.github.rafi0101:Android-Room-Database-Backup:1.0.0-beta09'
6364
### New:
6465

6566
From now on the Android-Room-Database-Backup library is pushed
66-
to [Maven Central](https://search.maven.org/artifact/de.raphaelebner/roomdatabasebackup/).
67+
to [Maven Central](https://search.maven.org/artifact/de.raphaelebner/roomdatabasebackup/).
68+
Add the dependency for `Android-Room-Database-Backup ` to your app-level `build.gradle` file.
6769

6870
```groovy
6971
implementation 'de.raphaelebner:roomdatabasebackup:1.0.0-beta09'
@@ -95,7 +97,7 @@ Usage
9597
.database(*YourDatabase*.getInstance(this))
9698
```
9799

98-
e.g. [`YourDatabase.kt`](app/src/main/java/com/ebner/roomdatabasebackup/sample/database/main/FruitDatabase.kt)
100+
e.g. [`YourDatabase.kt`](app/src/main/java/de/raphaelebner/roomdatabasebackup/sample/database/main/FruitDatabase.kt)
99101

100102
**Optional**
101103

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ dependencies {
5858
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
5959

6060
//ROOM SQLite
61-
def room_version = "2.3.0"
61+
def room_version = "2.4.0"
6262

6363
implementation "androidx.room:room-runtime:$room_version"
6464
kapt "androidx.room:room-compiler:$room_version"

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22
buildscript {
3-
ext.kotlin_version = '1.6.0'
3+
ext.kotlin_version = '1.6.10'
44
repositories {
55
google()
66
mavenCentral()
77
}
88
dependencies {
9-
classpath 'com.android.tools.build:gradle:7.0.3'
9+
classpath 'com.android.tools.build:gradle:7.0.4'
1010
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1111

1212
// NOTE: Do not place your application dependencies here; they belong
@@ -16,7 +16,7 @@ buildscript {
1616

1717
plugins {
1818
id('io.github.gradle-nexus.publish-plugin') version "1.1.0"
19-
id("org.jetbrains.dokka") version "1.6.0"
19+
id("org.jetbrains.dokka") version "1.6.10"
2020
}
2121

2222
apply from: "${rootDir}/scripts/publish-root.gradle"

core/build.gradle

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,17 @@ android {
2626
}
2727

2828
buildTypes {
29+
debug{
30+
buildConfigField("long", "VERSION_CODE", "${defaultConfig.versionCode}")
31+
buildConfigField("String","VERSION_NAME","\"${defaultConfig.versionName}\"")
32+
33+
}
2934
release {
3035
minifyEnabled false
3136
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
37+
buildConfigField("long", "VERSION_CODE", "${defaultConfig.versionCode}")
38+
buildConfigField("String","VERSION_NAME","\"${defaultConfig.versionName}\"")
39+
3240
}
3341
}
3442

@@ -59,7 +67,7 @@ dependencies {
5967
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
6068

6169
//ROOM SQLite
62-
def room_version = "2.3.0"
70+
def room_version = "2.4.0"
6371

6472
implementation "androidx.room:room-runtime:$room_version"
6573
kapt "androidx.room:room-compiler:$room_version"

core/src/main/java/de/raphaelebner/roomdatabasebackup/core/RoomBackup.kt

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.apache.commons.io.comparator.LastModifiedFileComparator
2222
import java.io.*
2323
import java.text.SimpleDateFormat
2424
import 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

Comments
 (0)