5 Replies - 10138 Views - Last Post: 20 January 2013 - 12:32 PM Rate Topic: ***-- 2 Votes

#1 baba16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-January 13

Image Steganography in Java

Posted 20 January 2013 - 12:16 PM

I am writing a Image encryption program in which I encode a string inside the image.

I am working with .png images.

Encoding is performed by following steps:

1. Pixels of the image are stored in a 2D array using getRGB().
2. Each Character in the string is converted in to binary form and stored in an array.
3. The LSB of every byte of the 4byte pixel is changed according to every bit of the binary form of the character. LSB of first byte is at 0th position, LSB of second byte is at 8th position, third byte is at 16th position, and fourth byte is at 24th position.

4.After encoding, the modified pixels are written into the original image by using setRGB() method.

For decoding:

1. Read the pixels of the image using getRGB();
2. The reverse method of encoding is applied for retreiving the data.
3. After every 8th bit, int containing the decoded bits is converted in to character. Then initialized with zero for the other character.

if((pixels[i][j] & change)==1)
{
tempText|=1 ;
}

tempText<<=1;
Where change is 1,256,65536,16777216 depending on the iteration. Whose 0th,8th,16th and 24th bits are one.

The code compiles fine. But after decoding, I get weird ascii characters in output. I couldn't find the reason for this. So I am consulting experts here.

code can be found here:

http://ideone.com/hXNbxq

Is This A Good Question/Topic? 0
  • +

Replies To: Image Steganography in Java

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,090
  • Joined: 27-December 08

Re: Image Steganography in Java

Posted 20 January 2013 - 12:16 PM

We will need to see your code, in code tags: :code:, along with the specific text you are storing and the output you are encountering.
Was This Post Helpful? 0
  • +
  • -

#3 baba16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-January 13

Re: Image Steganography in Java

Posted 20 January 2013 - 12:20 PM

View Postmacosxnerd101, on 20 January 2013 - 12:16 PM, said:

We will need to see your code, in code tags: :code:/>, along with the specific text you are storing and the output you are encountering.


I have provided the link to my code. The code is very big to be quoted here
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,090
  • Joined: 27-December 08

Re: Image Steganography in Java

Posted 20 January 2013 - 12:21 PM

Code on Dream.in.Code is here for others to benefit from later. Code on other sites can be taken down, making the thread useless for others. Please post your code here, using code tags: :code:.
Was This Post Helpful? 0
  • +
  • -

#5 baba16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-January 13

Re: Image Steganography in Java

Posted 20 January 2013 - 12:26 PM

// for encoding
 
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
import javax.imageio.ImageIO;
 
 
public class Stegnography {
 
    private BufferedImage image=null,new_image=null;
        private int pixels[][];
        private char Data[];
        private int Width,Height;
        private int Binary[];
        private int Length=0;
        
        public Stegnography()
        {
                File fileName=new File("C:\\Users\\Umer\\Desktop\\project.png");
                
                try
                {
                        new_image= ImageIO.read(fileName);
                        
                        image=new BufferedImage(new_image.getWidth(), new_image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
                        
                         Graphics2D graphics   = image.createGraphics();
                         graphics.drawRenderedImage(new_image, null);
                         
                         graphics.dispose();
 
                }
                catch (IOException e)
                {
                        e.printStackTrace();
                }
                
                pixels= new int[image.getWidth()][image.getHeight()];
                
                Binary= new int[8];
                
                Width=image.getWidth();
                Height=image.getHeight();
                
                for(int i=0;i<Width;i++)
                {
                        for(int j=0;j<Height;j++)
                        {
                                pixels[i][j]=image.getRGB(i, j);
                        }
                }
        
                String name= "adsf asdfasd fasdf asdasfa a dfasdfas fas fasdf asdf asdf sadf adsf asdf asd fasdf sadfadsfadsf adsf adsf ads fasdf asdf adsf as dfasfasd fadsf adsf asdf as" +
                                "df as fasdf asdf asdf asd fas fasf addsf asf adsf asfa dsf dasf as fad fsadf adsf adsf sadf adsf adsf af asddf as dfds" +
                                "asdfas fasf as fasf asdf sa fasf adfafdsfasdfasf";
                Length=name.length();
                
                Data=name.toCharArray();
                
 
                
                
                WriteLSB();
                
                try
                {
                        ImageIO.write(image, "png", fileName);
                }
                catch (IOException e) 
                {
 
                        e.printStackTrace();
                }
                
                System.out.println("Encoding Completed");
        }
        
        private void WriteLSB()
        {               
                int temp,tempText,x=0,p=0,i,j=0;
                Binary((int) Data[x]);
                x++;
                
                for(i=0;(i<Width && x<Data.length);i++)
                {
                        for(j=0;( j<Height && x<Data.length);j++)
                        {
 
                                int change=0;   
                                
                                for(int k=0;k<4;k++)
                                {
                                        if(k==0)
                                        {
                                                change=1;
                                        }
                                        else
                                                if(k==1)
                                                {
                                                        change=256;
                                                }
                                                else
                                                        if(k==2)
                                                        {
                                                                change=65536;
                                                        }
                                                        else
                                                                if(k==3)
                                                                {
                                                                        change = 16777216;
                                                                }
                                
                                        if(Binary[p]==0)
                                        {
                                                pixels[i][j]= pixels[i][j] & ~change; // ~1 ki all bits 1 hoti ha except LSB
                                        }
                                        else
                                                if(Binary[p]==1)
                                                {
                                                        pixels[i][j]= pixels[i][j] | change; // only LSB ko 1 krna ha
                                                }
                                        p++;
                                        
                                        if(p==8)
                                        {
                                                p=0;
                                                Binary((int) Data[x]);
                                                x++;
                                        }
                                }
                                
                        
                        
                        }
                }
 
                
                for(i=0;i<Width;i++)
                {
                        for(int h=0;h<Height;h++)
                        {
                                image.setRGB(i, h,pixels[i][h]);
                        }
                }
                
        }
        
        private void Binary(int temp)
        {
                Binary=null;
                Binary= new int[8];
                
                for(int i=7;i>=0;i--)// cuz character is of 1 byte
                {
                        Binary[i]=temp%2;
                        
                        System.out.print(Binary[i]);
                        temp/=2;
                }
                System.out.println();
        }
}
 
 
// for decoding
 
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
 
import javax.imageio.ImageIO;
 
 
public class RetreiveData {
 
    private BufferedImage image=null;
        private int pixels[][];
        private char Data[];
        private int Width,Height;
        private String myString="";
        
        public RetreiveData()
        {
                try
                {
                        image= ImageIO.read(new File("C:\\Users\\Umer\\Desktop\\project.png"));
                }
                catch (IOException e)
                {
                        e.printStackTrace();
                }
                
        
                
                pixels= new int[image.getWidth()][image.getHeight()];
                
                Width=image.getWidth();
                Height=image.getHeight();
                
                System.out.println(Width);
                System.out.println(Height);
                
                for(int i=0;i<Width;i++)
                {
                        for(int j=0;j<Height;j++)
                        {
                                pixels[i][j]=image.getRGB(i, j);
                        }
                }
                
                FileWriter fw=null;
                File file=null;
                BufferedWriter bw=null;
                
                try
                {
                        file = new File("C:\\Users\\Umer\\Desktop\\Retreive.txt");
                        fw = new FileWriter(file);
                        
                        bw= new BufferedWriter(fw);
                        
                } 
                catch (Exception e) 
                {
                        e.printStackTrace();
                }
 
                
                ReadLSB();
                
                try 
                {
                        bw.write(myString);
                        bw.close();
                } 
                catch (IOException e)
                {
                        e.printStackTrace();
                }
                
                System.out.println(myString);
        }
        
        private void ReadLSB()
        {               
                int temp,tempText=0,x=0,p=0;
                
                for(int i=0;i<Width;i++)
                {
                        for(int j=0;j<Height;j++)
                        {
                                temp=pixels[i][j];
                                
                                int change=0;   
                                
                                for(int k=0;k<4;k++)
                                {
                                        if(k==0)
                                        {
                                                change=1;
                                        }
                                        else
                                                if(k==1)
                                                {
                                                        change=256;
                                                }
                                                else
                                                        if(k==2)
                                                        {
                                                                change=65536;
                                                        }
                                                        else
                                                                if(k==3)
                                                                {
                                                                        change = 16777216;
                                                                }
                                
 
                                        if((pixels[i][j] & change)==1)  
                                        {
                                                tempText|=1 ;
                                        }
                                        
                                        tempText<<=1;
                                        
                                        p++;
                                        if(p==8)
                                        {
                                                myString += (char) tempText;// convert int to String and storing in String object
                                                p=0;
                                                                                            
                                                tempText=0;
 
                                        }
                                }
                                
                                
                        
                        }
                }
        }
}
 


Consider only first block of code including encoding and decoding. The later two are the same as first and they are posted by mistake

This post has been edited by macosxnerd101: 20 January 2013 - 12:24 PM
Reason for edit:: Removed triple post

Was This Post Helpful? 0
  • +
  • -

#6 baba16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-January 13

Re: Image Steganography in Java

Posted 20 January 2013 - 12:32 PM

View Postmacosxnerd101, on 20 January 2013 - 12:21 PM, said:

Code on Dream.in.Code is here for others to benefit from later. Code on other sites can be taken down, making the thread useless for others. Please post your code here, using code tags: :code:/>.


Have you found the bug in my code?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1