Skip to content

Instantly share code, notes, and snippets.

@Laurent-Andrieu
Created December 28, 2020 11:13
Show Gist options
  • Save Laurent-Andrieu/33675526e15c79b0305e5c7e09eb1e74 to your computer and use it in GitHub Desktop.
Save Laurent-Andrieu/33675526e15c79b0305e5c7e09eb1e74 to your computer and use it in GitHub Desktop.
Script steganographie: données chiffrés, datés.
"""
Steganography in python 3.9
+ Variable Data length and image size
+ Red colour modification
+ AES 128 CBC encrytion
+ UTF-8 data encoding
+ SHA-256 Checksum
+ Generation date time
"""
import os
import numpy as np
from PIL import Image
import datetime as dt
import hashlib
from base64 import b64encode
from Cryptodome.Cipher import AES
from Cryptodome.Util.Padding import pad
from Cryptodome.Random import get_random_bytes
def sizeof(data):
s = None
if isinstance(data, dict):
s = sum([len(value) for value in data.values()][2:])
elif isinstance(data, tuple):
if isinstance(data[0], dict):
s = sum([len(str(value)) for value in data[0].values()][2:]) + data[1]
elif isinstance(data[1], dict):
s = sum([len(str(value)) for value in data[1].values()][2:]) + data[0]
return s
# FILE SYSTEM
cwd = os.getcwd() + '\\'
image_file = cwd + 'Origine.png'
saved_image = cwd + 'Mod.png'
# DATA
message = b"B"
bin_message = [bin(ord(i))[2:] for i in message.decode('utf-8')]
# AES
key = get_random_bytes(16)
cipher = AES.new(key, AES.MODE_CBC)
ciphertext_bytes = cipher.encrypt(pad(message, AES.block_size))
iv = b64encode(cipher.iv).decode('utf-8')
ct = b64encode(ciphertext_bytes).decode('utf-8')
b_cipher = "".join([bin(ord(i))[2:] for i in ct])
data_length = len(b_cipher)
# HEADER
HEADER_S = {
'version': 0,
'header_length': 0,
'encryption': 0,
'data_length': data_length,
'checksum': int(hashlib.sha1(message).hexdigest(), 16),
'timestamp': int(dt.datetime.now().timestamp())
}
HEADER = {key: str(bin(value)[2:]) for key, value in HEADER_S.items()}
HEADER['header_length'] = str(bin(sizeof(HEADER))[2:])
print(HEADER)
# Bit sequence generation
sequence = []
for value in HEADER.values():
sequence.append(value)
bit_sequence = ''.join(sequence) + b_cipher
# Source Image creation
image_size = sizeof((HEADER, data_length))
source_img = Image.new('RGB', (image_size, image_size), color="white").save(image_file)
vector = np.array(Image.open(image_file))
red = []
# Original image iteration
for l_1 in vector:
for k_1 in l_1:
# Parity substraction
red_parity = k_1[0] % 2
red.append(k_1[0] - red_parity)
# Bit Sequence + Original color vector
red_1 = []
for bit, colour in zip(bit_sequence, red):
red_1.append(colour + int(bit))
# Append
for l_2 in vector:
for k_2, colour_1 in zip(l_2, red_1):
k_2[0] = colour_1
Image.fromarray(vector).save(saved_image)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment