Reputation: 1
I'm trying around to see if I can steganographically hide a database inside an image. I managed to convert the original .db file in binary, but my stegunhide function doesn't do that, instead converting to a text file with binary inside and the ending (dot)db. Any help is appreciated :)
def steghide():
db_location = input("Enter the path to the .db file: ")
try:
os.path.isfile(db_location)
with open(db_location, "rb") as file:
content = file.read()
binary_db = ''.join(f"{byte:>08b}" for byte in content)
except Exception as e:
print("Error:", e)
img_location = input("Enter the path to the .png file: ")
try:
os.path.isfile(img_location)
except Exception as e:
print("Error:", e)
image = PIL.Image.open(img_location, 'r')
width, height = image.size
img_arr = np.array(list(image.getdata()))
if image.mode == "P":
print("Error: Paletted images not supported. ")
exit()
channels = 4 if image.mode == "RGBA" else 3
pixels = img_arr.size // channels
indicator = "$STEGPASS$"
binary_db += indicator
byte_db = ''.join(f"{ord(c):08b}" for c in binary_db)
bits = len(byte_db)
if bits > pixels:
print("Error: Not enough space within image.")
else:
index = 0
for i in range(pixels):
for j in range(0, 3):
if index < bits:
img_arr[i][j] = int(bin(img_arr[i][j])[2:-1] + byte_db[index], 2)
index += 1
img_arr = img_arr.reshape((height, width, channels))
result = PIL.Image.fromarray(img_arr.astype('uint8'), image.mode)
result.save('encoded.png')
def stegunhide():
img_location = input("Enter the path to the .png file: ")
try:
os.path.isfile(img_location)
image = PIL.Image.open(img_location, 'r')
img_arr = np.array(list(image.getdata()))
if image.mode == "P":
print("Error: Paletted images not supported. ")
exit()
channels = 4 if image.mode == "RGBA" else 3
pixels = img_arr.size // channels
secret_bits = [bin(img_arr[i][j])[-1] for i in range(pixels) for j in range(0,3)]
secret_bits = ''.join(secret_bits)
secret_bits = [secret_bits[i:i+8] for i in range(0, len(secret_bits), 8)]
database = [chr(int(secret_bits[i], 2)) for i in range(len(secret_bits))]
database = ''.join(database)
indicator = "$STEGPASS$"
if indicator in database:
db_content = database[:database.index(indicator)]
with open('decrypted.db', "wb") as file:
file.write(db_content.encode())
else:
print('Error: No database found.')
except Exception as e:
print('Error:', e)
Honestly, I have no idea as to how to go on about this problem.
Upvotes: 0
Views: 38