Login  Register

Problems with regenerating a steganography-file

Posted by Fabian Pursche on Apr 02, 2008; 12:47pm
URL: http://imagej.273.s1.nabble.com/Problems-with-regenerating-a-steganography-file-tp3696699.html

Hello,

I'm trying to write two Plugins for ImageJ to hide a file in picture (8-bit
greyshade) and two read it out again. It works quite well, but I don't know
how to fit the new file right. I've implemented a header of two bytes in
front of my fileInputStream, but after regaining the header, the filesize
doesn't match.

Here are both codes:

//written by F.Pursche 2008

import ij.ImagePlus;
import ij.plugin.filter.PlugInFilter;
import ij.process.ImageProcessor;
import java.io.*;

public class Steganography_ implements PlugInFilter{

        public int setup (String arg, ImagePlus imp) {
        return DOES_8G;
    }

        public void run (ImageProcessor ip) {

                int w = ip.getWidth();
        int h = ip.getHeight();
        int maxsize = w*h;
        int counter;
        long header;
        counter = 0;
        header = 0;
        Exception E = null;

        File file = new File("C:\\source.txt");
        try{
        if(file.exists()){
        FileInputStream stream = new FileInputStream(file);
        if (((stream.available()*8)-16) > maxsize){throw E;}
        else{
        int buffer;
        int fileSize = stream.available();
        int fileSizeH = ((fileSize & 65280)/ 256);
        int fileSizeL = fileSize & 255;
        int firstByte = 0;

        for (int x=0; x<w; x++) {
        for (int y=0; y<h; y++) {

        if(header == 0){
                buffer = fileSizeH;
                }
                if(header == 1){
                buffer = fileSizeL;
                }
                else{
                buffer = firstByte;
                }
        int bit = 0;
        int lsb = 0;
        int p = 0;
        int pixel = ip.getPixel(x, y);

        if(counter == 0){
        lsb = buffer & 1;
        if(lsb == 1){
        bit = 1;
        }
        }
        if(counter == 1){
        lsb = buffer & 2;
        if(lsb == 2){
        bit = 1;
        }
        }
        if(counter == 2){
        lsb = buffer & 4;
        if(lsb == 4){
        bit = 1;
        }
        }
        if(counter == 3){
                        lsb = buffer & 8;
                        if(lsb == 8){
        bit = 1;
        }
                        }
        if(counter == 4){
                        lsb = buffer & 16;
                        if(lsb == 16){
        bit = 1;
        }
                        }
        if(counter == 5){
                        lsb = buffer & 32;
                        if(lsb == 32){
        bit = 1;
        }
                        }
        if(counter == 6){
                        lsb = buffer & 64;
                        if(lsb == 64){
        bit = 1;
        }
                        }
        if(counter == 7){
                        lsb = buffer & 128;
                        if(lsb == 128){
        bit = 1;
        }
                        counter = -1;
                        header++;
                        if(header > 1){
                        buffer=stream.read();
                        }

                        }

        p = (pixel & 254) + bit;

        ip.putPixel(x,y,p);
        counter++;

        }
        }
        }

        }
        }
        catch (Exception e) {
        System.out.println(e.toString());
        }


        }
}



2nd code:

//written by F.Pursche 2008

import ij.ImagePlus;
import ij.plugin.filter.PlugInFilter;
import ij.process.ImageProcessor;
import java.io.*;

public class ReSteganography_ implements PlugInFilter{

        public int setup (String arg, ImagePlus imp) {
        return DOES_8G;
    }

        public void run (ImageProcessor ip) {

                int w = ip.getWidth();
        int h = ip.getHeight();

        int counter, sizeCounter;
        counter = 0;
        sizeCounter = 0;
        long header = 0;
        int buffer=0;
        int fileSize, fileSizeH, fileSizeL;
                fileSizeH = 0;
                fileSizeL = 0;
                fileSize = -1;

        File file = new File("C:\\newfile.txt");
        try{

        FileOutputStream stream = new FileOutputStream(file);

        for (int x=0; x<w; x++) {
        for (int y=0; y<h; y++) {
        int bit = 0;
        int lsb;

        int pixel = ip.getPixel(x, y);

        lsb = pixel & 1;

        if(counter == 0){
        if(lsb == 1){
        bit = 1;
        buffer = buffer | bit;
        }
        }
        if(counter == 1){
        if(lsb == 1){
        bit = 2;
        buffer = buffer | bit;
        }
        }
        if(counter == 2){
        if(lsb == 1){
        bit = 4;
        buffer = buffer | bit;
        }
        }
        if(counter == 3){
                        if(lsb == 1){
        bit = 8;
        buffer = buffer | bit;
        }
                        }
        if(counter == 4){
                        if(lsb == 1){
        bit = 16;
        buffer = buffer | bit;
        }
                        }
        if(counter == 5){
                        if(lsb == 1){
        bit = 32;
        buffer = buffer | bit;
        }
                        }
        if(counter == 6){
                        if(lsb == 1){
        bit = 64;
        buffer = buffer | bit;
        }
                        }
        if(counter == 7){
                        if(lsb == 1){
        bit = 128;
        buffer = buffer | bit;
        }

                        if(header == 0){
                        fileSizeH = buffer;
                        }
                        if(header == 1){
                        fileSizeL = buffer;
                        fileSize = (fileSizeH * 256) | fileSizeL;

                        }
                        if(header > 1){
                        if(sizeCounter <= fileSize){
                        stream.write(buffer);
                        sizeCounter++;

                        }

                        }
                        header++;
                        buffer = 0;
                        counter = -1;
                        }
        counter++;
        }
        }
        stream.close();
        }

        catch (Exception e) {
        System.out.println(e.toString());
        }
        }
}


Just change the file in the first filter, maybe a txt-file.


Thanks
Fabian