Search code examples
javacryptographyhidden-files

Extracting hidden files out from an image using Java


We can hide rar/zip files inside an image in following ways. This concept is known as Steganography.

Suppose I want to hide a rar file in an image , I will write this command in Windows command prompt

copy /b image.jpg+as.zip hidden.jpg

Now to get back the contents,we can use Winrar/7zip or any other utility to open the hidden.jpg and it will display all its contents

Now to read a zip file I am using this java code

try{
            File file=new File(name);
            ZipFile zipFile=new ZipFile(file);
            Enumeration<? extends ZipEntry>enumeration=zipFile.entries();

            System.out.println("Listing Entries in the zipfile");
            while(enumeration.hasMoreElements()){
                Object key=enumeration.nextElement();
                System.out.println(key.toString()+":"+zipFile.getEntry(key.toString()));
            }

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

This code works fine for normal zip file/But when I try to open the "hidden.jpg" file with this code it is causing error. I tested the same with Apache Common Compress Library also . Can anybody point some way how I can do this in Java ?

I am sure this can be done cause we can view the hidden files using tools like WinRar


Solution

  • This is not real steganography since you are not hiding anything in the image. You are only adding data in the end of the file and if the image is edited in any way, the additional data is lost.

    The only reason some (not all) archivers can find the "hidden" data is because they search the whole file for a possible archive. Normal library routines do not. They expect the archive to start in the beginning.

    So if you want to open the archive, you have to find the start position. This may be possible by opening the image file and determining its size, or just searching for the beginning of the archive yourself.

    But as I said, this is not steganography and nothing is hidden in the image.

    Also, stenography is writing quick hand, steganography is hiding data inside another.