Omnibus/RSA-Encryption-dev
1
1import base642import cv23import numpy as np4#import pandas as pd5from PIL import Image6import os7import uuid as uniq8from qr import make_qr9import math10 11 12def to_bin(data):13 """Convert `data` to binary format as string"""14 if isinstance(data, str):15 return ''.join([ format(ord(i), "08b") for i in data ])16 elif isinstance(data, bytes):17 return ''.join([ format(i, "08b") for i in data ])18 elif isinstance(data, np.ndarray):19 return [ format(i, "08b") for i in data ]20 elif isinstance(data, int) or isinstance(data, np.uint8):21 return format(data, "08b")22 else:23 raise TypeError("Type not supported.")24def decode(image_name,txt=None):25 BGRimage = cv2.imread(image_name)26 image = cv2.cvtColor(BGRimage, cv2.COLOR_BGR2RGB) 27 binary_data = ""28 for row in image:29 for pixel in row:30 r, g, b = to_bin(pixel)31 binary_data += r[-1]32 binary_data += g[-1]33 binary_data += b[-1]34 all_bytes = [ binary_data[i: i+8] for i in range(0, len(binary_data), 8) ]35 decoded_data = ""36 for byte in all_bytes:37 decoded_data += chr(int(byte, 2))38 if decoded_data[-5:] == "=====":39 break40 this = decoded_data[:-5].split("#####",1)[0]41 this = eval(this)42 enc_in=this43 return this44 45def encode(image_name, secret_data,txt=None):46 BGRimage = cv2.imread(image_name)47 image = cv2.cvtColor(BGRimage, cv2.COLOR_BGR2RGB) 48 n_bytes = image.shape[0] * image.shape[1] * 3 // 849 print("[*] Maximum bytes to encode:", n_bytes)50 secret_data1=secret_data51 #secret_data1=f'{secret_data}#{resultp}'52 53 while True:54 if len(secret_data1)+5 < (n_bytes):55 secret_data1 = f'{secret_data1}#####'56 elif len(secret_data1)+5 >= (n_bytes):57 break 58 secret_data = secret_data1 59 if len(secret_data) > n_bytes:60 return image_name, gr.Markdown.update("""<center><h3>Input image is too large""")61 secret_data += "====="62 data_index = 063 binary_secret_data = to_bin(secret_data)64 data_len = len(binary_secret_data)65 for row in image:66 for pixel in row:67 r, g, b = to_bin(pixel)68 if data_index < data_len:69 pixel[0] = int(r[:-1] + binary_secret_data[data_index], 2)70 data_index += 171 if data_index < data_len:72 pixel[1] = int(g[:-1] + binary_secret_data[data_index], 2)73 data_index += 174 if data_index < data_len:75 pixel[2] = int(b[:-1] + binary_secret_data[data_index], 2)76 data_index += 177 if data_index >= data_len:78 break79 return image80def conv_im(qr_link,data):81 uniqnum = uniq.uuid4()82 83 byte_size = len(data)84 print (f'bytes:{byte_size}')85 data_pixels = byte_size*486 print (f'pixels:{data_pixels}')87 #data_sq=data_pixels/288 data_sq = int(math.sqrt(data_pixels))89 data_pad = data_sq+10090 print (f'square image:{data_pad}x{data_pad}')91 92 qr_im = make_qr(txt=qr_link)93 img1 = Image.open(qr_im)94 imgw = img1.size[0] 95 imgh = img1.size[1]96 print (f'qr Size:{img1.size}')97 #img1.thumbnail((imgw*4,imgh*4), Image.Resampling.LANCZOS)98 img1 = img1.resize((int(data_pad),int(data_pad)), Image.Resampling.LANCZOS)99 print (img1.size)100 img1.save(f'tmpim{uniqnum}.png')101 102 with open(f'tmpim{uniqnum}.png', "rb") as image_file:103 encoded_string = base64.b64encode(image_file.read())104 image_file.close()105 im_out = encode(f'tmpim{uniqnum}.png',data)106 return im_out