File size: 1,998 Bytes
6a23d11
 
 
5c1c2cf
6a23d11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from Crypto.PublicKey import RSA
from Crypto.Cipher import AES, PKCS1_OAEP
import cv2
import numpy as np 

def to_bin(data):
    if isinstance(data, str):
        return ''.join([ format(ord(i), "08b") for i in data ])
    elif isinstance(data, bytes):
        return ''.join([ format(i, "08b") for i in data ])
    elif isinstance(data, np.ndarray):
        return [ format(i, "08b") for i in data ]
    elif isinstance(data, int) or isinstance(data, np.uint8):
        return format(data, "08b")
    else:
        raise TypeError("Type not supported.")
def decode(image_name,txt=None):
    BGRimage = cv2.imread(image_name)
    image = cv2.cvtColor(BGRimage, cv2.COLOR_BGR2RGB)   
    binary_data = ""
    for row in image:
        for pixel in row:
            r, g, b = to_bin(pixel)
            binary_data += r[-1]
            binary_data += g[-1]
            binary_data += b[-1]
    all_bytes = [ binary_data[i: i+8] for i in range(0, len(binary_data), 8) ]
    decoded_data = ""
    for byte in all_bytes:
        decoded_data += chr(int(byte, 2))
        if decoded_data[-5:] == "=====":
            break
    this = decoded_data[:-5].split("#####",1)[0]
    this = eval(this)
    return this
def decrypt_text(im,in2,seed=None):
    enc_in = decode(im)
    if seed != None and seed != "":
        secret_code=seed
    else: 
        secret_code = " "     
    priv_key = decode(in2)
    private_key = RSA.import_key(priv_key,passphrase=secret_code)
    enc_session_key = enc_in[:private_key.size_in_bytes()]
    end1 = private_key.size_in_bytes()+16
    nonce = enc_in[private_key.size_in_bytes():end1]
    start1=end1+1
    end2 = private_key.size_in_bytes()+32
    start2=end2+1
    tag = enc_in[end1:end2]
    ciphertext = enc_in[end2:]
    cipher_rsa = PKCS1_OAEP.new(private_key)
    session_key = cipher_rsa.decrypt(enc_session_key)
    cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
    data = cipher_aes.decrypt_and_verify(ciphertext, tag)
    return(data.decode("utf-8"))