img_steg.py Logic Error Fix

Solved issues:
-Default changes second MSB instead of LSB
-Check does not take into account number of bits to be used for encoding

Notes:
-Altered check to compare bit sizes instead of byte sizes to accommodate above fix
This commit is contained in:
Rymndt 2023-05-31 18:27:49 +08:00
parent 56e4bb129c
commit 1e50a408d7
1 changed files with 8 additions and 5 deletions

View File

@ -12,7 +12,7 @@ class img_steg:
:type bit_to_hide: list[int] :type bit_to_hide: list[int]
""" """
self.image_name = image_name 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.bit_to_hide = [8 - bit_pos for bit_pos in bit_to_hide] if bit_to_hide else [7] # Default is LSB (index = 7)
self.delimiter = "abc-123-a==" self.delimiter = "abc-123-a=="
def encode(self, secret_data: str = "Hello World") -> np.ndarray: def encode(self, secret_data: str = "Hello World") -> np.ndarray:
@ -24,10 +24,13 @@ class img_steg:
:rtype: np.ndarray :rtype: np.ndarray
""" """
image = cv2.imread(self.image_name) # read image image = cv2.imread(self.image_name) # read image
n_bytes = image.shape[0] * image.shape[1] * 3 // 8 # Max bytes to encode n_bits = (image.shape[0] * image.shape[1] * 3) * 8 * len(self.bit_to_hide) # Max bits to encode
print("[*] Maximum bytes to encode:", n_bytes) print("[*] Maximum bytes to encode:", n_bits // 8)
print("[*] Maximum bits to encode:", n_bits)
secret_data += self.delimiter # Add delimiter at the end of data secret_data += self.delimiter # Add delimiter at the end of data
if len(secret_data) > n_bytes:
if len(secret_data)*8 > n_bits:
raise ValueError("[!] Insufficient bytes, need a bigger image or less data") raise ValueError("[!] Insufficient bytes, need a bigger image or less data")
print("[*] Encoding Data...") print("[*] Encoding Data...")
@ -37,7 +40,7 @@ class img_steg:
binary_secret_data = self.to_bin(secret_data) # Convert data to binary binary_secret_data = self.to_bin(secret_data) # Convert data to binary
data_len = len(binary_secret_data) # size of data to hide data_len = len(binary_secret_data) # size of data to hide
print(f"[+] Size of data to hide: {data_len}") print(f"[+] Size of data to hide: {data_len} bits")
print("[+] Starting encoding...") print("[+] Starting encoding...")
for row in image: for row in image: