return pic,size,config defoutputImage(path,pic,size): im = Image.new('P', size,'white') pixels = im.load() for i inrange(im.size[0]): for j inrange(im.size[1]): pixels[i,j] = (int(pic[j][i]))
p0 = random.randint(100,104) c0 = random.randint(200,204) config = (p0,c0) for x inrange(w): for y inrange(h): k = int(round(const*r3))%256 k = bin(k)[2:].ljust(8,'0') k = int(k[p0%8:]+k[:p0%8],2) r3 = f3(r3)
from PIL import Image import numpy as np from decimal import * getcontext().prec = 20
deff(x): # It is based on logistic map in chaotic systems # The parameter r takes the largest legal value assert (x >= 0) assert (x <= 1) r = 4 x = r * x * (1 - x) return x
im = Image.open('flag_enc.bmp') pixels = im.load() size = im.size w, h = size print(size) # print(np.array(im)) pic = np.zeros((h, w)) const = 10 ** 14 r1 = Decimal('0.478706063089473894123') r2 = Decimal('0.613494245341234672318') r3 = Decimal('0.946365754637812381837') for i inrange(im.size[0]): for j inrange(im.size[1]): pic[j][i] = pixels[i, j] print(pic)
for i inrange(200): r1 = f(r1) r2 = f(r2) r3 = f(r3) p0 = range(100, 105) c0 = range(200, 205) config = [] # 列出(p0, c0)的所有情况 for p in p0: for c in c0: config.append((p, c))
# 记录原先的坐标x1, y1 list_x1 = np.zeros((h, w)) list_y1 = np.zeros((h, w)) for x inrange(w): for y inrange(h): x1 = int(round(const * r1)) % w y1 = int(round(const * r2)) % h list_x1[y][x] = x1 list_y1[y][x] = y1 r1 = f(r1) r2 = f(r2)
list_k = [] for conf in config: arrk = np.zeros((h, w)) k = 0 p0 = conf[0] c0 = conf[1] for x inrange(w): for y inrange(h): k = int(round(const * r3)) % 256 k = bin(k)[2:].ljust(8, '0') k = int(k[p0 % 8:] + k[:p0 % 8], 2) r3 = f(r3) p0 = int(pic[y, x]) arrk[y][x] = k list_k.append(arrk) print(list_k)
list_p = [] for conf in config: x1 = 0 y1 = 0 picture = pic.copy() arrk = list_k[config.index(conf)] p0 = conf[0] c0 = conf[1] for x inrange(w-1, -1, -1): for y inrange(h-1, -1, -1): k = int(arrk[y][x]) p0 = pic[y][x] if (y-1) >= 0: c0 = int(pic[y-1][x]) elif (y - 1) < 0and (x-1) < 0: pass else: c0 = int(pic[h-1][x-1])
k_p = k ^ (int(pic[y][x])) ^ c0 k_p -= k if k_p < 0: k_p += 256 picture[y][x] = k_p
for x inrange(w - 1, -1, -1): for y inrange(h - 1, -1, -1): x1 = int(list_x1[y][x]) y1 = int(list_y1[y][x]) tmp = picture[y1][x1] picture[y1][x1] = picture[y][x] picture[y][x] = tmp list_p.append(picture) print(picture)
for k inrange(len(list_p)): im = Image.new('P', size, 'white') pixels = im.load() for i inrange(im.size[0]): for j inrange(im.size[1]): pixels[i, j] = (int(list_p[k][j][i])) im.save('p' + str(k) + '.bmp')