请根据以下代码,编写一个python脚本将被加密的图片解密出来:from PIL import Image from Crypto.Util.number import * from random import shuffle, randint, getrandbits flagImg = Image.open('flag.png') width = flagImg.width height = flagImg.height def makeSourceImg(): colors = long_to_bytes(getrandbits(width * height * 24))[::-1] img = Image.new('RGB', (width, height)) x = 0 for i in range(height): for j in range(width): img.putpixel((j, i), (colors[x], colors[x + 1], colors[x + 2])) x += 3 return img def xorImg(keyImg, sourceImg): img = Image.new('RGB', (width, height)) for i in range(height): for j in range(width): p1, p2 = keyImg.getpixel((j, i)), sourceImg.getpixel((j, i)) img.putpixel((j, i), tuple([(p1[k] ^ p2[k]) for k in range(3)])) return img """ source文件夹下面的图片生成过程: def makeImg(): colors = list(long_to_bytes(getrandbits(width * height * 23)).zfill(width * height * 24)) shuffle(colors) colors = bytes(colors) img = Image.new('RGB', (width, height)) x = 0 for i in range(height): for j in range(width): img.putpixel((j, i), (colors[x], colors[x + 1], colors[x + 2])) x += 3 return img for i in range(15): im = makeImg() im.save(f"./source/picture{i}.png") """ n1 = makeSourceImg() n2 = makeSourceImg() n3 = makeSourceImg() nonce = index = list(range(16)) shuffle(index) e=0 """ 这里flag.png已经提前被保存在source文件夹下了,文件名也是picture{xx}.png """ for i in index: im = Image.open(f"source/picture{i}.png") key = nonce[randint(0, 2)] encImg = xorImg(key, im) encImg.save(f'pics/enc{e}.png') e+=1
时间: 2024-04-13 19:30:02 浏览: 18
下面是一个用于解密被加密图片的Python脚本:
```python
from PIL import Image
def xorImg(keyImg, sourceImg):
img = Image.new('RGB', sourceImg.size)
width, height = sourceImg.size
for i in range(height):
for j in range(width):
p1, p2 = keyImg.getpixel((j, i)), sourceImg.getpixel((j, i))
img.putpixel((j, i), tuple([(p1[k] ^ p2[k]) for k in range(3)]))
return img
def decryptImages():
width = 0
height = 0
encImages = []
# 获取加密图片的宽度和高度
encImg = Image.open('pics/enc0.png')
width = encImg.width
height = encImg.height
# 解密每张加密图片,并保存到dec文件夹中
for i in range(16):
encImgPath = f'pics/enc{i}.png'
encImg = Image.open(encImgPath)
keyImgPath = f'source/picture{i}.png'
keyImg = Image.open(keyImgPath)
decImg = xorImg(keyImg, encImg)
decImgPath = f'dec/dec{i}.png'
decImg.save(decImgPath)
encImages.append(decImgPath)
return encImages
# 解密图片并获取解密后的图片路径列表
decryptedImages = decryptImages()
print("解密完成!解密后的图片保存在dec文件夹中。")
print("解密后的图片路径列表:")
for imgPath in decryptedImages:
print(imgPath)
```
这个脚本会读取加密图片和对应的源图片,通过`xorImg()`函数进行异或操作,得到解密后的图片。解密后的图片会保存在`dec`文件夹中,并返回解密后的图片路径列表。
你可以将该脚本保存为一个Python文件,然后运行它来解密被加密的图片。解密后的图片将保存在`dec`文件夹中。