为什么 hexcode 输出失败?

Why is hexcode output failing?

from os import urandom
from PIL import Image
import io



for y in range((len(array2) -1), -1, -1):  
  decrypted_initial = decrypt(array2[y], key)
  if y > 0:
    decrypted_cipher = decrypt(decrypted_initial, array2[(y -1)])
  if y == 0:
    decrypted_cipher = decrypt(decrypted_initial, fixed_initialization_vector)
  immage_array.append(decrypted_cipher)
immage_array = [x for sets in immage_array for x in sets] #The list is all zeros since the image is all black.


for i in range(1000):
  print (immage_array[i]) #1000 zeroes printed.
if immage_array == myMessage: #Comparing the input hexocde for the encryption and output hexcode from the decryption.
    print("Yay.") #Print successful. 
image = Image.open(io.BytesIO(bytes(immage_array))) #Not sure what this does exactly. I am just trying to get the hexcode to output as an image file.
image.save(output.png)

我正在尝试对图像进行链式分组密码,这是代码的一部分。我已经能够获取图像的十六进制代码,使用链块密码对其进行加密,然后对其进行解密。然后我将解密的十六进制代码与原始输入的十六进制代码进行比较,我得到了成功的匹配。十六进制代码作为列表而不是字节数组返回,但 if 语句仍然将它们识别为相等。不过,我无法将十六进制代码转换回图像。

我收到这个错误。 OSError:无法识别 0x039F1090 处的图像文件 <_io.BytesIO 对象>

出于测试目的,图像全是黑色像素,以便于确认我已确认与输入相同的十六进制代码的完整性。

是什么阻止我将十六进制代码转回图像?我曾尝试到处寻找有效的示例,但我一直无法完全理解它为何有效。这是我的逻辑。

immage_array 是一个列表 == (0, 0, 0, 0, ..., n)

bytes() #把所有整数0的immage_array列表变成一个全部由x00组成的字节数组。

io.BytesIO() Image.open() 函数操作字节数组所需的函数。

然后我将字节数组作为对象存储在变量图像中。

image.save(output.png) 保存函数将文件名作为参数。使用图像对象。

老实说,我已经尽最大努力找出逻辑错误的地方。如果能解释一下如何将 (0, 0, 0, ..., n) 的列表转换为图像,我将不胜感激。是的,我知道,我已经看到其他关于堆栈溢出的例子。我想知道为什么我的实施失败了。

我刚刚重新阅读了您的问题。如果您的 immage_array 是 [0, 0, 0...],则无法执行此操作。那不是图像,而是位图(细微差别)。使用 PIL.Image.frombytes 构造函数。

如果要加密图像,只需加密整个内容,而不是提取文件内容,将其转换为位图,然后encrypting/decrypting。当您将其转换为位图时,对于初学者来说,它会多一个数量级的数据,其次,您会丢失有关图像格式(RGB、ARGB、单色等)的信息

正如您现在透露的那样,您正在使用 PIL.Image.getdata 方法。这个 returns 是一个一维列表,那你怎么知道另一端的 width/height 呢?也就是说,如果您的图像是 640x480,从另一端您无法判断它是 480x640 还是 307200x1