Spaces:
Sleeping
Sleeping
Download embbed.py from low-key-007/crypto-api: direct link, hf CLI and curl.
- Browser
- Download file 1.25 kB
-
https://huggingface.co/spaces/low-key-007/crypto-api/resolve/main/embbed.py
- Command line
-
hf download hf://spaces/low-key-007/crypto-api/embbed.py
-
curl -L -o embbed.py https://huggingface.co/spaces/low-key-007/crypto-api/resolve/main/embbed.py
1.25 kB
| import os | |
| import numpy as np | |
| from PIL import Image | |
| from cryptography.fernet import Fernet | |
| def embed_data(encrypted_image_path, secret_image_path): | |
| # Load images | |
| enc_img = Image.open(encrypted_image_path).convert("L") | |
| sec_img = Image.open(secret_image_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) | |
| # Shuffle key for secret | |
| key3 = Fernet.generate_key() | |
| with open("out/keys.txt", "a") as f: | |
| f.write(key3.decode() + "\n") | |
| print("Key for block shuffling: ", key3) | |
| # Embed into encrypted image LSB | |
| h, w = enc_arr.shape | |
| flat = enc_arr.flatten() | |
| data_len = len(secret_bin) | |
| print("Length of secret data: ", data_len) | |
| bin_len = format(data_len, '016b') | |
| payload = bin_len + secret_bin | |
| payload = payload[:len(flat)] | |
| 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)) | |
| Image.fromarray(new_arr).save("reembbedded_image.png") | |
| print("Blocks after embedding are saved to reembbedded_image.png") | |