added AES 256 to upload and download
This commit is contained in:
56
app/src/main/java/com/dano/test1/AES256Helper.kt
Normal file
56
app/src/main/java/com/dano/test1/AES256Helper.kt
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -69,12 +69,11 @@ object DatabaseDownloader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Platzhalter für echte Entschlüsselung
|
|
||||||
private fun decryptDatabase(encryptedFile: File, outputFile: File) {
|
private fun decryptDatabase(encryptedFile: File, outputFile: File) {
|
||||||
// TODO: hier echte Entschlüsselungslogik einfügen
|
AES256Helper.decryptFile(encryptedFile, outputFile)
|
||||||
encryptedFile.copyTo(outputFile, overwrite = true)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun logFileContentAsHex(file: File, tag: String) {
|
private fun logFileContentAsHex(file: File, tag: String) {
|
||||||
try {
|
try {
|
||||||
val bytes = file.readBytes()
|
val bytes = file.readBytes()
|
||||||
|
|||||||
@ -23,25 +23,16 @@ object DatabaseUploader {
|
|||||||
fun uploadEncryptedDatabase(context: Context) {
|
fun uploadEncryptedDatabase(context: Context) {
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
try {
|
try {
|
||||||
Log.d("UPLOAD", "Upload gestartet")
|
|
||||||
|
|
||||||
val dbFile = context.getDatabasePath(DB_NAME)
|
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()) {
|
// Verschlüsselte Datei erstellen
|
||||||
Log.e("UPLOAD", "Datenbankdatei existiert nicht.")
|
val encryptedFile = File(context.cacheDir, ENCRYPTED_FILE_NAME)
|
||||||
return@launch
|
AES256Helper.encryptFile(dbFile, encryptedFile)
|
||||||
}
|
|
||||||
|
|
||||||
// Datenbankinhalt vor Upload ausgeben (Hex-String)
|
Log.d("UPLOAD", "Datei verschlüsselt: ${encryptedFile.absolutePath}")
|
||||||
logFileContentAsHex(dbFile, "UPLOAD")
|
|
||||||
|
|
||||||
val exportFile = File(context.cacheDir, ENCRYPTED_FILE_NAME)
|
uploadFile(context, encryptedFile, dbFile)
|
||||||
dbFile.copyTo(exportFile, overwrite = true)
|
|
||||||
|
|
||||||
Log.d("UPLOAD", "Datei kopiert: ${exportFile.absolutePath}")
|
|
||||||
|
|
||||||
uploadFile(context, exportFile, dbFile)
|
|
||||||
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e("UPLOAD", "Fehler beim Hochladen der DB", e)
|
Log.e("UPLOAD", "Fehler beim Hochladen der DB", e)
|
||||||
@ -49,6 +40,7 @@ object DatabaseUploader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun uploadFile(context: Context, file: File, originalDbFile: File) {
|
private fun uploadFile(context: Context, file: File, originalDbFile: File) {
|
||||||
val requestBody = MultipartBody.Builder()
|
val requestBody = MultipartBody.Builder()
|
||||||
.setType(MultipartBody.FORM)
|
.setType(MultipartBody.FORM)
|
||||||
|
|||||||
Reference in New Issue
Block a user