Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.image.BufferedImage;
- import java.awt.image.DataBufferByte;
- import java.awt.image.WritableRaster;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import javax.imageio.ImageIO;
- public class Steg
- {
- //required no of bytes (image) to store one byte of text
- private static final int DATA_SIZE = 8;
- private static final int MAX_INT_LEN = 4;
- public static boolean hide(String path,String imagePath)
- {
- //Read text file
- String input=readTextFile(path);
- //check if the file is empty or length==0
- if((input==null)||(input.length()==0))
- return false;
- //convert the text file to byte array
- byte[] inputBytes=buildeStego(input);
- //read image
- BufferedImage im=loadImage(imagePath);
- //check if the image file is empty
- if(im==null)
- return false;
- //convert the image to Bytes array
- byte imageBytes[]=accessBytes(im);
- if(!singleHide(imageBytes,inputBytes))
- return false;
- System.out.println("HERE2");
- //The new Image
- // String newImage=getFileName(imagePath);
- //return the new image and save it
- return writeImageToFile("Msg.png",im);
- }
- private static boolean singleHide(byte[] imageBytes, byte[] inputBytes)
- {
- int imageLength=imageBytes.length;
- System.out.println("Byte length of image: " + imageLength);
- int totalLength=inputBytes.length;
- System.out.println("Total byte length of message: " + totalLength);
- //check if the message can be hidden in the image.
- //EACH BYTE(MESSAGE) REQUIRE 8 BYTES FROM IMAGE.
- if((totalLength*DATA_SIZE) > imageLength)
- {
- System.out.println("Image not big enough for message");
- return false;
- }
- hideText(imageBytes,inputBytes,0);
- return true;
- }
- private static void hideText(byte[] imageBytes, byte[] inputBytes, int index)
- {
- for(int i=0;i<inputBytes.length;i++)
- {
- int value=inputBytes[i];
- //loop through the bits of this byte
- for(int j=7;j>=0;j--)
- {
- //Right shift to get the specific bit
- int bitValue=(value>>>j) & 1;
- //change the last bit for the image byte
- /*
- The bitwise-AND operation with the hexadecimal FE (1111110 binary) clears the
- right-most bit of imBytes[offset](i.e. the LSB), and the bitwise-OR places the stego bit value
- into the empty space
- */
- imageBytes[index]=(byte)((imageBytes[index] & 0xFE) | bitValue);
- index++;
- }
- }
- }
- private static boolean writeImageToFile(String imagePath, BufferedImage im)
- {
- System.out.println("HERE");
- BufferedImage image=null;
- try
- {
- image=im;
- ImageIO.write(image,"png", new File("/home/yaramohamed1/workspace/Steganography/image.png"));
- }
- catch(IOException e)
- {
- e.printStackTrace();
- }
- return true;
- }
- //Access the image pixels as byte array to be in the same form of message.
- private static byte[] accessBytes(BufferedImage im)
- {
- /*
- Buffered image is made of 3 data structures (Color Space,Sample Model,and Data Buffer)
- Raster class contains (Sample Model,Data Buffer)
- Raster Class will manages the the image data,the buffer data will contain the pixel data
- SampleModel==>Pixel Values
- ColorModel==>color for the pixels
- We can access raster class through writableRaster
- */
- WritableRaster raster=im.getRaster();
- DataBufferByte buffer=(DataBufferByte) raster.getDataBuffer();
- return buffer.getData();
- }
- //Read the image from a file.
- private static BufferedImage loadImage(String imagePath)
- {
- BufferedImage img=null;
- try
- {
- img=ImageIO.read(new File(imagePath));
- }
- catch(IOException e)
- {
- }
- return img;
- }
- private static byte[] buildeStego(String input)
- {
- //From String to Bytes.
- byte[] messageBytes=input.getBytes();
- //From Integer to Bytes array ,because we need to store the length of the messages in bytes too.
- byte[] lengthOfBytes=intToBytes(messageBytes.length);
- //Total length of the message+lengthOfIt
- int totalLength=lengthOfBytes.length+messageBytes.length;
- //create new byte array which contain the bytes array and the length of it.
- byte[] result=new byte[totalLength];
- //copy first the byte array of the length (input,startIndex,output,startIndexOfOutput,length of input)
- System.arraycopy(lengthOfBytes,0,result,0, lengthOfBytes.length);
- //Start copying the messageBytes too
- System.arraycopy(messageBytes,0,result,lengthOfBytes.length,messageBytes.length);
- //RESULT===[LengthOfBytes,messageBytes]
- return result;
- }
- //convert from integer to bytes
- private static byte[] intToBytes(int i)
- {
- //take in consideration that the Integer is 4 bytes.
- // map the parts of the integer to a byte array
- byte[] integerBs = new byte[MAX_INT_LEN];
- integerBs[0] = (byte) ((i >>> 24) & 0xFF);
- integerBs[1] = (byte) ((i >>> 16) & 0xFF);
- integerBs[2] = (byte) ((i >>> 8) & 0xFF);
- integerBs[3] = (byte) (i & 0xFF);
- return integerBs;
- } // e
- //Read Text from file.
- private static String readTextFile(String path)
- {
- BufferedReader br = null;
- StringBuilder message=new StringBuilder();
- try
- {
- String sCurrentLine;
- br = new BufferedReader(new FileReader(path));
- while ((sCurrentLine = br.readLine()) != null)
- {
- message.append(sCurrentLine);
- }
- }
- catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (br != null)br.close();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- return message.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement