0

I wrote a small python script a while ago, which uses the python cryptography module to encrypt some data using fernet. To do this, it reads the data from a file as bytes, runs the Fernet(key).encrypt(data) method, which returns a byte-object which can then be saved to another file. This works perfectly, with text as well as with images, encryption as well as decryption.

However, I now wanted to make an app in kotlin (using the fernet-java8 library) to be able to decrypt my data without my computer. For that, I use the following function to retrieve the data from the file:

val input: InputStream? = data!!.data?.let { getContentResolver().openInputStream(it) }
val inputAsString = input?.bufferedReader().use { it?.readText() } 

This code is more or less copied together from various posts. The data is then decrypted using this method:

fun decrypt(decabledata:String){
        println(decabledata.toString())
        val token=Token.fromString(decabledata)
        //val token=decabledata
        //val token= Token.fromString("gAAAAABj512Pcv-sxoGmeeI5oM-a_GSOSUORKjxrp1QEtZH97Gv0XpYFTcMS2MDD8bPBTI_WYbadmG7dcdqE72ySNx_9K6A2sA==")
        val fernetKey=Key("MYKEY")

        val validator: Validator<String> = object : StringValidator {
            @RequiresApi(Build.VERSION_CODES.O)
            override fun getTimeToLive(): TemporalAmount {
                //val timere:Long = 24
                return Duration.ofSeconds(Instant.MAX.getEpochSecond())
            }
        }
        val data = token.validateAndDecrypt(fernetKey, validator)
        val resview=findViewById(R.id.textView1) as TextView
        resview.setText(data.toString())
        println(data)
    }

This also works perfectly when decrypting text files. However, when I try to decrypt an image, the resulting file (which is then saved to Downloads) is not properly working and it cant be displayed. This makes (kind of) sense, because everything in this method is a string (Although when going by the python script, you would not need to distinct between the type of data). But when I try to change the method Token.fromString() to Token.fromBytes() and give a ByteArray object instead of a String, the method crashes with the following exception:

Process: com.example.cryptomobile, PID: 12020
    java.time.DateTimeException: Instant exceeds minimum or maximum instant
        at java.time.Instant.create(Instant.java:405)
        at java.time.Instant.ofEpochSecond(Instant.java:298)
        at com.macasaet.fernet.Token.fromBytes(Token.java:136)
        at com.example.cryptomobile.MainActivity.decrypt(MainActivity.kt:120)

Quite frankly, I have no idea whatsoever what this is supposed to mean or how I can fix it. I don't necessarily need to use the fromBytes method, if the picture decryption works with the fromString method too and my mistake is somewhere else, tell me and I will find it. Otherwise, any help on how I can decrypt pictures and text and/or fix or evade this exception would be greatly appreciated.

If you need any additional information on my code, feel free to tell me, I am not very experienced on StackOverflow. Thanks in advance.

ductTapeIsMagic
  • 113
  • 1
  • 8

0 Answers0