Você está na página 1de 6

https://fivedots.coe.psu.ac.th/~ad/jg/javaArt6/stego.

pdf
/

--https://fivedots.coe.psu.ac.th/~ad/jg/javaArt6/stego.pdf

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

public class Steg {


private static final int DATA_SIZE = 8;
private static final int MAX_INT_LEN = 4;
public static void main(String args[]) throws IOException
{

Steg s = new Steg();


BufferedImage i = s.getImage("");
byte bimg[] = s.get_byte_data(i);
byte btext[]=s.Stego_text("good morning!");
byte bcoded[]=s.encoding(bimg,btext,0);

// ByteArrayInputStream coded = new


ByteArrayInputStream(bcoded,0,1440000);

try {
// BufferedImage im= ImageIO.read(coded);
ImageIO.write(i, "png", new
File("C:\\Users\\Rahul\\Desktop\\pro\\304(2).bmp"));

} catch (IOException e) {
throw new RuntimeException(e);
}
// s.getImage("C:\\Users\\Rahul\\Desktop\\pro\\block.jpg");
// if(s.Encode("C:\\Users\\Rahul\\Desktop\\pro","i","png","i2","This is
the message"))
// {
// System.out.println("Encoding done");
// }
// String decoded= s.decode("C:\\Users\\Rahul\\Desktop\\pro","new");
// System.out.println("the message is " + decoded);
}
public String img_path(String path, String name, String ext)
{
String file_name = path + "/" + name + "." + ext;
return file_name;
}

// public Boolean Encode (String path,String orignal, String ext,String stegan,


String message)
// {
// String file_name=img_path(path,orignal,ext);
// BufferedImage image=getImage(file_name);
// BufferedImage image_user=img_compatible(image);
// image_user=add_message(image_user,message);
// return(setImage(image,new File(img_path(path,stegan,"png")),"png"));
// }

private BufferedImage img_compatible(BufferedImage image) {


BufferedImage new_image = new BufferedImage(image.getWidth(),
image.getHeight(),BufferedImage.TYPE_3BYTE_BGR);
Graphics2D graphics = new_image.createGraphics();
graphics.drawRenderedImage(image, null);
graphics.dispose(); //release all allocated memory for this
image
return new_image;
}
public BufferedImage getImage(String fullpath)
{
File f = new File("C:\\Users\\Rahul\\Desktop\\pro\\304.bmp");
BufferedImage img =null;
try {
img = ImageIO.read(f);
} catch (IOException e) {
e.printStackTrace();
}
return img;
}

//private BufferedImage add_message(BufferedImage image_user, String message)


//{
// byte img[] = get_byte_data(image_user);
// byte msg[] = message.getBytes();
// byte len[] = bit_conversion(msg.length);
// System.out.println("the messsage size is" + msg.length + "the image byte
size is" + img.length);
// try
// {
// System.out.println("trying to encode!");
// encode_text(img, len, 0); //0 first positions
//
// int msgLen = getMsgLength(img, 0);
// if (msgLen == -1)
// System.out.println("false");
// System.out.println("Byte length of message: " +
msgLen);
// System.out.println("length encoded!");
//
// byte[] t=encode_text(img, msg, 32); //1 integer i.e. 4 bytes of
space for length: 4bytes*8bit = 32 bits
// System.out.println("Message Encoded!");
//
// byte[] d= decode_text(t);
// System.out.println("Message Decoded!");
//
// String g = new String(d);
// System.out.println("String "+ g);
// }
// catch(Exception e)
// {
// JOptionPane.showMessageDialog(null,
// "Target File cannot hold message!", "Error",JOptionPane.ERROR_MESSAGE);
// }
// return image_user;
//}

public byte[] get_byte_data(BufferedImage image)


{
WritableRaster raster = image.getRaster();

DataBufferByte buffer = (DataBufferByte)raster.getDataBuffer();

return buffer.getData();
}

private byte[] bit_conversion(int length)


{
byte byte0 = (byte) (length & 0x000000FF);
byte byte1 = (byte) ((length & 0x0000FF00)>>> 8);
byte byte2 = (byte) ((length & 0x00FF0000)>>> 16);
byte byte3 = (byte) ((length & 0xFF000000)>>> 24);
byte []b = {byte3,byte2,byte1,byte0};
return b;
}

private byte[] encode_text(byte[] img, byte[] insert, int img_arrayincr)


{

int j=1;
try {
System.out.println(insert.length);
if (insert.length + img_arrayincr > img.length)
{
System.out.println("error");
throw new IllegalArgumentException("File not long
enough!");
}
System.out.println("fine");
} catch (Exception e1) {
System.out.println("notfine");// TODO Auto-generated cach block
e1.printStackTrace();
}

for(int i=0;i<insert.length;i++)
{
int a=0;
try {
a = insert[i];
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//stores the element of 'insert' array of type byte

for(int bit=7;bit>=0;bit--,++img_arrayincr) // replaces


the last bit of img array with bits of insert
{
int bits_of_insertbyte= (a>>>bit) & 1;
img[img_arrayincr] =(byte) ((img[img_arrayincr]
& 0xFE) | (bits_of_insertbyte));
System.out.println("hell yes!!"+ j++);
}
System.out.println("fine");
}

System.out.println("returning");
byte[] b;
return b=img;

private boolean setImage(BufferedImage image, File file, String ext)


{
try

{
file.delete(); //delete resources used by the File
ImageIO.write(image,ext,file);
return true;
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,
"File could not be saved!","Error",JOptionPane.ERROR_MESSAGE);
return false;
}

private byte[] extractHiddenBytes(byte[] imBytes,int size, int offset)


{
int finalPosn = offset + (size*DATA_SIZE);
if (finalPosn > imBytes.length) {
System.out.println("End of image reached");
return null;
}
byte[] hiddenBytes = new byte[size];
for (int j = 0; j < size; j++) { // loop through hidden bytes
for (int i=0; i < DATA_SIZE; i++) {
// make one hidden byte from DATA_SIZE image bytes
hiddenBytes[j] = (byte) ((hiddenBytes[j] << 1) |
(imBytes[offset] & 1));
/* shift existing bits left;
store LSB of image byte on the right */
offset++;
}
}
return hiddenBytes;
} // end of extractHiddenBytes()

private String getMessage(byte[] imBytes, int msgLen, int


offset)
{
byte[] msgBytes = extractHiddenBytes(imBytes, msgLen, offset);
// the message is msgLen bytes long
if (msgBytes == null)
return null;
String msg = new String(msgBytes);
// check the message is all characters
return msg;

} // end of getMessage()

private boolean singleHide(byte[] imBytes, byte[] stego)


{
int imLen = imBytes.length;
System.out.println("Byte length of image: " + imLen);
int totalLen = stego.length;
System.out.println("Total byte length of message: " + totalLen);
// check that the stego will fit into the image
/* multiply stego length by number of image bytes
required to store one stego byte */
if ((totalLen*DATA_SIZE) > imLen) {
System.out.println("Image not big enough for message");
return false;
}
encoding(imBytes, stego, 0); // hide at start of image
return true;
} // end of singleHide()

public byte[] encoding(byte[] imBytes, byte[] stego,


int offset)
{
for (int i = 0; i < stego.length; i++) { // loop through stego
int byteVal = stego[i];
for(int j=7; j >= 0; j--) //since they were encoded in the same orde
{ // looping through 8 bits of stego byte
int bitVal = (byteVal >>> j) & 1;
// change last bit of image byte to be the stego bit
imBytes[offset] = (byte)((imBytes[offset] & 0xFE) | bitVal);
offset++;
}
}
return imBytes;
} // end of hideStego()
public byte[] Stego_text(String inputText)
{
// convert data to byte arrays
byte[] msgBytes = inputText.getBytes();
byte[] lenBs = intToBytes(msgBytes.length);
int totalLen = lenBs.length + msgBytes.length;
byte[] stego = new byte[totalLen]; // for holding resulting stego
// combine the two fields into one byte array
System.arraycopy(lenBs, 0, stego, 0, lenBs.length);
// length of binary message
System.arraycopy(msgBytes, 0, stego, lenBs.length,msgBytes.length);
// binary message
return stego;
} // end of buildStego()

private byte[] intToBytes(int i)


{
// 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;
} // end of intToBytes()

}
http://www.dreamincode.net/forums/topic/27950-steganography/