Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.06 KB | None | 0 0
  1. package stego;
  2.  
  3.  
  4. import java.io.*;
  5. import java.awt.image.*;
  6. import javax.imageio.*;
  7.  
  8.  
  9.  
  10. public class kodowanie
  11. {
  12.   private static final int MAX_INT_LEN = 4;
  13.   private static final int DATA_SIZE = 8;
  14.  
  15.  
  16.  
  17.   public static boolean hide(String textFnm, String imFnm) throws IOException
  18.   {
  19.    
  20.     String inputText = readTextFile(textFnm);
  21.     if ((inputText == null) || (inputText.length() == 0))
  22.       return false;
  23.  
  24.     byte[] stego = buildStego(inputText);
  25.  
  26.  
  27.     BufferedImage im = loadImage(imFnm);
  28.     if (im == null){
  29.        return false;}
  30.     byte imBytes[] = accessBytes(im);
  31.  
  32.     if (!singleHide(imBytes, stego))  
  33.      return false;
  34.  
  35.  
  36.     String fnm = getFileName(imFnm);
  37.     boolean write = ImageIO.write(im, "PNG", new File("zakodowany.PNG"));
  38.     return write;
  39.   }
  40.  
  41.  
  42.  
  43.   private static String readTextFile(String fnm)
  44.   //odczytanie pliku tekstowego
  45.   {
  46.     BufferedReader br = null;
  47.     StringBuilder sb = new StringBuilder();
  48.  
  49.     try {
  50.       br = new BufferedReader(new FileReader( new File(fnm) ));      String text = null;
  51.       while ((text = br.readLine()) != null)
  52.             sb.append(text).append("\n");
  53.     }
  54.     catch (Exception e) {
  55.        return null;
  56.     }
  57.     finally {
  58.       try {
  59.         if (br != null)
  60.           br.close();
  61.        }
  62.        catch (IOException e) {
  63.              return null;
  64.        }
  65.     }
  66.     return sb.toString();
  67.   }
  68.  
  69.  
  70.  
  71.   private static byte[] buildStego(String inputText)
  72.   {
  73.    //konwersja bitow do arraya
  74.     byte[] msgBytes = inputText.getBytes();
  75.     byte[] lenBs = intToBytes(msgBytes.length);
  76.  
  77.     int totalLen = lenBs.length + msgBytes.length;
  78.     byte[] stego = new byte[totalLen];    
  79.  
  80.  
  81.     System.arraycopy(lenBs, 0, stego, 0, lenBs.length);          //dlugosc wiadomosci
  82.     System.arraycopy(msgBytes, 0, stego, lenBs.length, msgBytes.length);   //wiadomosc binarnie
  83.          return stego;
  84.   }  
  85.  
  86.  
  87.  
  88.   private static byte[] intToBytes(int i)
  89.   {
  90.     byte[] integerBs = new byte[MAX_INT_LEN];
  91.     integerBs[0] = (byte) ((i >>> 24) & 0xFF);
  92.     integerBs[1] = (byte) ((i >>> 16) & 0xFF);
  93.     integerBs[2] = (byte) ((i >>> 8) & 0xFF);
  94.     integerBs[3] = (byte) (i & 0xFF);
  95.  
  96.     return integerBs;
  97.   }
  98.  
  99.  
  100.  
  101.   private static BufferedImage loadImage(String imFnm)
  102.   {
  103.     BufferedImage im = null;
  104.     try {
  105.       im = ImageIO.read( new File(imFnm) );
  106.  
  107.     }
  108.     catch (IOException e)
  109.     {  return im; }
  110.  
  111.     return im;
  112.   }  
  113.  
  114.  
  115.  
  116.   private static byte[] accessBytes(BufferedImage image)
  117.   //odczytanie bitow w obrazku
  118.   {
  119.     WritableRaster raster = image.getRaster();
  120.     DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer();
  121.     return buffer.getData();
  122.   }  
  123.  
  124.  
  125.  
  126.   private static boolean singleHide(byte[] imBytes, byte[] stego)
  127.   // ukrywanie 'stego' w obrazk
  128.   {
  129.     int imLen = imBytes.length; //dlugosc obrazka
  130.    
  131.     int totalLen = stego.length; //dlugosc wiadomosci
  132.        Gui.dltxt.setText("Długość wiadomości: " + totalLen + "\nDługość obrazka: " + imLen);
  133.     if ((totalLen*DATA_SIZE) > imLen) { //sprawdzenie czy obrazek nie jest za maly
  134.       System.out.println("BLAD! Obrazek za maly");
  135.       return false;
  136.     }
  137.  
  138.     hideStego(imBytes, stego, 0);  // ukrycie na początku obrazka
  139.     return true;
  140.   }  
  141.  
  142.  
  143.   //static int j = Gui.ile;
  144.   private static void hideStego(byte[] imBytes, byte[] stego, int offset)
  145.   // ukrywanie reszty wiadomosci
  146.   {
  147.       //int j = Gui.ile;
  148.     for (int i = 0; i < stego.length; i++) {       // przejscie przez stego
  149.       int byteVal = stego[i];
  150.       for(int j=7; j >= 0; j--) {    // przejscie przez bity
  151.         int bitVal = (byteVal >>> j) & 1;
  152.  
  153.         // ostatni bit obrazka = ostatni bit stego
  154.         imBytes[offset] = (byte)((imBytes[offset] & 0xFE) | bitVal);
  155.         offset++;
  156.       }
  157.     }
  158.   }
  159.  
  160.  
  161.  
  162.   private static String getFileName(String fnm)
  163.   // odczytanie nazwy pliku
  164.   {
  165.     int extPosn = fnm.lastIndexOf('.');
  166.     if (extPosn == -1) {
  167.       return fnm;
  168.     }
  169.     return fnm.substring(0, extPosn);
  170.   }  
  171.  
  172.  
  173.     private static boolean canOverWrite(String fnm)
  174.   { return true; }  
  175.  
  176.  
  177.  
  178.  
  179.   // --------------------------------------------------------------------------
  180.  
  181.  
  182.   public static boolean reveal(String imFnm)
  183.   //odkodowanie
  184.   {
  185.     // odczytanie obrazka jako arraya
  186.     BufferedImage im = loadImage(imFnm);
  187.     if (im == null)
  188.       return false;
  189.     byte[] imBytes = accessBytes(im);
  190.  
  191.     // odczytanie dlugosci wiadomosci
  192.     int msgLen = getMsgLength(imBytes, 0);
  193.     if (msgLen == -1)
  194.       return false;
  195.    
  196.     String msg = getMessage(imBytes, msgLen, MAX_INT_LEN*DATA_SIZE);
  197.    
  198.     if (msg != null) {
  199.       String fnm = getFileName(imFnm);
  200.       writeStringToFile("odkodowany.txt", msg);
  201.       return true;
  202.  
  203.     }
  204.     else {
  205.         return false;
  206.     }
  207.    
  208.   }  
  209.  
  210.  
  211.  
  212.   private static int getMsgLength(byte[] imBytes, int offset)
  213.   {
  214.     byte[] lenBytes = extractHiddenBytes(imBytes, MAX_INT_LEN, offset);
  215.            
  216.     if (lenBytes == null)
  217.       return -1;
  218.  
  219.     int msgLen = ((lenBytes[0] & 0xff) << 24) |
  220.                  ((lenBytes[1] & 0xff) << 16) |
  221.                  ((lenBytes[2] & 0xff) << 8) |
  222.                   (lenBytes[3] & 0xff);
  223.  
  224.     if ((msgLen <= 0) || (msgLen > imBytes.length))  {
  225.       Gui.tekst.setText("Błąd odkodowania!");
  226.       return -1;
  227.     }
  228.     Gui.dltxt.setText("Długość wiadomości: " + msgLen);
  229.     return msgLen;
  230.   }  
  231.  
  232.  
  233.  
  234.   private static String getMessage(byte[] imBytes, int msgLen, int offset)
  235.   //przerabia wiadomosc do bin i zwraca je jako string
  236.   {
  237.     byte[] msgBytes = extractHiddenBytes(imBytes, msgLen, offset);
  238.     if (msgBytes == null)
  239.       return null;
  240.  
  241.     String msg = new String(msgBytes);
  242.     if (isPrintable(msg)) {
  243.       return msg;
  244.     }
  245.     else
  246.       return null;
  247.   }  
  248.  
  249.  
  250.  
  251.   private static byte[] extractHiddenBytes(byte[] imBytes, int size, int offset)
  252.   {
  253.     int finalPosn = offset + (size*DATA_SIZE);
  254.     if (finalPosn > imBytes.length) {
  255.        return null;
  256.     }
  257.     byte[] hiddenBytes = new byte[size];
  258.  
  259.     for (int j = 0; j < size; j++) {    
  260.       for (int i=0; i < DATA_SIZE; i++) {  
  261.         hiddenBytes[j] = (byte) ((hiddenBytes[j] << 1) | (imBytes[offset] & 1));
  262.         offset++;
  263.       }
  264.     }
  265.     return hiddenBytes;
  266.   }  
  267.  
  268.  
  269.  
  270.   private static boolean isPrintable(String str)
  271.   {
  272.     for (int i=0; i < str.length(); i++)
  273.       if (!isPrintable(str.charAt(i))) {
  274.                return false;
  275.       }
  276.     return true;
  277.   }
  278.  
  279.  
  280.  
  281.   private static boolean isPrintable(int ch)
  282.   {
  283.     if (Character.isWhitespace(ch) && (ch < 127))  
  284.       return true;
  285.     else if ((ch > 32) && (ch < 127))
  286.       return true;
  287.  
  288.     return false;
  289.   }  
  290.  
  291.  
  292.   private static boolean writeStringToFile(String outFnm, String msgStr)
  293.   // zapisanie do pliku
  294.   {
  295.     if (!canOverWrite(outFnm))
  296.       return false;
  297.  
  298.     try {
  299.       FileWriter out = new FileWriter( new File(outFnm) );
  300.       out.write(msgStr);
  301.       out.close();
  302.             return true;
  303.     }
  304.     catch(IOException e)
  305.     {  
  306.        return false;
  307.     }
  308.   }  
  309.  
  310.  
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement