Advertisement
yaramohamed1

Steg(3)

May 22nd, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.78 KB | None | 0 0
  1.  
  2. import java.awt.image.BufferedImage;
  3. import java.awt.image.DataBufferByte;
  4. import java.awt.image.WritableRaster;
  5. import java.io.BufferedReader;
  6. import java.io.BufferedWriter;
  7. import java.io.File;
  8. import java.io.FileReader;
  9. import java.io.FileWriter;
  10. import java.io.IOException;
  11. import java.util.ArrayList;
  12. import java.util.Random;
  13.  
  14. import javax.imageio.ImageIO;
  15.  
  16. import org.jasypt.util.binary.BasicBinaryEncryptor;
  17.  
  18. public class Steg {
  19.     // required no of bytes (image) to store one byte of text
  20.     private static final int DATA_SIZE = 8;
  21.     // INTEGER IS 4 Bytes.
  22.     private static final int MAX_INT_LEN = 4;
  23.     //Password Length
  24.     private static final int PASSWORD_LEN = 10;
  25.     //random password
  26.     private static Random rand = new Random();
  27.     public static boolean hide(String path, String imagePath)
  28.    
  29.     {
  30.         //Read the message as Byte Array.
  31.         byte[] messageBytes=readMessageBytes(path);
  32.         if(messageBytes==null)
  33.             return false;
  34.        
  35.     //Generate Password
  36.     String password=generatePassword();
  37.     byte[] passwordBytes=password.getBytes();
  38.    
  39.     //Encrypt Message
  40.     byte[] encryptedMessageBytes=encryptMessageBytes(messageBytes,password);
  41.    if(encryptedMessageBytes==null)
  42.        return false;
  43.       byte[] inputBytes = buildeStego(passwordBytes,encryptedMessageBytes);
  44.  
  45.         // read image
  46.         BufferedImage im = loadImage(imagePath);
  47.  
  48.         // check if the image file is empty
  49.         if (im == null)
  50.             return false;
  51.         // convert the image to Bytes array
  52.         byte imageBytes[] = accessBytes(im);
  53.  
  54.         if (!singleHide(imageBytes, inputBytes))
  55.             return false;
  56.         System.out.println("HERE2");
  57.         // The new Image
  58.         // String newImage=getFileName(imagePath);
  59.  
  60.         // return the new image and save it
  61.         System.out.println("HERE AGAIN");
  62.         return writeImageToFile("Msg.png", im);
  63.  
  64.     }
  65.  
  66.     private static byte[] readMessageBytes(String path)
  67.     {
  68.         // TODO Auto-generated method stub
  69.         BufferedReader br = null;
  70.         StringBuilder message = new StringBuilder();
  71.         try {
  72.             String sCurrentLine;
  73.             br = new BufferedReader(new FileReader(path));
  74.  
  75.             while ((sCurrentLine = br.readLine()) != null) {
  76.                 message.append(sCurrentLine);
  77.             }
  78.  
  79.         }
  80.  
  81.         catch (IOException e) {
  82.             e.printStackTrace();
  83.         } finally {
  84.             try {
  85.                 if (br != null)
  86.                     br.close();
  87.             } catch (IOException ex) {
  88.                 ex.printStackTrace();
  89.             }
  90.         }
  91.         byte[] messageBytes=message.toString().getBytes();
  92.         return messageBytes;
  93.     }
  94.    
  95.  
  96.     private static byte[] encryptMessageBytes(byte[] messageBytes,
  97.             String password) {
  98.         //USE AES AND MD5 FOR ENCRYPTION
  99.         BasicBinaryEncryptor temp=new BasicBinaryEncryptor();
  100.         temp.setPassword(password);
  101.         return temp.encrypt(messageBytes);
  102.     }
  103.  
  104.     private static String generatePassword()
  105.     {
  106.         // LETTERS USED IN PASSWORD.
  107.         String letters="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  108.         StringBuffer pass=new StringBuffer(PASSWORD_LEN);
  109.         int index=0;
  110.         for(int i=0;i<PASSWORD_LEN;i++)
  111.         {
  112.             //get random integer
  113.             index=rand.nextInt(letters.length());
  114.             pass.append(letters.charAt(index));
  115.         }
  116.        
  117.         return pass.toString();
  118.     }
  119.  
  120.     private static boolean singleHide(byte[] imageBytes, byte[] inputBytes) {
  121.         int imageLength = imageBytes.length;
  122.         System.out.println("Byte length of image: " + imageLength);
  123.         int totalLength = inputBytes.length;
  124.         System.out.println("Total byte length of message: " + totalLength);
  125.  
  126.         // check if the message can be hidden in the image.
  127.         // EACH BYTE(MESSAGE) REQUIRE 8 BYTES FROM IMAGE.
  128.         if ((totalLength * DATA_SIZE) > imageLength) {
  129.             System.out.println("Image not big enough for message");
  130.             return false;
  131.         }
  132.  
  133.         hideText(imageBytes, inputBytes, 0);
  134.         return true;
  135.     }
  136.  
  137.     private static void hideText(byte[] imageBytes, byte[] inputBytes, int index) {
  138.         for (int i = 0; i < inputBytes.length; i++) {
  139.             int value = inputBytes[i];
  140.             // loop through the bits of this byte
  141.             for (int j = 7; j >= 0; j--) {
  142.                 // Right shift to get the specific bit
  143.                 int bitValue = (value >>> j) & 1;
  144.                 // change the last bit for the image byte
  145.                 /*
  146.                  * The bitwise-AND operation with the hexadecimal FE (1111110
  147.                  * binary) clears the right-most bit of imBytes[offset](i.e. the
  148.                  * LSB), and the bitwise-OR places the stego bit value into the
  149.                  * empty space
  150.                  */
  151.                 imageBytes[index] = (byte) ((imageBytes[index] & 0xFE) | bitValue);
  152.                 index++;
  153.             }
  154.         }
  155.  
  156.     }
  157.  
  158.     private static boolean writeImageToFile(String imagePath, BufferedImage im) {
  159.         System.out.println("HERE");
  160.         BufferedImage image = null;
  161.         try {
  162.             image = im;
  163.             ImageIO.write(image, "png", new File(
  164.                     "/home/yaramohamed1/workspace/Steganography2/image.png"));
  165.  
  166.         } catch (IOException e) {
  167.             e.printStackTrace();
  168.         }
  169.         return true;
  170.     }
  171.  
  172.     // Access the image pixels as byte array to be in the same form of message.
  173.     private static byte[] accessBytes(BufferedImage im) {
  174.         /*
  175.          * Buffered image is made of 3 data structures (Color Space,Sample
  176.          * Model,and Data Buffer) Raster class contains (Sample Model,Data
  177.          * Buffer) Raster Class will manages the the image data,the buffer data
  178.          * will contain the pixel data SampleModel==>Pixel Values
  179.          * ColorModel==>color for the pixels We can access raster class through
  180.          * writableRaster
  181.          */
  182.  
  183.         WritableRaster raster = im.getRaster();
  184.         DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer();
  185.         return buffer.getData();
  186.     }
  187.  
  188.     // Read the image from a file.
  189.     private static BufferedImage loadImage(String imagePath) {
  190.         BufferedImage img = null;
  191.         try {
  192.             img = ImageIO.read(new File(imagePath));
  193.         } catch (IOException e) {
  194.  
  195.         }
  196.         return img;
  197.     }
  198.  
  199.     private static byte[] buildeStego(byte[] passwordBytes,byte[] encryptedMessageBytes)
  200.     {
  201.        
  202.         // From Integer to Bytes array ,because we need to store the length of
  203.         // the messages in bytes too.
  204.         byte[] lengthOfBytes = intToBytes(encryptedMessageBytes.length);
  205.         // Total length of the message+lengthOfIt+lengthOfPassword
  206.         int totalLength = passwordBytes.length+lengthOfBytes.length + encryptedMessageBytes.length;
  207.         byte[] result = new byte[totalLength];
  208.         // create new byte array which contain the bytes array and the length of
  209.         // it.
  210.         int destPosition=0;
  211.         // copy first the byte array of the length
  212.         // (input,startIndex,output,startIndexOfOutput,length of input)
  213.         System.arraycopy(passwordBytes, 0, result,destPosition, passwordBytes.length);
  214.         destPosition+=passwordBytes.length;
  215.         System.arraycopy(lengthOfBytes, 0, result, destPosition, lengthOfBytes.length);
  216.         destPosition+=lengthOfBytes.length;
  217.         // Start copying the messageBytes too
  218.         System.arraycopy(encryptedMessageBytes, 0, result,destPosition,encryptedMessageBytes.length);
  219.         // RESULT===[LengthOfBytes,messageBytes]
  220.         return result;
  221.     }
  222.  
  223.     // convert from integer to bytes
  224.     private static byte[] intToBytes(int i) {
  225.         // take in consideration that the Integer is 4 bytes.
  226.         // map the parts of the integer to a byte array
  227.         byte[] integerBs = new byte[MAX_INT_LEN];
  228.         integerBs[0] = (byte) ((i >>> 24) & 0xFF);
  229.         integerBs[1] = (byte) ((i >>> 16) & 0xFF);
  230.         integerBs[2] = (byte) ((i >>> 8) & 0xFF);
  231.         integerBs[3] = (byte) (i & 0xFF);
  232.         return integerBs;
  233.     } // e
  234.  
  235.     // Read Text from file.
  236.  
  237.     private static String readTextFile(String path) {
  238.         BufferedReader br = null;
  239.         StringBuilder message = new StringBuilder();
  240.         try {
  241.             String sCurrentLine;
  242.             br = new BufferedReader(new FileReader(path));
  243.  
  244.             while ((sCurrentLine = br.readLine()) != null) {
  245.                 message.append(sCurrentLine);
  246.             }
  247.  
  248.         }
  249.  
  250.         catch (IOException e) {
  251.             e.printStackTrace();
  252.         } finally {
  253.             try {
  254.                 if (br != null)
  255.                     br.close();
  256.             } catch (IOException ex) {
  257.                 ex.printStackTrace();
  258.             }
  259.         }
  260.         return message.toString();
  261.     }
  262.  
  263.     public static boolean reveal(String imagePath) {
  264.         // load image from file.
  265.         BufferedImage im = loadImage(imagePath);
  266.         // check if the image is empty
  267.         if (im == null)
  268.             return false;
  269.  
  270.         // get image Bytes
  271.         byte[] imageBytes = accessBytes(im);
  272.         int imageLength=imageBytes.length;
  273.         System.out.println("Byte length of image: " + imageBytes.length);
  274.  
  275.         String message=extractMessage(imageBytes,0);
  276.         if(message!=null)
  277.         {
  278.             return writeTextToFile("Result.txt", message);
  279.         }
  280.         else
  281.         {
  282.             System.out.println("ERROR : NO MESSAGE");
  283.             return false;
  284.         }
  285.        
  286.     }
  287.  
  288.     private static String extractMessage(byte[] imageBytes, int index) {
  289.         String password=getPassword(imageBytes,index);
  290.         if(password==null)
  291.         return null;
  292.         //Move to the length of the message.
  293.         index+=(PASSWORD_LEN*DATA_SIZE);
  294.         int messageLength=getMessageLength(imageBytes,index);
  295.         if(messageLength==-1)
  296.             return null;
  297.         //Move to the message
  298.         index+=(MAX_INT_LEN*DATA_SIZE);
  299.         return getMessage(imageBytes,messageLength,password,index);
  300.     }
  301.  
  302.     private static String getPassword(byte[] imageBytes, int index) {
  303.         byte[] passwordBytes=extractHiddenBytes(imageBytes,PASSWORD_LEN,index);
  304.         if(passwordBytes==null)
  305.             return null;
  306.         String password=new String(passwordBytes); 
  307.         return password;
  308.     }
  309.  
  310.     private static boolean writeTextToFile(String path, String msg) {
  311.  
  312.         try {
  313.  
  314.             File file = new File(
  315.                     "/home/yaramohamed1/workspace/Steganography2/result.txt");
  316.  
  317.             // if file doesnt exists, then create it
  318.             if (!file.exists()) {
  319.                 file.createNewFile();
  320.             }
  321.  
  322.             FileWriter fw = new FileWriter(file.getAbsoluteFile());
  323.             BufferedWriter bw = new BufferedWriter(fw);
  324.             bw.write(msg);
  325.             bw.newLine();
  326.             bw.close();
  327.  
  328.             System.out.println("Done");
  329.  
  330.         } catch (IOException e) {
  331.             e.printStackTrace();
  332.         }
  333.  
  334.         return false;
  335.     }
  336.  
  337.     private static String getMessage(byte[] imageBytes, int messageLength,
  338.             String password,int index) {
  339.         byte[] messageBytes = extractHiddenBytes(imageBytes, messageLength,
  340.                 index);
  341.         if (messageBytes == null)
  342.             return null;
  343.         BasicBinaryEncryptor temp=new BasicBinaryEncryptor();
  344.         temp.setPassword(password);
  345.         byte[] messagesBytes=null;
  346.         try
  347.         {
  348.             messagesBytes=temp.decrypt(messageBytes);
  349.         }
  350.         catch(Exception e)
  351.         {
  352.             System.out.println("ERROR : DECRYPTION");
  353.             return null;
  354.         }
  355.  
  356.         String message = new String(messagesBytes);
  357.         return message;
  358.  
  359.     }
  360.  
  361.     private static int getMessageLength(byte[] imageBytes, int index) {
  362.  
  363.         // get the binary message length as byte array
  364.         byte[] lengthBytes = extractHiddenBytes(imageBytes, MAX_INT_LEN, index);
  365.  
  366.         // check if it is null ,return -1
  367.         if (lengthBytes == null)
  368.             return -1;
  369.  
  370.         // convert the byte array into an integer
  371.         int messageLength = ((lengthBytes[0] & 0xff) << 24)
  372.                 | ((lengthBytes[1] & 0xff) << 16)
  373.                 | ((lengthBytes[2] & 0xff) << 8) | (lengthBytes[3] & 0xff);
  374.  
  375.         // check if messageLength is zero or that the message length is more
  376.         // than the image length
  377.         if (messageLength <= 0 || (messageLength > imageBytes.length)) {
  378.             System.out.println("ERROR : MESSGAE LENGTH");
  379.             return -1;
  380.  
  381.         }
  382.         return messageLength;
  383.     }
  384.  
  385.     private static byte[] extractHiddenBytes(byte[] imageBytes, int size,
  386.             int index) {
  387.         // Given that each byte of the message is stored in 8 bytes of the image
  388.         // size==4;
  389.         // datasize=8;
  390.         // check if the image has enough place to retiveral the text from it
  391.         int position = index + (size * DATA_SIZE);
  392.         if (position > imageBytes.length) {
  393.             System.out.println("ERROR : END OF IMAGE");
  394.             return null;
  395.         }
  396.         // Handle the text message in bytes.
  397.         byte[] hiddenBytes = new byte[size];
  398.         for (int j = 0; j < size; j++) {
  399.             // construct new hiddenByte[] each time.
  400.             for (int i = 0; i < DATA_SIZE; i++) {
  401.                 // make one hidden byte from DATA_SIZE image bytes
  402.                 // the extracted bit will be added to the right end og the
  403.                 // hiddenBytes[] with bitwise OR
  404.                 hiddenBytes[j] = (byte) ((hiddenBytes[j] << 1) | (imageBytes[index] & 1));
  405.                 /*
  406.                  * shift existing bits left; store LSB of image byte on the
  407.                  * right
  408.                  */
  409.                 index++;
  410.             }
  411.         }
  412.         return hiddenBytes;
  413.     }
  414.  
  415. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement