Fix decryption function

This commit is contained in:
2020-07-05 18:16:37 +02:00
parent b1b4ad3030
commit 576e16a2fc
10 changed files with 128 additions and 43 deletions

View File

@@ -1,12 +1,12 @@
package com.github.mondei1.offpass
import android.content.Context
import android.util.Base64
import android.util.Log
import com.github.mondei1.offpass.entities.Compression
import com.github.mondei1.offpass.entities.CompressionHelper
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.channels.broadcast
import kotlinx.coroutines.runBlocking
import java.lang.Error
@@ -71,7 +71,7 @@ class QRSchema {
fields[0] = fields[0].replace("%", "")
session_key = fields[0].substring(0, 10) // Get first 10 chars, which are the
// session key
this.title = fields[0].substring(11, fields[0].length)
this.title = fields[0].substring(10, fields[0].length)
} else {
this.title = fields[0]
}
@@ -121,18 +121,110 @@ class QRSchema {
+ fields[i].toString())
}
}
// Final step is to resolve any compressed values
}
Log.i("QR-Code schema", "Found: $session_key, $title, $username, $password, " +
"$email, $website_url. ${custom.toString()} and ${question_awnser.toString()}")
return true
}
/**
* This function will take all defined variables and converts them into the QR-Code schema
* This function will take an raw string input and will decrypt it.
*
* @return It returns the raw decrypted raw value.
*/
fun build() {
fun decrypt(raw: String, passphrase: String): String {
val parts = raw.split(":")
val iv = parts[1]
val salt = parts[2]
val encrypted_content = parts[3]
Log.i("Decrypt schema", "Give data: $encrypted_content")
this.decrypted_raw = CryptoOperations().decrypt(
encrypted_content, passphrase,
iv.toByteArray(), salt.toByteArray())
return this.decrypted_raw
}
/**
* This function will take all defined variables and converts them into the QR-Code schema.
*
* @param compress_values Takes an string array where a entry can be `website_url` in case of an URL or
* the key of an optional field. For a security question take the question as key.
*/
fun build(compress_values: Array<String>, passphrase: String): String {
/* First phase is to construct the encrypted data. */
val session_key = CryptoOperations().nextString(10) // used for
var used_compression: Boolean = false
var url_copy = website_url
if (compress_values.contains("website_url")) {
used_compression = true
val email_key = CryptoOperations().nextString(4)
this.db?.add(session_key, Compression(email_key, website_url, null, null))
url_copy = "\$$email_key"
}
var encrypted_content: String =
"${this.title}|${this.username}|${this.password}|${this.email}|${url_copy}|"
// Loop thought optional/custom fields
for (i in this.custom) {
if (compress_values.contains(i.key)) {
used_compression = true
var key_key = CryptoOperations().nextString(4)
var key_value = CryptoOperations().nextString(4)
this.db?.add(session_key,
Compression(key_key, i.key, null, null))
this.db?.add(session_key,
Compression(key_value, i.value, null, null))
encrypted_content += "(\$$key_key)\$$key_value|"
} else {
encrypted_content += "(${i.key})${i.value}|"
}
}
// Loop thought security questions
for (i in this.question_awnser) {
used_compression = true
if (compress_values.contains(i.key)) {
var key_key = CryptoOperations().nextString(4)
var key_value = CryptoOperations().nextString(4)
this.db?.add(session_key,
Compression(key_key, i.key, null, null))
this.db?.add(session_key,
Compression(key_value, i.value, null, null))
encrypted_content += "()\$$key_key%\$$key_value|"
} else {
encrypted_content += "()${i.key}%${i.value}|"
}
}
encrypted_content = encrypted_content.dropLast(1)
if (used_compression) encrypted_content = "%$session_key%$encrypted_content"
Log.i("QR-Code Builder", "Constructed encrypted content: $encrypted_content")
// Now, let's encrypt that
val enc = runBlocking {
CryptoOperations().encrypt(passphrase, encrypted_content)
}
// TODO: Make schema version dynamic (currently hardcoded)
var final = "op1:" +
"${String(enc.iv)}:" +
"${String(enc.salt)}:" +
enc.result
.replace("\n", "")
Log.i("QR-Code Builder", "Returning final result: $final")
this.raw = final
return final
}