1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| from secret import key flag = b'NSSCTF{******FAKE*********}' enc_c = b'' for i in range(len(key)): enc_c += (key[i]^(flag[i%7])).to_bytes(1,'big') print(f'enc_c={enc_c}')
from PIL import Image class Depart: def get_pixel(self, image): image = Image.open(image) imageRGB = image.convert('RGB') width=image.size[0] height=image.size[1] imageBin='' for i in range(width): for j in range(height): r, g, b = imageRGB.getpixel((i, j)) imageBin+='{:08b}'.format(r) imageBin+='{:08b}'.format(g) imageBin+='{:08b}'.format(b) return imageBin def departBin(self, bin_data): imageBinList=[] data_length=len(bin_data) while data_length>=64: imageBinList.append(bin_data[:64]) data_length-=64 bin_data=bin_data[64:] if data_length == 0: padding_byte_size=8 padding_data=('{:08b}'.format(padding_byte_size))*padding_byte_size imageBinList.append(padding_data) else: padding_length = 64 - data_length padding_byte_size = (padding_length)//8 padding_data = ('{:08b}'.format(padding_byte_size))*padding_byte_size imageBinList.append(bin_data + padding_data) return imageBinList def make_cipher(self, imageBinList): f=open('cipher.txt','w') for i in imageBinList: f.write(i+'\n') f.close()
from Crypto.Cipher import AES from Crypto.Util.Padding import * from PIL import Image
class Convert: def encrypt_image(self, image_path, key): image = Image.open(image_path) pixels = image.tobytes() cipher = AES.new(key, AES.MODE_ECB) encrypted_pixels = cipher.encrypt(pad(pixels, AES.block_size)) encrypted_image = Image.frombytes(image.mode, image.size, encrypted_pixels) return encrypted_image def run(self): image_path = "flag.png" encrypted_image = self.encrypt_image(image_path, key) encrypted_image.save("cipher.png")
import random class Same: def generate_random_image(self, image_path): cipher_image = Image.open(image_path) width, height = cipher_image.size random_image = Image.new("RGB", (width, height)) random_pixels = random_image.load() for x in range(width): for y in range(height): red = random.randint(0, 255) green = random.randint(0, 255) blue = random.randint(0, 255) random_pixels[x, y] = (red, green, blue) random_image.save("random_image.png")
def drawNewYearPicture(): convert = Convert() convert.run()
depart = Depart() image_dir = "cipher.png" bin_data = depart.get_pixel(image_dir) imageBinList = depart.departBin(bin_data) depart.make_cipher(imageBinList)
getRandom = Same() getRandom.generate_random_image(image_dir)
if __name__ == '__main__': drawNewYearPicture()
|