txt_steg.py Logic Error Fix
Solved issues: -Default changes second MSB instead of LSB -Check for size limit was checking number of characters in data floor division 8 instead of multiplying by number of bits available for use. Notes: Renamed byte to char for clarity (Python Char = 4 bytes)
This commit is contained in:
parent
5713c4ea24
commit
b25941ae1c
|
@ -11,7 +11,7 @@ class txt_steg:
|
||||||
:type bit_to_hide: list[int]
|
:type bit_to_hide: list[int]
|
||||||
"""
|
"""
|
||||||
self.text_file = text_file # PathName of the text file
|
self.text_file = text_file # PathName of the text file
|
||||||
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] #Each bit value converted to index (e.g. bit 2 -> 6), Default is LSB (index = 7)
|
||||||
self.delimiter = "abc-123==" # Delimiter to indicate the end of the secret data
|
self.delimiter = "abc-123==" # Delimiter to indicate the end of the secret data
|
||||||
|
|
||||||
def encode(self, secret_data: str = "Hello World") -> str:
|
def encode(self, secret_data: str = "Hello World") -> str:
|
||||||
|
@ -30,13 +30,13 @@ class txt_steg:
|
||||||
|
|
||||||
secret_data += self.delimiter # Add delimiter
|
secret_data += self.delimiter # Add delimiter
|
||||||
|
|
||||||
# Max Bytes to encode
|
# Max Bits to encode (1 Character = 8 bits)
|
||||||
n_bytes = len(data) // 8
|
n_bits = len(data) * len(self.bit_to_hide) # Bits that can be used (Each character x Number of bits that can be used)
|
||||||
|
|
||||||
# Check if secret data can be encoded into text file
|
# Check if secret data can be encoded into text file
|
||||||
if len(secret_data) > n_bytes:
|
if len(secret_data) * 8 > n_bits:
|
||||||
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)*8} is greater than data length {n_bits}")
|
||||||
|
|
||||||
data_index = 0
|
data_index = 0
|
||||||
# Convert secret data to binary
|
# Convert secret data to binary
|
||||||
|
@ -46,18 +46,18 @@ class txt_steg:
|
||||||
|
|
||||||
print(f"[+] Starting encoding...")
|
print(f"[+] Starting encoding...")
|
||||||
# Encode data into text
|
# Encode data into text
|
||||||
for byte in data:
|
for char in data:
|
||||||
byte = self.to_bin(byte)
|
bin_char = self.to_bin(char)
|
||||||
byte = "0" * (8 - len(byte)) + byte
|
bin_char = "0" * (8 - len(bin_char)) + bin_char
|
||||||
if data_index >= len(binary_secret_data):
|
if data_index >= len(binary_secret_data):
|
||||||
encoded_data += self.from_bin(''.join(byte))
|
encoded_data += self.from_bin(''.join(bin_char))
|
||||||
else:
|
else:
|
||||||
for bit_pos in self.bit_to_hide:
|
for bit_pos in self.bit_to_hide:
|
||||||
if data_index < len(binary_secret_data):
|
if data_index < len(binary_secret_data):
|
||||||
byte = list(byte)
|
bin_char = list(bin_char)
|
||||||
byte[bit_pos] = binary_secret_data[data_index]
|
bin_char[bit_pos] = binary_secret_data[data_index]
|
||||||
data_index += 1
|
data_index += 1
|
||||||
encoded_data += self.from_bin(''.join(byte))
|
encoded_data += self.from_bin(''.join(bin_char))
|
||||||
|
|
||||||
print(f"[+] Encoded Successfully!")
|
print(f"[+] Encoded Successfully!")
|
||||||
return encoded_data
|
return encoded_data
|
||||||
|
@ -79,18 +79,18 @@ class txt_steg:
|
||||||
|
|
||||||
binary_data = ""
|
binary_data = ""
|
||||||
print(f"[+] Gathering data from bit positions: {self.bit_to_hide}")
|
print(f"[+] Gathering data from bit positions: {self.bit_to_hide}")
|
||||||
for byte in data:
|
for char in data:
|
||||||
byte = self.to_bin(byte)
|
bin_char = self.to_bin(char)
|
||||||
byte = "0" * (8 - len(byte)) + byte
|
bin_char = "0" * (8 - len(bin_char)) + bin_char
|
||||||
for bit_pos in self.bit_to_hide:
|
for bit_pos in self.bit_to_hide:
|
||||||
binary_data += byte[bit_pos]
|
binary_data += bin_char[bit_pos]
|
||||||
|
|
||||||
print(f"[+] Converting binary data to text...")
|
print(f"[+] Converting binary data to text...")
|
||||||
all_bytes = [binary_data[i:i + 8] for i in range(0, len(binary_data), 8)]
|
all_chars = [binary_data[i:i + 8] for i in range(0, len(binary_data), 8)]
|
||||||
|
|
||||||
decoded_data = ""
|
decoded_data = ""
|
||||||
for byte in all_bytes:
|
for char in all_chars:
|
||||||
decoded_data += chr(int(byte, 2))
|
decoded_data += chr(int(char, 2))
|
||||||
if decoded_data[-len(self.delimiter):] == self.delimiter:
|
if decoded_data[-len(self.delimiter):] == self.delimiter:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue