souramoo / unapkm

APKM file decryptor
Apache License 2.0
176 stars 19 forks source link

Question: how come I can't re-use the header? #12

Closed AndroidDeveloperLB closed 4 years ago

AndroidDeveloperLB commented 4 years ago

I remember that I was told here somewhere, that I can re-use the header so that it will be faster.

I've tried to do it, but it failed. Example:

                val inputFile = File("/storage/emulated/0/test.apkm")
                Log.d("AppLog", "getting header..")
                var header: UnApkm.Header? = null
                val lazySodium = LazySodiumAndroid(SodiumAndroid())
                FileInputStream(inputFile).use {
                    header = UnApkm.processHeader(it, lazySodium)
                }
                Log.d("AppLog", "getting into zip")
                FileInputStream(inputFile).use {
                    ZipInputStream(UnApkm.decryptStream(it, header!!, lazySodium)).use {
                        Log.d("AppLog", "got into zip stream")
                        while (true) {
                            val name = it.nextEntry?.name ?: break
                            Log.d("AppLog", "name:$name")
                        }
                    }
                }

It crashes when it reaches the part of the val name = it.nextEntry?.name ?: break as it fails to parse the file as a zip file :

 W/System.err: java.io.IOException: decrypto error
 W/System.err:     at com.lb.apkmtest.UnApkm$decryptStream$pipeWriter$1.run(UnApkm.kt:57)
 W/System.err:     at java.lang.Thread.run(Thread.java:919)

Using the normal method, it works fine:

ZipInputStream(UnApkm.decryptStream(FileInputStream(inputFile))).use {
    Log.d("AppLog", "got into zip stream")
    while (true) {
        val name = it.nextEntry?.name ?: break
        Log.d("AppLog", "name:$name")
    }
}

How come? I tried to re-create the instance of LazySodiumAndroid(SodiumAndroid()) . It didn't work.

AndroidDeveloperLB commented 4 years ago

Never mind, forgot it was special:

Log.d("AppLog", "getting header")
val lazySodiumAndroid = LazySodiumAndroid(SodiumAndroid())
val header = FileInputStream(inputFile).use { UnApkm.processHeader(it, lazySodiumAndroid, true) }
Log.d("AppLog", "done getting header. handling content")
FileInputStream(inputFile).use {
    UnApkm.processHeader(it, lazySodiumAndroid, false)
    ZipInputStream(UnApkm.decryptStream(it, header, lazySodiumAndroid)).use {
        Log.d("AppLog", "got into zip stream")
        while (true) {
            val name = it.nextEntry?.name ?: break
            Log.d("AppLog", "name:$name")
        }
    }
}
Log.d("AppLog", "done handling content. Handle it again:")
FileInputStream(inputFile).use {
    UnApkm.processHeader(it, lazySodiumAndroid, false)
    ZipInputStream(UnApkm.decryptStream(it, header, lazySodiumAndroid)).use {
        Log.d("AppLog", "got into zip stream2")
        while (true) {
            val name = it.nextEntry?.name ?: break
            Log.d("AppLog", "name:$name")
        }
    }
}