字节阵列到图像视图

发布于 2025-01-25 05:16:59 字数 3183 浏览 3 评论 0原文

我正在尝试使用AES及其成功的工作来对图像进行加密

,但是一旦我获得了最终解密的字节阵列,我就无法设置为

  val bitmap = (imge.drawable as BitmapDrawable).bitmap
  val width = bitmap.width
  val height = bitmap.height
  val decryptedText: ByteArray = cipher.doFinal(bytes)

                
  val configBmp = Bitmap.Config.valueOf(bitmap.config.name)
  val bitmap_tmp = Bitmap.createBitmap(width, height, configBmp)
  val buffer = ByteBuffer.wrap(decryptedText)
  bitmap_tmp.copyPixelsFromBuffer(buffer)

  img.setImageBitmap(bitmap_tmp)

解密的文本,这是解密的字节阵列,一旦我在未加密的图像的帮助下进行了此操作,我就会得到宽度和高度以及使用它将字节阵列转回图像。

但是,我尝试在不使用以下代码的帮助下使用原始图像统计数据,例如高度和长度和位图。

 val decryptedText: ByteArray = cipher.doFinal(bytes)

 val ol = BitmapFactory.decodeByteArray(decryptedText, 0, decryptedText.size)
 img.setImageBitmap(ol)

但是,这不起作用,在日志猫中显示出来: -

D/skia: --- Failed to create image decoder with message 'unimplemented'

应用不会崩溃,但是我无法在图像视图中获得解密的图像,

如何将字节数组转换为位图,因此可以设置图像。

加密功能: -

   fun encryption(imageView: ImageView, name: String, uid: String) {
        val bitmap = (imge.drawable as BitmapDrawable).bitmap

        val size = bitmap.rowBytes * bitmap.height
        val byteBuffer: ByteBuffer = ByteBuffer.allocate(size)
        bitmap.copyPixelsToBuffer(byteBuffer)
        val byteArray = byteBuffer.array()

        Firebase.firestore.collection("profiles").document("YHMauLouORRtrYBV2h4dHJ5A0s72")
            .update("key", "$secretKey1")

        val cipher = Cipher.getInstance("AES")
        val keySpec = SecretKeySpec(secretKey1, "AES")
        cipher.init(Cipher.ENCRYPT_MODE, keySpec)
        val z = cipher.doFinal(byteArray)
        val file = File.createTempFile("$name", null)
        file.writeBytes(z)
        Log.d("first", "quick")
        val storageReference = FirebaseStorage.getInstance()
            .getReference("/image/0XhL4jD4XCemk38rcRkIEjJMgjh2/Aadhar/")
        storageReference.putFile(Uri.fromFile(file))
            .addOnSuccessListener {
                Toast.makeText(context, "Success", Toast.LENGTH_SHORT).show()
            }
    }

解密功能: -

 val bitmap = (imge.drawable as BitmapDrawable).bitmap
        val width = bitmap.width
        val height = bitmap.height
        val cipher = Cipher.getInstance("AES")
        val SecretKeySpec = SecretKeySpec(secretKey1, "AES")
        cipher.init(Cipher.DECRYPT_MODE, SecretKeySpec)

        val storageRef =
            FirebaseStorage.getInstance().reference?.child("/image/0XhL4jD4XCemk38rcRkIEjJMgjh2/Aadhar/")
        val localfile = File.createTempFile("temp", ".txt")
        storageRef.getFile(localfile)
            .addOnSuccessListener {

                val bytes = localfile.readBytes()
                val decryptedText: ByteArray = cipher.doFinal(bytes)

                val configBmp = Bitmap.Config.valueOf(bitmap.config.name)
                val bitmap_tmp = Bitmap.createBitmap(width, height, configBmp)
                val buffer = ByteBuffer.wrap(decryptedText)
                bitmap_tmp.copyPixelsFromBuffer(buffer)


                img.setImageBitmap(bitmap_tmp)

I am trying to Encrypt the Image with Aes and its working successfully

But once I have the final decrypted Byte array I am unable to set to image

  val bitmap = (imge.drawable as BitmapDrawable).bitmap
  val width = bitmap.width
  val height = bitmap.height
  val decryptedText: ByteArray = cipher.doFinal(bytes)

                
  val configBmp = Bitmap.Config.valueOf(bitmap.config.name)
  val bitmap_tmp = Bitmap.createBitmap(width, height, configBmp)
  val buffer = ByteBuffer.wrap(decryptedText)
  bitmap_tmp.copyPixelsFromBuffer(buffer)

  img.setImageBitmap(bitmap_tmp)

decrypted text is the decrypted byte array , Once I have this with the help of unencrypted image I am getting the width and height and using that to turn the byte array back into a image.

However I tried to do without using the stats of original image like height and length and bitmap by the help of below code.

 val decryptedText: ByteArray = cipher.doFinal(bytes)

 val ol = BitmapFactory.decodeByteArray(decryptedText, 0, decryptedText.size)
 img.setImageBitmap(ol)

However this doesn't work and in Log cat it shows:-

D/skia: --- Failed to create image decoder with message 'unimplemented'

App is not crashing but I am unable to get decrypted image in my image view

How do I convert the byte array to bitmap so Image can be set.

Encryption Function:-

   fun encryption(imageView: ImageView, name: String, uid: String) {
        val bitmap = (imge.drawable as BitmapDrawable).bitmap

        val size = bitmap.rowBytes * bitmap.height
        val byteBuffer: ByteBuffer = ByteBuffer.allocate(size)
        bitmap.copyPixelsToBuffer(byteBuffer)
        val byteArray = byteBuffer.array()

        Firebase.firestore.collection("profiles").document("YHMauLouORRtrYBV2h4dHJ5A0s72")
            .update("key", "$secretKey1")

        val cipher = Cipher.getInstance("AES")
        val keySpec = SecretKeySpec(secretKey1, "AES")
        cipher.init(Cipher.ENCRYPT_MODE, keySpec)
        val z = cipher.doFinal(byteArray)
        val file = File.createTempFile("$name", null)
        file.writeBytes(z)
        Log.d("first", "quick")
        val storageReference = FirebaseStorage.getInstance()
            .getReference("/image/0XhL4jD4XCemk38rcRkIEjJMgjh2/Aadhar/")
        storageReference.putFile(Uri.fromFile(file))
            .addOnSuccessListener {
                Toast.makeText(context, "Success", Toast.LENGTH_SHORT).show()
            }
    }

Decryption Function:-

 val bitmap = (imge.drawable as BitmapDrawable).bitmap
        val width = bitmap.width
        val height = bitmap.height
        val cipher = Cipher.getInstance("AES")
        val SecretKeySpec = SecretKeySpec(secretKey1, "AES")
        cipher.init(Cipher.DECRYPT_MODE, SecretKeySpec)

        val storageRef =
            FirebaseStorage.getInstance().reference?.child("/image/0XhL4jD4XCemk38rcRkIEjJMgjh2/Aadhar/")
        val localfile = File.createTempFile("temp", ".txt")
        storageRef.getFile(localfile)
            .addOnSuccessListener {

                val bytes = localfile.readBytes()
                val decryptedText: ByteArray = cipher.doFinal(bytes)

                val configBmp = Bitmap.Config.valueOf(bitmap.config.name)
                val bitmap_tmp = Bitmap.createBitmap(width, height, configBmp)
                val buffer = ByteBuffer.wrap(decryptedText)
                bitmap_tmp.copyPixelsFromBuffer(buffer)


                img.setImageBitmap(bitmap_tmp)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文