Advertisement
yaramohamed1

PART11

May 22nd, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.61 KB | None | 0 0
  1. import java.awt.image.BufferedImage;
  2. import java.awt.image.DataBufferByte;
  3. import java.awt.image.WritableRaster;
  4. import java.io.BufferedReader;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9.  
  10. import javax.imageio.ImageIO;
  11.  
  12.  
  13. public class Steg
  14. {
  15.     //required no of bytes (image) to store one byte of text
  16.     private static final int DATA_SIZE = 8;  
  17.     private static final int MAX_INT_LEN = 4;
  18.    
  19.     public static boolean hide(String path,String imagePath)
  20.     {
  21.        
  22.         //Read text file
  23.         String input=readTextFile(path);
  24.         //check if the file is empty or length==0
  25.         if((input==null)||(input.length()==0))
  26.             return false;
  27.        
  28.         //convert the text file to byte array
  29.         byte[] inputBytes=buildeStego(input);
  30.        
  31.         //read image
  32.         BufferedImage im=loadImage(imagePath);
  33.        
  34.         //check if the image file is empty
  35.         if(im==null)
  36.             return false;
  37.         //convert the image to Bytes array
  38.         byte imageBytes[]=accessBytes(im);
  39.        
  40.        
  41.         if(!singleHide(imageBytes,inputBytes))
  42.             return false;
  43.         System.out.println("HERE2");
  44.         //The new Image
  45.       // String newImage=getFileName(imagePath);
  46.        
  47.        //return the new image and save it
  48.        return writeImageToFile("Msg.png",im);
  49.        
  50.        
  51.        
  52.     }
  53.  
  54.  
  55.  
  56.     private static boolean singleHide(byte[] imageBytes, byte[] inputBytes)
  57.     {
  58.         int imageLength=imageBytes.length;
  59.         System.out.println("Byte length of image: " + imageLength);
  60.         int totalLength=inputBytes.length;
  61.         System.out.println("Total byte length of message: " + totalLength);
  62.        
  63.         //check if the message can be hidden in the image.
  64.         //EACH BYTE(MESSAGE) REQUIRE 8 BYTES FROM IMAGE.
  65.         if((totalLength*DATA_SIZE) > imageLength)
  66.         {
  67.             System.out.println("Image not big enough for message");
  68.             return false;
  69.         }
  70.        
  71.         hideText(imageBytes,inputBytes,0);
  72.         return true;
  73.     }
  74.  
  75.     private static void hideText(byte[] imageBytes, byte[] inputBytes, int index)
  76.     {
  77.         for(int i=0;i<inputBytes.length;i++)
  78.         {
  79.             int value=inputBytes[i];
  80.             //loop through the bits of this byte
  81.             for(int j=7;j>=0;j--)
  82.             {
  83.                 //Right shift to get the specific bit
  84.                 int bitValue=(value>>>j) & 1;
  85.                 //change the last bit for the image byte
  86.                 /*
  87.                  The bitwise-AND operation with the hexadecimal FE (1111110 binary) clears the
  88.                  right-most bit of imBytes[offset](i.e. the LSB), and the bitwise-OR places the stego bit value
  89.                  into the empty space
  90.                  */
  91.                 imageBytes[index]=(byte)((imageBytes[index] & 0xFE) | bitValue);
  92.                 index++;
  93.             }
  94.         }
  95.        
  96.     }
  97.  
  98.     private static boolean writeImageToFile(String imagePath, BufferedImage im)
  99.     {
  100.         System.out.println("HERE");
  101.         BufferedImage image=null;
  102.         try
  103.         {
  104.         image=im;
  105.         ImageIO.write(image,"png", new File("/home/yaramohamed1/workspace/Steganography/image.png"));
  106.  
  107.          
  108.         }
  109.         catch(IOException e)
  110.         {
  111.         e.printStackTrace();   
  112.         }
  113.         return true;
  114.     }
  115.      
  116.     //Access the image pixels as byte array to be in the same form of message.
  117.     private static byte[] accessBytes(BufferedImage im)
  118.     {  
  119.         /*
  120.          Buffered image is made of 3 data structures (Color Space,Sample Model,and Data Buffer)
  121.          Raster class contains (Sample Model,Data Buffer)
  122.          Raster Class will manages the the image data,the buffer data will contain the pixel data
  123.          SampleModel==>Pixel Values
  124.          ColorModel==>color for the pixels
  125.          We can access raster class through writableRaster
  126.          */
  127.        
  128.         WritableRaster raster=im.getRaster();
  129.         DataBufferByte buffer=(DataBufferByte) raster.getDataBuffer();
  130.         return buffer.getData();
  131.     }
  132.    
  133.    
  134.     //Read the image from a file.
  135.     private static BufferedImage loadImage(String imagePath)
  136.     {
  137.         BufferedImage img=null;
  138.         try
  139.         {
  140.             img=ImageIO.read(new File(imagePath));
  141.         }
  142.         catch(IOException e)
  143.         {
  144.            
  145.         }
  146.         return img;
  147.     }
  148.  
  149.     private static byte[] buildeStego(String input)
  150.     {
  151.         //From String to Bytes.
  152.         byte[] messageBytes=input.getBytes();
  153.         //From Integer to Bytes array ,because we need to store the length of the messages in bytes too.
  154.         byte[] lengthOfBytes=intToBytes(messageBytes.length);
  155.         //Total length of the message+lengthOfIt
  156.         int totalLength=lengthOfBytes.length+messageBytes.length;
  157.         //create new byte array which contain the bytes array and the length of it.
  158.         byte[] result=new byte[totalLength];
  159.         //copy first the byte array of the length (input,startIndex,output,startIndexOfOutput,length of input)
  160.         System.arraycopy(lengthOfBytes,0,result,0, lengthOfBytes.length);
  161.         //Start copying the messageBytes too
  162.         System.arraycopy(messageBytes,0,result,lengthOfBytes.length,messageBytes.length);
  163.        
  164.         //RESULT===[LengthOfBytes,messageBytes]
  165.         return result;
  166.     }
  167.     //convert from integer to bytes
  168.     private static byte[] intToBytes(int i)
  169.     {
  170.     //take in consideration that the Integer is 4 bytes.
  171.      // map the parts of the integer to a byte array
  172.      byte[] integerBs = new byte[MAX_INT_LEN];
  173.      integerBs[0] = (byte) ((i >>> 24) & 0xFF);
  174.      integerBs[1] = (byte) ((i >>> 16) & 0xFF);
  175.      integerBs[2] = (byte) ((i >>> 8) & 0xFF);
  176.      integerBs[3] = (byte) (i & 0xFF);
  177.      return integerBs;
  178.     }  // e
  179.  
  180.    
  181.     //Read Text from file.
  182.     private static String readTextFile(String path)
  183.     {
  184.         BufferedReader br = null;
  185.         StringBuilder message=new StringBuilder();
  186.         try
  187.         {
  188.             String sCurrentLine;
  189.             br = new BufferedReader(new FileReader(path));
  190.            
  191.            
  192.            
  193.             while ((sCurrentLine = br.readLine()) != null)
  194.             {
  195.                 message.append(sCurrentLine);              
  196.             }
  197.            
  198.         }
  199.        
  200.         catch (IOException e) {
  201.             e.printStackTrace();
  202.         } finally {
  203.             try {
  204.                 if (br != null)br.close();
  205.             } catch (IOException ex) {
  206.                 ex.printStackTrace();
  207.             }
  208.         }
  209.         return message.toString();
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement