One operation of my Android project is to decrypt one file (chosen in a set of ~30 files for pagination1) of ~10 Mb each and I do this with the following:
fun InputStream.cipherDecryptWithSessionPwd(password: String): String {
var payloadArray: List<Byte>? = this.readBytes().toList()
var salt = payloadArray?.subList(0,32)
var spec: Rfc2898DeriveBytes? = Rfc2898DeriveBytes(password, salt?.toByteArray(), 50000)
var realData = payloadArray?.subList(32, payloadArray.size)
val keyBytes = spec?.getBytes(256 / 8)
val ivBytes = spec?.getBytes(128 / 8)
var cipher: Cipher? = Cipher.getInstance("AES/CFB/PKCS7Padding")
var secretKey: SecretKeySpec? = SecretKeySpec(keyBytes, "AES")
var ivSpec: IvParameterSpec? = IvParameterSpec(ivBytes)
cipher?.init(Cipher.DECRYPT_MODE, secretKey, ivSpec)
var byteArrayInputStream: ByteArrayInputStream? = ByteArrayInputStream(realData?.toByteArray())
var decipherStream: CipherInputStream? = CipherInputStream(byteArrayInputStream, cipher)
val d = BufferedReader(InputStreamReader(decipherStream, Charsets.UTF_8), 0xFFFF)
return d.readLine()
}
The problem with this is that at around the 72nd time2 I repeat the operation Android kills my application because it runs out of memory3.
This is the second version of the decryption function, the first one caused the OOM error around the 25th time the operation was made. The problem here clearly is the number of conversions done (InputStream -> ByteArray -> List<Byte> and others).
I don't know how to optimize more, the first version of the method was not mine and I never did encryption (and optimization) before.
Scenario (and what have been tried):
1 - Files are stored encrypted on the device, can't have them decrypted but at runtime
2 - With the decrypted result (JSON) I fill a model using [Gson] (https://github.com/google/gson), but the OOME seem to be thrown when my function is running (the exception points at the first line of the function)
3 - Calculations are done and the model is disposed of when the next page is shown, then the whole operation has to be repeated
# - We tried reducing the number of JSON "records" inside each file from 5000 per file to 1000 per file reducing the file size to 2 Mb but the problem persists
Notes:
1 - We event tried reducing the pagination reaching files size to 2 Mb but the problem persists
2 - This has to be done at least 130 times per session
3 - With, Knox my application is the only one allowed to run on the device
nulleach object when no more needed and nothing worked, I think that asking here is the last resort. One option would drastically change the whole project causing a seatback of months \$\endgroup\$