3

I need to decode a message from the top line of pixels in a BMP picture. There is a message hidden in the red value in binary. I have tried finding a way to do this and haven't succeeded because most programs use .jpg or .png file types for images and BMP images don't work in that code. I need to find out how to reverse this code to decode the message using arrays, and I need to print the message in ASCII text. For some programs, they allowed to me array the top line of the image but I don't know how to print it. Here is the code for encoding the message in the image:

import java.awt.*;
class Encode
{
    public void encodeMessage(Picture image, int [] binaryArray)
    {
        Pixel pixelTarget = new Pixel(image,0,0);
        Pixel [] pixelArray = image.getPixels();
        Color pixelColor = null;
        int redValue = 0;

        for(int x = 0; x < binaryArray.length; x++)
        {
            redValue = binaryArray[x];
            pixelTarget = pixelArray[x];
            pixelTarget.setRed(redValue);
        }
        pixelTarget = pixelArray[binaryArray.length];
        pixelTarget.setRed(255);

        image.write("SecretMessage.bmp");
        image.explore();
    }
}
public class EncodeTester
{ 
    public static void main(String[] args)
    {
        int[] bitArray = {0,1,1,0,0,0,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,1,0,0,1,1,0,0,0,0,1,0,1,1,1,0,0,1,0,0,1,1,1,1,0,0,1};    
        Picture image = new Picture("earth.bmp");
        Encode message = new Encode();

        message.encodeMessage(image, bitArray);

    }
}

Could someone please explain how to continue or write up a sample code for me to read the bmp file, get the red values from arrays and print it out in ASCII text. Thanks

EDIT: This isn't exactly steganograhpy. It is different as the code is hidden only in the first line and I need to use int[] arrays to array all pixels, then array the top line of pixels, then print out the red values in ASCII text.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571

1 Answers1

1

I'm not familiar with the classes Picture and Pixel since they're not in the standard library, but if this is the Pixel class you are using, try the following:

  • create a method decodeMessage(Picture image) and get a Pixel array with image.getPixels() just like you did in encodeMessage().
  • loop through the message bits (using the 255 you put at the end of the encoding as an indicator for the sequence ending) and read the values with pixelTarget.getRed(). These values correspond to the encoded binary values. Generate an array for these bits.
  • Now you have to convert every block of 7 bits into an integer. Use bitwise operations like they did here and cast the resulting int values to char values (like here).*
  • Convert the char array to a String by passing the array to the String constructor.
  • Print the string.

(*) Keep in mind that binary representations are ambiguous, so you have to decide what byte order you are using for the ASCII bits.

Community
  • 1
  • 1
leifvan
  • 11
  • 2