我想加密图像中的数据,但生成的密文仍然是有效的图像.我在python中使用AES加密图像然后,我替换文件中的标头,但是窗口无法打开加密图像.
码
def encrypt_file(self, in_filename, out_filename):
filesize = os.path.getsize(in_filename)
iv = Random.new().read(AES.block_size)
cipher = AES.new(self.key, AES.MODE_ECB, iv)
chunksize = 64 * 1024
with open(in_filename, 'rb') as infile:
with open(out_filename, 'wb') as outfile:
outfile.write(struct.pack('<Q', filesize))
outfile.write(iv)
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += ' ' * (16 - len(chunk) % 16)
cifrado = base64.b64encode(cipher.encrypt(chunk))
print cifrado
outfile.write(cipher.encrypt(chunk))
Run Code Online (Sandbox Code Playgroud)
我想要这个效果: 欧洲央行企鹅