Frage

I just started studying computer science and our teacher gave us this small, but tricky programming assignment. I need to decode a .bmp image http://postimg.org/image/vgtcka251/ our teacher have handed us and after 4 hours of research and trying i'm no closer to decoding it. He gave us his encoding method:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class HideMsgInPicture {
    final static long HEADSIZE=120;

    public static void main(String[] args) throws IOException {
        encode();
        decode();
    }
    private static void encode() throws IOException {
        FileInputStream   in = null;
        FileInputStream  msg = null;
        FileOutputStream out = null;
        try {
            in  = new FileInputStream("car.bmp");
            msg = new FileInputStream("msg.txt");
            out = new FileOutputStream("carX.bmp");
            int c,mb;
            byte clearBit1 = (byte) 0xFE; //254; // 11111110

            for (int i=1;i<=HEADSIZE;i++) out.write(in.read()); //copy header

            while ((mb = msg.read()) != -1) {  // for all byte in message

                for (int bit=7; bit>=0; bit--) // 1 bit a time from messsage
                {  c = in.read() & clearBit1;  // get picturebyte,clear last bit
                   c = (c | ((mb >> bit) & 1));// put msg-bit in end of pic-byte
                   out.write(c);               // add pic-byte in new file
                }
            }

            for (int bit=7; bit>=0; bit--) // add 8 zeroes as stop-byte of msg
            {  c = in.read() & clearBit1;  // get picturebyte,clear last bit
               out.write(c);               // add pic-byte in new file
            }

            while ((c = in.read()) != -1) out.write(c);// copy rest of file
        }
        finally {
            if (in  != null)  in.close();
            if (msg != null) msg.close();
            if (out != null) out.close();
        }
    }
}

Would anyone be able to send me in the right direction?

War es hilfreich?

Lösung

How much do you know about steganography? The simplest algorithm (which is what your assignment is implementing) is the least significant bit (LSB). In short, you convert your message to binary (i.e. character 'a' = 01100001) and write the individual bits in the rightmost bits of the pixel values. For example, take 8 pixels (each represented by a byte) and in the first byte hide a 0, in the second 1, in the third 1, in the fourth 0, etc. To extract your message, obtain the binary string from the LSB in your pixels and convert it back to text.

Your teacher gave you the hiding algorithm, so basically you have to write an algorithm which reverses the process. You don't need to look further than that, you just have to understand what this code does. Just the inline comments should be enough.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top