added AES 256 to upload and download

This commit is contained in:
oxidiert
2025-08-11 13:53:31 +02:00
parent 385e7cd72e
commit 0052d1908a
3 changed files with 65 additions and 18 deletions

View File

@ -0,0 +1,56 @@
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import javax.crypto.Cipher
import javax.crypto.CipherInputStream
import javax.crypto.CipherOutputStream
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
import kotlin.random.Random
object AES256Helper {
private const val TRANSFORMATION = "AES/CBC/PKCS5Padding"
private const val ALGORITHM = "AES"
private const val IV_SIZE = 16
// Beispiel-Key: 32 Bytes = 256 bit. Ersetze das durch deinen eigenen sicheren Schlüssel!
private val keyBytes = "12345678901234567890123456789012".toByteArray(Charsets.UTF_8)
private val secretKey = SecretKeySpec(keyBytes, ALGORITHM)
// Verschlüsseln: InputFile -> OutputFile (mit zufälligem IV vorne in der Datei)
fun encryptFile(inputFile: File, outputFile: File) {
val iv = ByteArray(IV_SIZE)
Random.nextBytes(iv)
val ivSpec = IvParameterSpec(iv)
val cipher = Cipher.getInstance(TRANSFORMATION)
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec)
FileOutputStream(outputFile).use { fileOut ->
// IV vorne reinschreiben
fileOut.write(iv)
CipherOutputStream(fileOut, cipher).use { cipherOut ->
FileInputStream(inputFile).use { fileIn ->
fileIn.copyTo(cipherOut)
}
}
}
}
// Entschlüsseln: InputFile (IV+Ciphertext) -> OutputFile (Klartext)
fun decryptFile(inputFile: File, outputFile: File) {
FileInputStream(inputFile).use { fileIn ->
val iv = ByteArray(IV_SIZE)
if (fileIn.read(iv) != IV_SIZE) throw IllegalArgumentException("Ungültige Datei oder IV fehlt")
val ivSpec = IvParameterSpec(iv)
val cipher = Cipher.getInstance(TRANSFORMATION)
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec)
CipherInputStream(fileIn, cipher).use { cipherIn ->
FileOutputStream(outputFile).use { fileOut ->
cipherIn.copyTo(fileOut)
}
}
}
}
}

View File

@ -69,12 +69,11 @@ object DatabaseDownloader {
}
}
// Platzhalter für echte Entschlüsselung
private fun decryptDatabase(encryptedFile: File, outputFile: File) {
// TODO: hier echte Entschlüsselungslogik einfügen
encryptedFile.copyTo(outputFile, overwrite = true)
AES256Helper.decryptFile(encryptedFile, outputFile)
}
private fun logFileContentAsHex(file: File, tag: String) {
try {
val bytes = file.readBytes()

View File

@ -23,25 +23,16 @@ object DatabaseUploader {
fun uploadEncryptedDatabase(context: Context) {
CoroutineScope(Dispatchers.IO).launch {
try {
Log.d("UPLOAD", "Upload gestartet")
val dbFile = context.getDatabasePath(DB_NAME)
Log.d("UPLOAD", "Pfad zur DB: ${dbFile.absolutePath}, existiert: ${dbFile.exists()}")
if (!dbFile.exists()) return@launch
if (!dbFile.exists()) {
Log.e("UPLOAD", "Datenbankdatei existiert nicht.")
return@launch
}
// Verschlüsselte Datei erstellen
val encryptedFile = File(context.cacheDir, ENCRYPTED_FILE_NAME)
AES256Helper.encryptFile(dbFile, encryptedFile)
// Datenbankinhalt vor Upload ausgeben (Hex-String)
logFileContentAsHex(dbFile, "UPLOAD")
Log.d("UPLOAD", "Datei verschlüsselt: ${encryptedFile.absolutePath}")
val exportFile = File(context.cacheDir, ENCRYPTED_FILE_NAME)
dbFile.copyTo(exportFile, overwrite = true)
Log.d("UPLOAD", "Datei kopiert: ${exportFile.absolutePath}")
uploadFile(context, exportFile, dbFile)
uploadFile(context, encryptedFile, dbFile)
} catch (e: Exception) {
Log.e("UPLOAD", "Fehler beim Hochladen der DB", e)
@ -49,6 +40,7 @@ object DatabaseUploader {
}
}
private fun uploadFile(context: Context, file: File, originalDbFile: File) {
val requestBody = MultipartBody.Builder()
.setType(MultipartBody.FORM)