-1

I'm having some issues with my program, basically what I'm trying to do is Stenography, insert an image into another image and then extract the secret image.

My program is able to insert just fine, but extracting it I get this error.

enter image description here

It was working fine the other day, I was able to read and write perfectly. I am also using the same BMP images as the other day as well. Anyone know the issue? I didn't change any of the code from it's working state, I even uploaded to github to make sure that version worked.

There are 3 files of code that is on my github.

https://github.com/am3ience/Steganography/blob/master/dcstego.py

https://github.com/am3ience/Steganography/blob/master/dcimage.py

https://github.com/am3ience/Steganography/blob/master/dcutils.py

The problem seems to be happening in dcutils.py in my read function. Any help would be amazing, i'm stumped.

#examine the lsb of each pixel, grouping into bytes
#check for nulls to signify if we are dealing with data or header info
#bytes determined to be data result in the hidden file
#---------------------------------------------------------------
def read(mainimage, output, password):
    lsbByte_Array = []
    dataString = ""
    secretFileName = ""
    lsbString = ""
    count = 0#iterator
    headerReceived=0#flags
    sizeReceived=0
    imageObject = dcimage.openFile(mainimage)
    pixels = imageObject.load()
    imageWidth, imageHeight = imageObject.size

    #cycle through each pixel
    for x in range(imageWidth):
        for y in range(imageHeight):
            r, g, b = pixels[x, y]
            #trim so we are dealing with only the least significant bit
            redPixel = str(bin(r)[2:].zfill(8))[7]
            greenPixel = str(bin(g)[2:].zfill(8))[7]
            bluePixel = str(bin(b)[2:].zfill(8))[7]
            secretBits = [redPixel, greenPixel, bluePixel]

            #for each of rgb
            for i in range(0,3):
                #check if our flags are set
                if (headerReceived == 0 or sizeReceived == 0):
                    lsbString += secretBits[i]

                    #verify each byte
                    if len(lsbString) == 8:
                        lsbByte_Array.append(lsbString)

                        #check if we have received a NULL byte
                        if lsbString == "00000000":
                            if headerReceived == 0:

                                #convert the the bit array into an ascii String
                                #set flag when header and size was received
                                fileName = ''.join(binascii.unhexlify('%x' % int(b,2)) for b in lsbByte_Array[0:len(lsbByte_Array) - 1])
                                print "File name: " + str(fileName)
                                headerReceived = 1
                            elif sizeReceived == 0:
                                 fileSize = ''.join(binascii.unhexlify('%x' % int(b,2)) for b in lsbByte_Array[0:len(lsbByte_Array) - 1])
                                print "File size: " + fileSize
                                sizeReceived=1

                            #reset the values
                            lsbByte_Array = []
                        lsbString = ""

                #once headers received, resulting data is hidden data
                elif (headerReceived == 1 and sizeReceived == 1):
                    if int(count) < int(fileSize):
                        #keep appending secret bits to the dataString until depleted
                        dataString += secretBits[i]
                        count += 1
                    else:
                        #send to have hidden file created
return dcimage.saveImage(output, dataString)
Paul
  • 105
  • 1
  • 12
  • `fileSize` is an empty string. What do you expect `int(fileSize)` to return? – Barmar Oct 04 '17 at 23:46
  • 1
    Please use `True` and `False` for boolean values, not `1` and `0`. It makes the code much clearer. – Barmar Oct 04 '17 at 23:48
  • @Barmar yeah that makes sense, however I don't get how filezise could be empty when I'm populating everything properly? Again this worked the other day, and now it just doesn't work all of a sudden.... – Paul Oct 04 '17 at 23:57
  • Programs don't change their behavior spontaneously. Either the script changed or the file changed. – Barmar Oct 05 '17 at 00:11
  • If you can't replicate the state when the code worked, mentioning that is useless. By the way, inserting your secret worked fine because the program didn't throw any errors does not necessarily mean it did what you expected it to do. Start from the offending line and print its variables' contents to figure out what triggered the error and go one step back to figure out why those variables held such unexpected values, until you find the cause. – Reti43 Oct 05 '17 at 00:24
  • Never, ever, ever post screenshots of text if the image isn't actually relevant to the problem (font rendering, widget positioning, etc..). Copy and paste or retype the text. – nobody Oct 06 '17 at 02:12

2 Answers2

1
fileSize = ''.join(binascii.unhexlify('%x' % int(b,2)) for b in 
                   lsbByte_Array[0:len(lsbByte_Array) - 1])

if lsbByte_Array is an empty array then fileSize will be an empty string, which is obviously why int(fileSize) fails.

You should probably use a default value, for example

fileSize = ''.join(binascii.unhexlify('%x' % int(b,2)) for b in 
                   lsbByte_Array[0:len(lsbByte_Array) - 1]) or 0

As a side note, stuff like lsbByte_Array[0:len(lsbByte_Array) - 1]) can simply be written as lsbByte_Array[0:-1].

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • Yea that makes sense, but I'm trying to figure out why it's empty in the first place, when i'm populating everything just fine? again, it was working jsut fine the other day, I can't imagine why it's not working now.. – Paul Oct 04 '17 at 23:58
  • It's dependent on what's in the file you're processing. Maybe it has an empty image. – Barmar Oct 05 '17 at 00:00
  • @Barmar the file it's creating is not empty – Paul Oct 05 '17 at 00:02
  • @PaulCabz Use a debugger or print the values. We can't debug your code for you at that level over the internet. – DeepSpace Oct 05 '17 at 00:06
0

I feel really dumb, I just needed to hear it from other people, it turns out in my infinite wisdom, I just tried opening images without actual hidden data in it. But I did fix up my code as other users here advised me to. Thanks for everything guys!

Paul
  • 105
  • 1
  • 12