在 android 中显示图像的加密字节数组
Displaying an encrypted byte array of an image in android
我有一张图像,我将其转换为字节数组。然后我用 AES 加密这个字节数组,我希望显示一个视觉结果来表示这个加密。
问题是所有 header 和元信息也被加密,因此传递给 byteToImage() 的加密字节数组未被识别为图像的有效表示,即 decodeByteArray() returns null.
我曾尝试切断原始图像的前 512 个字节并将其追加到加密字节数组的开头,希望这将恢复 header 信息 - 但它没有'没用。我有 .png 和 .bmp 图像。我理想中想要的是一种在 android 中表示 RAW 图像并逐字节加密此信息的方法 - 无需 fiddle 周围 headers 等
非常感谢任何帮助。
private static byte[] imageToBytes(ImageView iv){
byte[] imageInByte = null;
Bitmap originalImage;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
originalImage = drawable.getBitmap();
originalImage.compress(Bitmap.CompressFormat.PNG, 0, baos); // was 70
imageInByte = baos.toByteArray();
return imageInByte;
}
private static Bitmap bytesToImage(byte data[]) {
// byte[] x = Base64.decode(data, Base64.DEFAULT); using x in place of data also fails
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
return bmp;
}
你的想法行不通。如果你只加密位图字节数组的很多字节,你将破坏它的结构,之后你就不再有位图了。所以你不能显示它。如果您希望能够显示 'encrypted' 图像,请按像素进行。看看 Bitmap.getPixel()/setPixel() 或 Bitmap.getPixels()/setPixels()。
我有一张图像,我将其转换为字节数组。然后我用 AES 加密这个字节数组,我希望显示一个视觉结果来表示这个加密。
问题是所有 header 和元信息也被加密,因此传递给 byteToImage() 的加密字节数组未被识别为图像的有效表示,即 decodeByteArray() returns null.
我曾尝试切断原始图像的前 512 个字节并将其追加到加密字节数组的开头,希望这将恢复 header 信息 - 但它没有'没用。我有 .png 和 .bmp 图像。我理想中想要的是一种在 android 中表示 RAW 图像并逐字节加密此信息的方法 - 无需 fiddle 周围 headers 等
非常感谢任何帮助。
private static byte[] imageToBytes(ImageView iv){
byte[] imageInByte = null;
Bitmap originalImage;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
originalImage = drawable.getBitmap();
originalImage.compress(Bitmap.CompressFormat.PNG, 0, baos); // was 70
imageInByte = baos.toByteArray();
return imageInByte;
}
private static Bitmap bytesToImage(byte data[]) {
// byte[] x = Base64.decode(data, Base64.DEFAULT); using x in place of data also fails
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
return bmp;
}
你的想法行不通。如果你只加密位图字节数组的很多字节,你将破坏它的结构,之后你就不再有位图了。所以你不能显示它。如果您希望能够显示 'encrypted' 图像,请按像素进行。看看 Bitmap.getPixel()/setPixel() 或 Bitmap.getPixels()/setPixels()。