Spaces:
Sleeping

File size: 1,447 Bytes
3916cce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
from PIL import Image
from cryptography.fernet import Fernet
from pathlib import Path

class StegoEmbedder:
    def process(self, encrypted_img_path: Path, secret_img_path: Path, output_dir: Path):
        """
        Embeds a secret image into the LSB of the encrypted cover.
        Returns: Tuple(path_to_stego_img, key3)
        """
        enc_img = Image.open(encrypted_img_path).convert("L")
        sec_img = Image.open(secret_img_path).convert("L")
        
        enc_arr = np.array(enc_img)
        sec_arr = np.array(sec_img)

        # Flatten secret data
        secret_data = sec_arr.flatten().astype(np.uint8)
        secret_bin = ''.join(format(x, '08b') for x in secret_data)
        
        # Key 3: Secret Shuffle Key
        key3 = Fernet.generate_key()

        # LSB Embedding Logic
        h, w = enc_arr.shape
        flat = enc_arr.flatten()
        data_len = len(secret_bin)
        
        # Header: Length of data (16 bits)
        bin_len = format(data_len, '016b')
        payload = bin_len + secret_bin
        payload = payload[:len(flat)] # Safety clip

        new_flat = flat.copy()
        for i, bit in enumerate(payload):
            new_flat[i] = (new_flat[i] & ~1) | int(bit)

        new_arr = new_flat.reshape((h, w))
        
        output_path = output_dir / "stego_image.png"
        Image.fromarray(new_arr).save(output_path)
        
        return output_path, key3.decode()