yaramohamed1

Steg(2)

May 22nd, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.01 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.         System.out.println("Byte length of image: " + imageBytes.length);
  273.  
  274.         // get msg length
  275.         int messageLength = getMessageLength(imageBytes, 0);
  276.         // check if not empty
  277.         if (messageLength == -1)
  278.             return false;
  279.         System.out.println("Byte length of message: " + messageLength);
  280.  
  281.         // get message by passing the length of image,message and array length
  282.         // of (Max)
  283.         String msg = getMessage(imageBytes, messageLength, MAX_INT_LEN
  284.                 * DATA_SIZE);
  285.         if (msg != null)
  286.             return writeTextToFile("Result.txt", msg);
  287.         return true;
  288.  
  289.     }
  290.  
  291.     private static boolean writeTextToFile(String path, String msg) {
  292.  
  293.         try {
  294.  
  295.             File file = new File(
  296.                     "/home/yaramohamed1/workspace/Steganography/result.txt");
  297.  
  298.             // if file doesnt exists, then create it
  299.             if (!file.exists()) {
  300.                 file.createNewFile();
  301.             }
  302.  
  303.             FileWriter fw = new FileWriter(file.getAbsoluteFile());
  304.             BufferedWriter bw = new BufferedWriter(fw);
  305.             bw.write(msg);
  306.             bw.newLine();
  307.             bw.close();
  308.  
  309.             System.out.println("Done");
  310.  
  311.         } catch (IOException e) {
  312.             e.printStackTrace();
  313.         }
  314.  
  315.         return false;
  316.     }
  317.  
  318.     private static String getMessage(byte[] imageBytes, int messageLength,
  319.             int index) {
  320.         byte[] messageBytes = extractHiddenBytes(imageBytes, messageLength,
  321.                 index);
  322.         if (messageBytes == null)
  323.             return null;
  324.  
  325.         String message = new String(messageBytes);
  326.         return message;
  327.  
  328.     }
  329.  
  330.     private static int getMessageLength(byte[] imageBytes, int index) {
  331.  
  332.         // get the binary message length as byte array
  333.         byte[] lengthBytes = extractHiddenBytes(imageBytes, MAX_INT_LEN, index);
  334.  
  335.         // check if it is null ,return -1
  336.         if (lengthBytes == null)
  337.             return -1;
  338.  
  339.         // convert the byte array into an integer
  340.         int messageLength = ((lengthBytes[0] & 0xff) << 24)
  341.                 | ((lengthBytes[1] & 0xff) << 16)
  342.                 | ((lengthBytes[2] & 0xff) << 8) | (lengthBytes[3] & 0xff);
  343.  
  344.         // check if messageLength is zero or that the message length is more
  345.         // than the image length
  346.         if (messageLength <= 0 || (messageLength > imageBytes.length)) {
  347.             System.out.println("ERROR : MESSGAE LENGTH");
  348.             return -1;
  349.  
  350.         }
  351.         return messageLength;
  352.     }
  353.  
  354.     private static byte[] extractHiddenBytes(byte[] imageBytes, int size,
  355.             int index) {
  356.         // Given that each byte of the message is stored in 8 bytes of the image
  357.         // size==4;
  358.         // datasize=8;
  359.         // check if the image has enough place to retiveral the text from it
  360.         int position = index + (size * DATA_SIZE);
  361.         if (position > imageBytes.length) {
  362.             System.out.println("ERROR : END OF IMAGE");
  363.             return null;
  364.         }
  365.         // Handle the text message in bytes.
  366.         byte[] hiddenBytes = new byte[size];
  367.         for (int j = 0; j < size; j++) {
  368.             // construct new hiddenByte[] each time.
  369.             for (int i = 0; i < DATA_SIZE; i++) {
  370.                 // make one hidden byte from DATA_SIZE image bytes
  371.                 // the extracted bit will be added to the right end og the
  372.                 // hiddenBytes[] with bitwise OR
  373.                 hiddenBytes[j] = (byte) ((hiddenBytes[j] << 1) | (imageBytes[index] & 1));
  374.                 /*
  375.                  * shift existing bits left; store LSB of image byte on the
  376.                  * right
  377.                  */
  378.                 index++;
  379.             }
  380.         }
  381.         return hiddenBytes;
  382.     }
  383.  
  384. }
Advertisement
Add Comment
Please, Sign In to add comment