1
\$\begingroup\$

As this is pure Python so using loop with big files make the code very slow any improvements or suggestions.

Hide Code

 # Consts
HEADER_SIZE = 54
DELIMITER = "$"
class LSBEncrypter(object):
 def __init__(self):
 self.image_byte_counter = 0
 self.new_image_data = ''
 self.original_image = ''
 self.text_to_hide = ''
 def open_image(self):
 with open(ImageFile, "rb") as f:
 self.original_image = ''.join(map(chr, f.read()))
 def read_header(self):
 for x in range(0, HEADER_SIZE):
 self.new_image_data += self.original_image[x]
 self.image_byte_counter += 1
 def hide_text_size(self):
 sz = len(self.text_to_hide)
 s_sz = str(sz)
 s_sz += DELIMITER
 self.do_steg(s_sz)
 def do_steg(self, steg_text):
 for ch in range(0, len(steg_text)):
 current_char = steg_text[ch]
 current_char_binary = '{0:08b}'.format(ord(current_char))
 for bit in range(0, len(current_char_binary)):
 new_byte_binary = ''
 current_image_binary = '{0:08b}'.format(ord(self.original_image[self.image_byte_counter]))
 new_byte_binary = current_image_binary[:7]
 new_byte_binary += current_char_binary[bit]
 new_byte = chr(int(new_byte_binary, 2))
 self.new_image_data += new_byte
 self.image_byte_counter += 1
 def copy_rest(self):
 self.new_image_data += self.original_image[self.image_byte_counter:]
 def close_file(self):
 with open(StegImageFile, "wb") as out:
 out.write(bytearray(map(ord, self.new_image_data)))
 def run(self, stega_text):
 self.text_to_hide = stega_text
 self.open_image()
 self.read_header()
 self.hide_text_size()
 self.do_steg(self.text_to_hide)
 self.copy_rest()
 self.close_file()
 def hide_(self, Texte, image, steg):
 global TextToHide, ImageFile, StegImageFile
 TextToHide = Texte
 ImageFile = image
 StegImageFile = steg
 stega_instance = LSBEncrypter()
 stega_instance.run(TextToHide)

Reveal Code

 import binascii
HEADER_SIZE = 54
DELIMITER = "$"
class LSBDecrypter:
 def __init__(self):
 self.StegImageFile = ''
 # self.fh = open(StegImageFile, 'rb')
 self.number_of_chars_in_text = 0
 self.original_text = ''
 def read_header(self):
 self.fh = open(StegImageFile, 'rb')
 for i in range(0, HEADER_SIZE):
 byte = self.fh.read(1)
 def get_char(self):
 new_byte = ''
 for bit in range(0, 8):
 byte = self.fh.read(1)
 new_byte += str(ord(byte) & 0x01)
 n = int(new_byte, 2)
 desteg_char = binascii.unhexlify('%x' % n)
 desteg_char = ''.join(map(chr, desteg_char))
 return desteg_char
 def get_text_size(self):
 curr_ch = self.get_char()
 s_sz = ''
 while curr_ch != DELIMITER:
 s_sz += curr_ch
 curr_ch = self.get_char()
 if (s_sz != ''):
 self.number_of_chars_in_text = int(s_sz)
 def read_stega_text(self):
 decoded_chars = 0;
 while decoded_chars < self.number_of_chars_in_text:
 self.original_text += self.get_char()
 decoded_chars += 1
 def close_file(self):
 self.fh.close();
 def get_text(self):
 self.read_header()
 self.get_text_size()
 self.read_stega_text()
 self.close_file()
 return self.original_text
 def reveal_(self, stego):
 global StegImageFile
 StegImageFile = stego
 destag_insta = LSBDecrypter()
 text = destag_insta.get_text()
 print (text)
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Sep 15, 2017 at 23:56
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Performance:

def get_char(self):
 new_byte = ''
 for bit in range(0, 8):
 byte = self.fh.read(1)
 new_byte += str(ord(byte) & 0x01)
 n = int(new_byte, 2)
 desteg_char = binascii.unhexlify('%x' % n)
 desteg_char = ''.join(map(chr, desteg_char))
 return desteg_char

This is not as simple as it could be. You are reading 8 bits as strings to build a single byte, converting it to an integer, then to a hex string (string format before unhexlify), then to a hexadecimal string, then mapping to a character, and then joining them together. I recommend doing more math and less string conversion and concatenation.

Style:

1. Use consistent string delimiters

  • You have ' in most places, like open(StegImageFile, 'rb'), but you also have DELIMITER = "$" and open(StegImageFile, "wb"). Use ' everywhere if that's what you're using.

2. Remove unnecessary parentheses

  • if (s_sz != ''): should be if s_sz != '':, or even more concise: if s_sz:.

3. Consider meaningful variable names

  • s_sz isn't a helpful variable name, and I don't know what it is at a glance, other than that it's the string form of sz (also not helpful).

4. Remove the space in print (text)

  • You don't use spaces after any other function names.

5. Remove weird leading spaces

  • The space before # Consts

  • The space before import binascii

answered May 15, 2018 at 21:50
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.