I use the cryptography library: androidx.security:security-crypto:1.0.0-beta01 according to this example https://developer.android.com/guide/topics/security/cryptography I cannot correctly decrypt the file I encrypted. After decryption, all pages of the pdf file are blank. the screenshot shows the file size changes. Can anyone tell me what I'm doing wrong?
object Encryptor {
private lateinit var context: Context
private val keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC
private val masterKeyAlias = MasterKeys.getOrCreate(keyGenParameterSpec)
fun decrypt(file: File): File {
val bytes = file.encrypted.openFileInput().readBytes()
return File("${context.filesDir}/storage", "decrypt.pdf").apply { writeBytes(bytes) }
}
fun encrypt(fromFile: File, toFile: File) {
val text = fromFile.readText()
toFile.encrypted.openFileOutput().bufferedWriter().use { bufferWriter ->
bufferWriter.write(text)
}
}
private val File.encrypted: EncryptedFile
get() = EncryptedFile.Builder(
this,
context,
masterKeyAlias,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
).build()
}
The code snippet from the example decrypts the file for a very long time (6 sec +/-) And this code also does not solve my problem
val contents = file.encrypted.openFileInput().bufferedReader().useLines { lines ->
lines.fold("") { working, line ->
"$working\n$line"
}
}
someFile.bufferedWriter().write(contents)
