'AES encryption of Image In Kotlin

I am trying to encrypt and decrypt the image by AES in Kotlin

Key Generation Function-

 override suspend fun key_genration(callback: Enc) {
    val keygenerator = KeyGenerator.getInstance("AES")
    keygenerator.init(128)
    val secretKey: Key = keygenerator.generateKey()
    val secretKey1 = secretKey.encoded
    val result = Firebase.firestore.collection("profiles").document("YHMauLouORRtrYBV2h4dHJ5A0s72").update("key","$secretKey1")

}

Encryption Function -

   override suspend fun encryption(imageView: ImageView, Key: ByteArray, name: String, uid: String) {

    val bitmap = (imageView.drawable as BitmapDrawable).bitmap
    val baos = ByteArrayOutputStream()
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos) // bm is the bitmap object
    val ba: ByteArray = baos.toByteArray()

    val cipher = Cipher.getInstance("AES")

    val keySpec = SecretKeySpec(Key, "AES")
   Log.d("key11119","$keySpec")

    cipher.init(Cipher.ENCRYPT_MODE, keySpec)
    val txt = cipher.doFinal(ba)
    Log.d("poott0","$txt")
    val p =  txt.toString(Charsets.ISO_8859_1)

    Log.d("poott","$p")


  //  val encryptText = String(txt, Charset.defaultCharset())
    val file = File.createTempFile("$name", ".txt")
    file.writeText(p)
    //val storageReference = FirebaseStorage.getInstance().getReference("image/$uid/$name")
    val storageReference = FirebaseStorage.getInstance()
        .getReference("/image/0XhL4jD4XCemk38rcRkIEjJMgjh2/Aadhar/")

    storageReference.putFile(Uri.fromFile(file))
}

Decryption Function -

 val img = view.findViewById<ImageView>(R.id.imageView2)
    Firebase.firestore.collection("profiles").whereEqualTo("UID", "YHMauLouORRtrYBV2h4dHJ5A0s72").get()
            .addOnSuccessListener {
                val key = it.first().toObject(docretreving::class.java).key
                Log.d("key111","$key")
                Log.d("pooptt","Success")
                val storageRef =
                    FirebaseStorage.getInstance().reference?.child("/image/0XhL4jD4XCemk38rcRkIEjJMgjh2/Aadhar/")
                val localfile = File.createTempFile("temp",".txt")
                storageRef.getFile(localfile)
                    .addOnSuccessListener {
                        Log.d("pooptt","Success")
                        val inputStream: InputStream = localfile.inputStream()
                        val inputString = inputStream.bufferedReader().use { it.readText() }
                        println(inputString)
                        Log.d("pooptt", inputString)
                        val key1 = "mxkLZmSFE1aKWzr6JyybjQ==".toByteArray()
                        Log.d("key111","$key1")
                        val keySpec = SecretKeySpec(key1, "AES")
                        val isro= inputString.toByteArray(Charsets.ISO_8859_1)
                        val cipher = Cipher.getInstance("AES")
                        cipher.init(Cipher.DECRYPT_MODE,keySpec)
                        var decryptedText : ByteArray = cipher.doFinal(isro)
                        Log.d("key110","$decryptedText")
                        val baos = ObjectInputStream(ByteArrayInputStream(decryptedText))
                        val bitmap = baos.readObject() as Bitmap
                        img.setImageBitmap(bitmap)

                    }



            }

It says invalid Block size in decryption function , I am not sure if I am doing this correct. The key generating function the key and gives to encryption function[For now I was just testing with 1 key converted to base64 ] Thanks in Advance



Solution 1:[1]

You should not put the ciphertext through a buffered reader, which handles text and may change binary data. Files and ciphertext both consist of bytes, you should treat them as such and not convert them to text. If you need text, use base 64.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Maarten Bodewes