Re-Arragement of the whole folder
Before Width: | Height: | Size: 97 KiB |
Before Width: | Height: | Size: 501 KiB |
Before Width: | Height: | Size: 799 KiB |
|
@ -1,153 +1,153 @@
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
|
||||||
|
|
||||||
class docx_steg:
|
class docx_steg:
|
||||||
def __init__(self, docx_file: str = "testfile.docx", bits_to_hide: list[int] = None) -> None:
|
def __init__(self, docx_file: str = "testfile.docx", bits_to_hide: list[int] = None) -> None:
|
||||||
"""
|
"""
|
||||||
Initialize the class
|
Initialize the class
|
||||||
:param docx_file: PathName of the docx file to encode or decode
|
:param docx_file: PathName of the docx file to encode or decode
|
||||||
:type docx_file: str
|
:type docx_file: str
|
||||||
:param bits_to_hide: Bit to hide the data in (1 - LSB to 8 - MSB)
|
:param bits_to_hide: Bit to hide the data in (1 - LSB to 8 - MSB)
|
||||||
:type bits_to_hide: list[int]
|
:type bits_to_hide: list[int]
|
||||||
"""
|
"""
|
||||||
self.docx_file = docx_file
|
self.docx_file = docx_file
|
||||||
self.bits_to_hide = [8 - bit_pos for bit_pos in bits_to_hide] if bits_to_hide else [1] # Default is LSB
|
self.bits_to_hide = [8 - bit_pos for bit_pos in bits_to_hide] if bits_to_hide else [1] # Default is LSB
|
||||||
self.delimiter = "=====" # Delimiter to indicate the end of the secret data
|
self.delimiter = "=====" # Delimiter to indicate the end of the secret data
|
||||||
|
|
||||||
def encode(self, secret_data: str = "Hello World") -> bytes:
|
def encode(self, secret_data: str = "Hello World") -> bytes:
|
||||||
"""
|
"""
|
||||||
Encode the secret data into the docx file
|
Encode the secret data into the docx file
|
||||||
|
|
||||||
:param secret_data: String of data to hide
|
:param secret_data: String of data to hide
|
||||||
|
|
||||||
:return: Encoded docx file as bytes
|
:return: Encoded docx file as bytes
|
||||||
:rtype: bytes
|
:rtype: bytes
|
||||||
"""
|
"""
|
||||||
|
|
||||||
with open(self.docx_file, "rb") as f:
|
with open(self.docx_file, "rb") as f:
|
||||||
data = f.read()
|
data = f.read()
|
||||||
|
|
||||||
# Max Bytes to encode
|
# Max Bytes to encode
|
||||||
n_bytes = len(data) // 8
|
n_bytes = len(data) // 8
|
||||||
|
|
||||||
# Check if secret data can be encoded into docx file
|
# Check if secret data can be encoded into docx file
|
||||||
# print(f"Secret data length: {len(secret_data)}, Max data length: {n_bytes}")
|
# print(f"Secret data length: {len(secret_data)}, Max data length: {n_bytes}")
|
||||||
if len(secret_data) > n_bytes:
|
if len(secret_data) > n_bytes:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"[-] Error: Binary Secret data length {len(secret_data)} is greater than data length {n_bytes}")
|
f"[-] Error: Binary Secret data length {len(secret_data)} is greater than data length {n_bytes}")
|
||||||
|
|
||||||
# Convert secret data to binary
|
# Convert secret data to binary
|
||||||
binary_secret_data = self.to_bin(secret_data)
|
binary_secret_data = self.to_bin(secret_data)
|
||||||
|
|
||||||
# Add delimiter
|
# Add delimiter
|
||||||
binary_secret_data += self.to_bin(self.delimiter)
|
binary_secret_data += self.to_bin(self.delimiter)
|
||||||
|
|
||||||
data_index = 0
|
data_index = 0
|
||||||
encoded_data = bytearray()
|
encoded_data = bytearray()
|
||||||
|
|
||||||
print(f"[+] Starting encoding...")
|
print(f"[+] Starting encoding...")
|
||||||
# Encode data into docx
|
# Encode data into docx
|
||||||
for byte in data:
|
for byte in data:
|
||||||
if data_index >= len(binary_secret_data):
|
if data_index >= len(binary_secret_data):
|
||||||
encoded_data.append(byte)
|
encoded_data.append(byte)
|
||||||
else:
|
else:
|
||||||
for bit_pos in self.bits_to_hide:
|
for bit_pos in self.bits_to_hide:
|
||||||
if data_index < len(binary_secret_data):
|
if data_index < len(binary_secret_data):
|
||||||
byte = list(format(byte, "08b"))
|
byte = list(format(byte, "08b"))
|
||||||
byte = list(byte)
|
byte = list(byte)
|
||||||
byte[bit_pos] = binary_secret_data[data_index]
|
byte[bit_pos] = binary_secret_data[data_index]
|
||||||
data_index += 1
|
data_index += 1
|
||||||
# Convert byte back to int
|
# Convert byte back to int
|
||||||
byte = int("".join(byte), 2)
|
byte = int("".join(byte), 2)
|
||||||
encoded_data.append(byte)
|
encoded_data.append(byte)
|
||||||
|
|
||||||
print(f"[+] Encoding completed successfully.")
|
print(f"[+] Encoding completed successfully.")
|
||||||
return bytes(encoded_data)
|
return bytes(encoded_data)
|
||||||
|
|
||||||
def decode(self) -> str:
|
def decode(self) -> str:
|
||||||
"""
|
"""
|
||||||
Decode the secret data from the docx file
|
Decode the secret data from the docx file
|
||||||
:return: Decoded secret data
|
:return: Decoded secret data
|
||||||
:rtype: str
|
:rtype: str
|
||||||
"""
|
"""
|
||||||
with open(self.docx_file, "rb") as f:
|
with open(self.docx_file, "rb") as f:
|
||||||
data = f.read()
|
data = f.read()
|
||||||
|
|
||||||
binary_data = ""
|
binary_data = ""
|
||||||
print(f"[+] Starting decoding...")
|
print(f"[+] Starting decoding...")
|
||||||
for byte in data:
|
for byte in data:
|
||||||
# Add byte to binary data
|
# Add byte to binary data
|
||||||
# Convert byte from int to binary string
|
# Convert byte from int to binary string
|
||||||
byte = format(byte, "08b")
|
byte = format(byte, "08b")
|
||||||
# Pad byte with 0's to make sure it has 8 bits
|
# Pad byte with 0's to make sure it has 8 bits
|
||||||
byte = "0" * (8 - len(byte)) + byte
|
byte = "0" * (8 - len(byte)) + byte
|
||||||
for bit_pos in self.bits_to_hide:
|
for bit_pos in self.bits_to_hide:
|
||||||
binary_data += byte[bit_pos]
|
binary_data += byte[bit_pos]
|
||||||
|
|
||||||
all_bytes = [binary_data[i: i + 8] for i in range(0, len(binary_data), 8)]
|
all_bytes = [binary_data[i: i + 8] for i in range(0, len(binary_data), 8)]
|
||||||
|
|
||||||
decoded_data = ""
|
decoded_data = ""
|
||||||
for byte in all_bytes:
|
for byte in all_bytes:
|
||||||
decoded_data += chr(int(byte, 2))
|
decoded_data += chr(int(byte, 2))
|
||||||
if decoded_data[-len(self.delimiter):] == self.delimiter:
|
if decoded_data[-len(self.delimiter):] == self.delimiter:
|
||||||
break
|
break
|
||||||
|
|
||||||
print(f"[+] Decoding completed successfully.")
|
print(f"[+] Decoding completed successfully.")
|
||||||
# Return the decoded data
|
# Return the decoded data
|
||||||
return decoded_data[:-len(self.delimiter)]
|
return decoded_data[:-len(self.delimiter)]
|
||||||
|
|
||||||
def to_bin(self, data: str) -> str | list[str]:
|
def to_bin(self, data: str) -> str | list[str]:
|
||||||
"""
|
"""
|
||||||
Convert text file data to binary format as string
|
Convert text file data to binary format as string
|
||||||
:param data: String
|
:param data: String
|
||||||
:type data: str
|
:type data: str
|
||||||
:return: Binary Data: String | List[String]
|
:return: Binary Data: String | List[String]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if isinstance(data, str):
|
if isinstance(data, str):
|
||||||
return ''.join([format(ord(i), "08b") for i in data])
|
return ''.join([format(ord(i), "08b") for i in data])
|
||||||
elif isinstance(data, bytes) or isinstance(data, np.ndarray):
|
elif isinstance(data, bytes) or isinstance(data, np.ndarray):
|
||||||
return [format(i, "08b") for i in data]
|
return [format(i, "08b") for i in data]
|
||||||
elif isinstance(data, int) or isinstance(data, np.unit8):
|
elif isinstance(data, int) or isinstance(data, np.unit8):
|
||||||
return format(data, "08b")
|
return format(data, "08b")
|
||||||
else:
|
else:
|
||||||
raise TypeError("Type not supported")
|
raise TypeError("Type not supported")
|
||||||
|
|
||||||
def from_bin(self, data: str) -> str:
|
def from_bin(self, data: str) -> str:
|
||||||
"""
|
"""
|
||||||
Convert binary `data` back to the original format
|
Convert binary `data` back to the original format
|
||||||
:param data: String
|
:param data: String
|
||||||
:type data: str
|
:type data: str
|
||||||
:return: Original Data: String
|
:return: Original Data: String
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return ''.join([chr(int(data[i:i + 8], 2)) for i in range(0, len(data), 8)])
|
return ''.join([chr(int(data[i:i + 8], 2)) for i in range(0, len(data), 8)])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
with open("secret_data.txt", "r") as f:
|
with open("secret_data.txt", "r") as f:
|
||||||
secret_data = f.read()
|
secret_data = f.read()
|
||||||
|
|
||||||
bits_to_hide = np.random.choice(range(1, 9), np.random.randint(1, 9), replace=False)
|
bits_to_hide = np.random.choice(range(1, 9), np.random.randint(1, 9), replace=False)
|
||||||
bits_to_hide = list(bits_to_hide)
|
bits_to_hide = list(bits_to_hide)
|
||||||
bits_to_hide.sort()
|
bits_to_hide.sort()
|
||||||
# bits_to_hide = [1]
|
# bits_to_hide = [1]
|
||||||
print(f"Bits to hide: {bits_to_hide}")
|
print(f"Bits to hide: {bits_to_hide}")
|
||||||
|
|
||||||
docx_file = "testfile.docx"
|
docx_file = "testfile.docx"
|
||||||
output_encoded_docx = "encoded_document.docx"
|
output_encoded_docx = "encoded_document.docx"
|
||||||
|
|
||||||
|
|
||||||
# Encode data, get as bytes
|
# Encode data, get as bytes
|
||||||
encoded_data = docx_steg(docx_file=docx_file, bits_to_hide=bits_to_hide).encode(secret_data=secret_data)
|
encoded_data = docx_steg(docx_file=docx_file, bits_to_hide=bits_to_hide).encode(secret_data=secret_data)
|
||||||
|
|
||||||
# Write encoded data to file
|
# Write encoded data to file
|
||||||
with open(output_encoded_docx, "wb") as f:
|
with open(output_encoded_docx, "wb") as f:
|
||||||
f.write(encoded_data)
|
f.write(encoded_data)
|
||||||
|
|
||||||
# Decode data from encoded docx file
|
# Decode data from encoded docx file
|
||||||
decoded_data = docx_steg(docx_file=output_encoded_docx, bits_to_hide=bits_to_hide).decode()
|
decoded_data = docx_steg(docx_file=output_encoded_docx, bits_to_hide=bits_to_hide).decode()
|
||||||
|
|
||||||
# Print the decoded data
|
# Print the decoded data
|
||||||
print("[+] Decoded data:", decoded_data)
|
print("[+] Decoded data:", decoded_data)
|
After Width: | Height: | Size: 117 KiB |
Before Width: | Height: | Size: 140 KiB |
Before Width: | Height: | Size: 519 KiB |
Before Width: | Height: | Size: 97 KiB |
Before Width: | Height: | Size: 4.7 KiB After Width: | Height: | Size: 4.7 KiB |
Before Width: | Height: | Size: 4.1 KiB After Width: | Height: | Size: 4.1 KiB |
Before Width: | Height: | Size: 799 KiB |
Before Width: | Height: | Size: 278 KiB |
|
@ -1,191 +0,0 @@
|
||||||
import cv2
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
|
|
||||||
class img_steg:
|
|
||||||
def __init__(self, image_name: str = "image.png", bit_to_hide: list[int] = None) -> None:
|
|
||||||
"""
|
|
||||||
Initialize the class
|
|
||||||
:param image_name: Name of the image to encode or decode
|
|
||||||
:type image_name: str
|
|
||||||
:param bit_to_hide: List of bit position to hide the data (LSB is 1, MSB is 8)
|
|
||||||
:type bit_to_hide: list[int]
|
|
||||||
"""
|
|
||||||
self.image_name = image_name
|
|
||||||
self.bit_to_hide = [8 - bit_pos for bit_pos in bit_to_hide] if bit_to_hide else [1] # Default is LSB
|
|
||||||
self.delimiter = "abc-123-a=="
|
|
||||||
|
|
||||||
def encode(self, secret_data: str = "Hello World") -> np.ndarray:
|
|
||||||
"""
|
|
||||||
Encode the secret data in the image
|
|
||||||
:param secret_data: Data to hide in the image
|
|
||||||
:type secret_data: str
|
|
||||||
:return: Encoded Image
|
|
||||||
:rtype: np.ndarray
|
|
||||||
"""
|
|
||||||
image = cv2.imread(self.image_name) # read image
|
|
||||||
n_bytes = image.shape[0] * image.shape[1] * 3 // 8 # Max bytes to encode
|
|
||||||
print("[*] Maximum bytes to encode:", n_bytes)
|
|
||||||
secret_data += self.delimiter # Add delimiter at the end of data
|
|
||||||
if len(secret_data) > n_bytes:
|
|
||||||
raise ValueError("[!] Insufficient bytes, need a bigger image or less data")
|
|
||||||
print("[*] Encoding Data...")
|
|
||||||
|
|
||||||
# Convert bit to hide position
|
|
||||||
bit_to_hide = self.bit_to_hide
|
|
||||||
data_index = 0
|
|
||||||
binary_secret_data = self.to_bin(secret_data) # Convert data to binary
|
|
||||||
data_len = len(binary_secret_data) # size of data to hide
|
|
||||||
|
|
||||||
print(f"[+] Size of data to hide: {data_len}")
|
|
||||||
print("[+] Starting encoding...")
|
|
||||||
|
|
||||||
for row in image:
|
|
||||||
for pixel in row:
|
|
||||||
r, g, b = self.to_bin(pixel) # Convert RGB Values to binary format
|
|
||||||
for bit_pos in bit_to_hide:
|
|
||||||
if data_index < data_len:
|
|
||||||
r = list(r)
|
|
||||||
r[bit_pos] = binary_secret_data[
|
|
||||||
data_index] # hide data into specified bit position of red pixel
|
|
||||||
pixel[0] = int(''.join(r), 2)
|
|
||||||
data_index += 1
|
|
||||||
if data_index < data_len:
|
|
||||||
g = list(g)
|
|
||||||
g[bit_pos] = binary_secret_data[
|
|
||||||
data_index]
|
|
||||||
pixel[1] = int(''.join(g), 2)
|
|
||||||
data_index += 1
|
|
||||||
if data_index < data_len:
|
|
||||||
b = list(b)
|
|
||||||
b[bit_pos] = binary_secret_data[
|
|
||||||
data_index]
|
|
||||||
pixel[2] = int(''.join(b), 2)
|
|
||||||
data_index += 1
|
|
||||||
if data_index >= data_len:
|
|
||||||
break
|
|
||||||
pixel[0] = int(''.join(r), 2) # convert modified binary back to integer for red pixel
|
|
||||||
pixel[1] = int(''.join(g), 2) # convert modified binary back to integer for green pixel
|
|
||||||
pixel[2] = int(''.join(b), 2) # convert modified binary back to integer for blue pixel
|
|
||||||
|
|
||||||
if data_index >= data_len:
|
|
||||||
break
|
|
||||||
|
|
||||||
print(f"[+] Encoding completed")
|
|
||||||
|
|
||||||
return image
|
|
||||||
|
|
||||||
def decode(self) -> str:
|
|
||||||
"""
|
|
||||||
Decode the data hidden in the image
|
|
||||||
|
|
||||||
:return: Decoded Data: String
|
|
||||||
"""
|
|
||||||
print("[+] Decoding...")
|
|
||||||
image = cv2.imread(self.image_name) # read image
|
|
||||||
binary_data = ""
|
|
||||||
bit_to_hide = self.bit_to_hide
|
|
||||||
|
|
||||||
print(f"[+] Extracting data from image...")
|
|
||||||
for row in image:
|
|
||||||
for pixel in row:
|
|
||||||
r, g, b = self.to_bin(pixel)
|
|
||||||
for bit_pos in bit_to_hide:
|
|
||||||
binary_data += r[bit_pos] # retrieve data from specified bit position of red pixel
|
|
||||||
binary_data += g[bit_pos] # retrieve data from specified bit position of green pixel
|
|
||||||
binary_data += b[bit_pos] # retrieve data from specified bit position of blue pixel
|
|
||||||
|
|
||||||
print(f"[+] Forming binary data completed")
|
|
||||||
print(f"[+] Decoding binary data...")
|
|
||||||
|
|
||||||
# Split by 8 bits
|
|
||||||
all_bytes = [binary_data[i: i + 8] for i in range(0, len(binary_data), 8)]
|
|
||||||
# Convert from bits to characters
|
|
||||||
decoded_data = ""
|
|
||||||
for byte in all_bytes:
|
|
||||||
decoded_data += chr(int(byte, 2))
|
|
||||||
if decoded_data[-len(self.delimiter):] == self.delimiter:
|
|
||||||
break
|
|
||||||
|
|
||||||
print(f"[+] Decoding completed")
|
|
||||||
return decoded_data[:-len(self.delimiter)]
|
|
||||||
|
|
||||||
def to_bin(self, data: str) -> str | list[str]:
|
|
||||||
"""
|
|
||||||
Convert data to binary format as string
|
|
||||||
|
|
||||||
:param data: Data to convert
|
|
||||||
:type data: str
|
|
||||||
:return: Binary Data
|
|
||||||
:rtype: str | list[str]
|
|
||||||
"""
|
|
||||||
if isinstance(data, str):
|
|
||||||
return ''.join([format(ord(i), "08b") for i in data])
|
|
||||||
elif isinstance(data, bytes) or isinstance(data, np.ndarray):
|
|
||||||
return [format(i, "08b") for i in data]
|
|
||||||
elif isinstance(data, int) or isinstance(data, np.unit8):
|
|
||||||
return format(data, "08b")
|
|
||||||
else:
|
|
||||||
raise TypeError("Type not supported")
|
|
||||||
|
|
||||||
def from_bin(self, data: str) -> str:
|
|
||||||
"""
|
|
||||||
# UNUSED
|
|
||||||
Convert binary `data` back to the original format
|
|
||||||
|
|
||||||
:param data: Binary Data
|
|
||||||
:type data: str
|
|
||||||
:return: Original Data
|
|
||||||
:rtype: str
|
|
||||||
"""
|
|
||||||
return ''.join([chr(int(data[i:i + 8], 2)) for i in range(0, len(data), 8)])
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
# Variables
|
|
||||||
image_name = "test.bmp"
|
|
||||||
encoded_image_name = "encoded_image.bmp"
|
|
||||||
secret_data = ""
|
|
||||||
with open("../Txt_Steg/secret_data.txt", "r") as f:
|
|
||||||
secret_data = f.read()
|
|
||||||
|
|
||||||
# Generate random bit positions to hide data into image for testing
|
|
||||||
bit_to_hide = np.random.choice(range(1, 9), np.random.randint(1, 9), replace=False)
|
|
||||||
bit_to_hide = list(bit_to_hide)
|
|
||||||
bit_to_hide.sort()
|
|
||||||
print(f"Bits to hide: {bit_to_hide}")
|
|
||||||
|
|
||||||
print("Welcome to Image Steganography")
|
|
||||||
|
|
||||||
# Encode the data into the image
|
|
||||||
encoded_image = img_steg(image_name=image_name, bit_to_hide=bit_to_hide).encode(secret_data)
|
|
||||||
cv2.imwrite(encoded_image_name, encoded_image)
|
|
||||||
|
|
||||||
# Decode the data from the image
|
|
||||||
decoded_data = img_steg(image_name=encoded_image_name, bit_to_hide=bit_to_hide).decode()
|
|
||||||
print("Decoded Data:", decoded_data)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
|
|
||||||
##############################
|
|
||||||
# _ _
|
|
||||||
# | | | |___ __ _ __ _ ___
|
|
||||||
# | | | / __|/ _` |/ _` |/ _ \
|
|
||||||
# | |_| \__ \ (_| | (_| | __/
|
|
||||||
# \___/|___/\__,_|\__, |\___|
|
|
||||||
# |___/
|
|
||||||
##############################
|
|
||||||
#
|
|
||||||
# Initialise class with image name and bit to hide
|
|
||||||
#
|
|
||||||
# image_name = image name with extension (String)
|
|
||||||
# bit_to_hide = [1, 2, 3] (Please Sort the list in ascending order)
|
|
||||||
#
|
|
||||||
# To encode:
|
|
||||||
# encoded_image = img_steg(image_name, bit_to_hide).encode(secret_data)
|
|
||||||
#
|
|
||||||
# To decode:
|
|
||||||
# decoded_data = img_steg(image_name, bit_to_hide).decode()
|
|
||||||
# Both are string types
|
|