Advertisement
DoromaAnim

st

Dec 10th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. import random
  2. from matplotlib import pyplot as plt
  3. from PIL import Image
  4.  
  5.  
  6. def str_to_bitlst(strr):
  7.     """ Zwraca listę bitów w binarnym zapisie napisu"""
  8.     res = []
  9.     for i in strr:
  10.         res.extend(list(map(int, bin(ord(i))[2:].zfill(8))))
  11.     return res
  12.  
  13.  
  14. def bitgen(lst):
  15.     for i in range(len(lst) // 2):
  16.         yield 2*lst[2 * i] + lst[2 * i + 1]
  17.  
  18.  
  19. def encrypt(text, image_name):
  20.     XOR = [0, 1, 2, 3]
  21.     binary_text = str_to_bitlst(text)
  22.  
  23.     image = Image.open(image_name)
  24.     image = image.convert('RGB')
  25.     width, height = image.size
  26.     image1 = image.copy()
  27.     image2 = image.copy()
  28.  
  29.     generator = bitgen(binary_text)
  30.  
  31.     breaks = 0
  32.     for i in range(height):
  33.         for j in range(width):
  34.             pixel_id = (i, j)
  35.             image_pixel = list(image.getpixel(pixel_id))
  36.             image1_pixel = image_pixel.copy()
  37.             image2_pixel = image_pixel.copy()
  38.             for k in range(3):
  39.                 try:
  40.                     bits_to_encrypt = next(generator)
  41.                     xor = random.choice(XOR)
  42.                     tmp = image_pixel[k] ^ (image_pixel[k] & 3)
  43.                     image1_pixel[k] = tmp ^ xor
  44.                     image2_pixel[k] = tmp ^ bits_to_encrypt ^ xor
  45.                 except StopIteration:
  46.                     breaks = 1
  47.                 if breaks:
  48.                     break
  49.             image1.putpixel(pixel_id, tuple(image1_pixel))
  50.             image2.putpixel(pixel_id, tuple(image2_pixel))
  51.             if breaks:
  52.                 break
  53.         if breaks:
  54.             break
  55.  
  56.     image1.save(image_name[:-4] + '1' + image_name[-4:])
  57.     image2.save(image_name[:-4] + '2' + image_name[-4:])
  58.  
  59.  
  60. def decrypt(image_name1, image_name2):
  61.     image1 = Image.open(image_name1)
  62.     image2 = Image.open(image_name2)
  63.     width, height = image1.size
  64.     decrypted_bits = []
  65.  
  66.     for i in range(height):
  67.         for j in range(width):
  68.             image1_pixel = image1.getpixel((i, j))
  69.             image2_pixel = image2.getpixel((i, j))
  70.             for k, l in zip(image1_pixel, image2_pixel):
  71.                 tmp = k ^ l
  72.                 decrypted_bits.append(bin(tmp)[2:].zfill(2))
  73.  
  74.     result = []
  75.     for i in range((len(decrypted_bits) + 3) // 4):
  76.         result.append(chr(int("".join(decrypted_bits[4 * i: 4 * (i + 1)]), 2)))
  77.  
  78.     return "".join(result).strip()
  79.  
  80.  
  81. def show_images(*image_names):
  82.     images = [Image.open(i) for i in image_names]
  83.     fig, ax = plt.subplots(1, len(image_names))
  84.  
  85.     for idx, i in enumerate(images):
  86.         ax[idx].imshow(i)
  87.         ax[idx].axis('off')
  88.  
  89.     plt.show()
  90.  
  91.  
  92. def main():
  93.     image_name = input('Podaj nazwę obrazka w którym ma zostać ukryty tekst: ')
  94.     text = input('Podaj napis, który ma zostać ukryty: ')
  95.     encrypt(text, image_name)
  96.     show_images(image_name, image_name[:-4] + '1' + image_name[-4:],
  97.                 image_name[:-4] + '2' + image_name[-4:])
  98.  
  99.  
  100. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement