from PIL import Image import random from Crypto.Util.number import *
definvert_right(m, l, val=''): length = 32 mx = 0xffffffff if val == '': val = mx i, res = 0, 0 while i * l < length: mask = (mx << (length - l) & mx) >> i * l tmp = m & mask m = m ^ tmp >> l & val res += tmp i += 1 return res
definvert_left(m, l, val): length = 32 mx = 0xffffffff i, res = 0, 0 while i * l < length: mask = (mx >> (length - l) & mx) << i * l tmp = m & mask m ^= tmp << l & val res |= tmp i += 1 return res
definvert_temper(m): m = invert_right(m, 18) m = invert_left(m, 15, 4022730752) m = invert_left(m, 7, 2636928640) m = invert_right(m, 11) return m
defclone_mt(record): state = [invert_temper(i) for i in record] gen = random.Random() gen.setstate((3, tuple(state + [0]), None)) return gen
defmakeSourceImg(): colors = long_to_bytes(g.getrandbits(width * height * 24))[::-1] img = Image.new('RGB', (width, height)) x = 0 for i inrange(height): for j inrange(width): img.putpixel((j, i), (colors[x], colors[x + 1], colors[x + 2])) x += 3 return img
height, width = 208, 208
img = Image.open('attach.png') a = [] for i inrange(4): for j inrange(208): p = img.getpixel((j, i)) for pi in p: a.append(pi) M = [] for i inrange(len(a) // 4): px = (a[4 * i + 3] << 24) + (a[4 * i + 2] << 16) + (a[4 * i + 1] << 8) + a[4 * i + 0] M.append(px) g = clone_mt(M)
img1 = makeSourceImg() img2 = Image.open('attach.png') img3 = Image.new("RGB", (width, height)) for i inrange(height): for j inrange(width): p1, p2 = img1.getpixel((j, i)), img2.getpixel((j, i)) img3.putpixel((j, i), tuple([(p1[k] ^ p2[k]) for k inrange(3)])) img3.save('flag.png')