Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package stego;
- import java.io.*;
- import java.awt.image.*;
- import javax.imageio.*;
- public class kodowanie
- {
- private static final int MAX_INT_LEN = 4;
- private static final int DATA_SIZE = 8;
- public static boolean hide(String textFnm, String imFnm) throws IOException
- {
- String inputText = readTextFile(textFnm);
- if ((inputText == null) || (inputText.length() == 0))
- return false;
- byte[] stego = buildStego(inputText);
- BufferedImage im = loadImage(imFnm);
- if (im == null){
- return false;}
- byte imBytes[] = accessBytes(im);
- if (!singleHide(imBytes, stego))
- return false;
- String fnm = getFileName(imFnm);
- boolean write = ImageIO.write(im, "PNG", new File("zakodowany.PNG"));
- return write;
- }
- private static String readTextFile(String fnm)
- //odczytanie pliku tekstowego
- {
- BufferedReader br = null;
- StringBuilder sb = new StringBuilder();
- try {
- br = new BufferedReader(new FileReader( new File(fnm) )); String text = null;
- while ((text = br.readLine()) != null)
- sb.append(text).append("\n");
- }
- catch (Exception e) {
- return null;
- }
- finally {
- try {
- if (br != null)
- br.close();
- }
- catch (IOException e) {
- return null;
- }
- }
- return sb.toString();
- }
- private static byte[] buildStego(String inputText)
- {
- //konwersja bitow do arraya
- byte[] msgBytes = inputText.getBytes();
- byte[] lenBs = intToBytes(msgBytes.length);
- int totalLen = lenBs.length + msgBytes.length;
- byte[] stego = new byte[totalLen];
- System.arraycopy(lenBs, 0, stego, 0, lenBs.length); //dlugosc wiadomosci
- System.arraycopy(msgBytes, 0, stego, lenBs.length, msgBytes.length); //wiadomosc binarnie
- return stego;
- }
- private static byte[] intToBytes(int i)
- {
- 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;
- }
- private static BufferedImage loadImage(String imFnm)
- {
- BufferedImage im = null;
- try {
- im = ImageIO.read( new File(imFnm) );
- }
- catch (IOException e)
- { return im; }
- return im;
- }
- private static byte[] accessBytes(BufferedImage image)
- //odczytanie bitow w obrazku
- {
- WritableRaster raster = image.getRaster();
- DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer();
- return buffer.getData();
- }
- private static boolean singleHide(byte[] imBytes, byte[] stego)
- // ukrywanie 'stego' w obrazk
- {
- int imLen = imBytes.length; //dlugosc obrazka
- int totalLen = stego.length; //dlugosc wiadomosci
- Gui.dltxt.setText("Długość wiadomości: " + totalLen + "\nDługość obrazka: " + imLen);
- if ((totalLen*DATA_SIZE) > imLen) { //sprawdzenie czy obrazek nie jest za maly
- System.out.println("BLAD! Obrazek za maly");
- return false;
- }
- hideStego(imBytes, stego, 0); // ukrycie na początku obrazka
- return true;
- }
- //static int j = Gui.ile;
- private static void hideStego(byte[] imBytes, byte[] stego, int offset)
- // ukrywanie reszty wiadomosci
- {
- //int j = Gui.ile;
- for (int i = 0; i < stego.length; i++) { // przejscie przez stego
- int byteVal = stego[i];
- for(int j=7; j >= 0; j--) { // przejscie przez bity
- int bitVal = (byteVal >>> j) & 1;
- // ostatni bit obrazka = ostatni bit stego
- imBytes[offset] = (byte)((imBytes[offset] & 0xFE) | bitVal);
- offset++;
- }
- }
- }
- private static String getFileName(String fnm)
- // odczytanie nazwy pliku
- {
- int extPosn = fnm.lastIndexOf('.');
- if (extPosn == -1) {
- return fnm;
- }
- return fnm.substring(0, extPosn);
- }
- private static boolean canOverWrite(String fnm)
- { return true; }
- // --------------------------------------------------------------------------
- public static boolean reveal(String imFnm)
- //odkodowanie
- {
- // odczytanie obrazka jako arraya
- BufferedImage im = loadImage(imFnm);
- if (im == null)
- return false;
- byte[] imBytes = accessBytes(im);
- // odczytanie dlugosci wiadomosci
- int msgLen = getMsgLength(imBytes, 0);
- if (msgLen == -1)
- return false;
- String msg = getMessage(imBytes, msgLen, MAX_INT_LEN*DATA_SIZE);
- if (msg != null) {
- String fnm = getFileName(imFnm);
- writeStringToFile("odkodowany.txt", msg);
- return true;
- }
- else {
- return false;
- }
- }
- private static int getMsgLength(byte[] imBytes, int offset)
- {
- byte[] lenBytes = extractHiddenBytes(imBytes, MAX_INT_LEN, offset);
- if (lenBytes == null)
- return -1;
- int msgLen = ((lenBytes[0] & 0xff) << 24) |
- ((lenBytes[1] & 0xff) << 16) |
- ((lenBytes[2] & 0xff) << 8) |
- (lenBytes[3] & 0xff);
- if ((msgLen <= 0) || (msgLen > imBytes.length)) {
- Gui.tekst.setText("Błąd odkodowania!");
- return -1;
- }
- Gui.dltxt.setText("Długość wiadomości: " + msgLen);
- return msgLen;
- }
- private static String getMessage(byte[] imBytes, int msgLen, int offset)
- //przerabia wiadomosc do bin i zwraca je jako string
- {
- byte[] msgBytes = extractHiddenBytes(imBytes, msgLen, offset);
- if (msgBytes == null)
- return null;
- String msg = new String(msgBytes);
- if (isPrintable(msg)) {
- return msg;
- }
- else
- return null;
- }
- private static byte[] extractHiddenBytes(byte[] imBytes, int size, int offset)
- {
- int finalPosn = offset + (size*DATA_SIZE);
- if (finalPosn > imBytes.length) {
- return null;
- }
- byte[] hiddenBytes = new byte[size];
- for (int j = 0; j < size; j++) {
- for (int i=0; i < DATA_SIZE; i++) {
- hiddenBytes[j] = (byte) ((hiddenBytes[j] << 1) | (imBytes[offset] & 1));
- offset++;
- }
- }
- return hiddenBytes;
- }
- private static boolean isPrintable(String str)
- {
- for (int i=0; i < str.length(); i++)
- if (!isPrintable(str.charAt(i))) {
- return false;
- }
- return true;
- }
- private static boolean isPrintable(int ch)
- {
- if (Character.isWhitespace(ch) && (ch < 127))
- return true;
- else if ((ch > 32) && (ch < 127))
- return true;
- return false;
- }
- private static boolean writeStringToFile(String outFnm, String msgStr)
- // zapisanie do pliku
- {
- if (!canOverWrite(outFnm))
- return false;
- try {
- FileWriter out = new FileWriter( new File(outFnm) );
- out.write(msgStr);
- out.close();
- return true;
- }
- catch(IOException e)
- {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement