Stéganographie

Résolu
pikamo Messages postés 34 Date d'inscription dimanche 21 mars 2010 Statut Membre Dernière intervention 21 novembre 2012 - 20 nov. 2012 à 23:41
pikamo Messages postés 34 Date d'inscription dimanche 21 mars 2010 Statut Membre Dernière intervention 21 novembre 2012 - 21 nov. 2012 à 21:08
salut,

j'ai trouvé un code de stéganographie technique LSB ...

console d'erreur :
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at org.jasypt.util.binary.BasicBinaryEncryptor.(BasicBinaryEncryptor.java:68)
at StegCrypt.encryptMsgBytes(StegCrypt.java:201)
at StegCrypt.hide(StegCrypt.java:92)
at Main.main(Main.java:40)


voila la classe :

public class StegCrypt
{
  private static final int PASSWORD_LEN = 10;
  private static final int MAX_INT_LEN = 4;

  private static final int DATA_SIZE = 8;   
           // number of image bytes required to store one stego byte

  private static Random rand = new Random();    // used for password generation



  public static boolean hide(String textFnm, String imFnm)
  /* hide message read from textFnm inside image read from imFnm;
     the resulting image is stored in Msg.png */
  {
    // read in the message as a byte array
    byte[] msgBytes = readMsgBytes(textFnm); 
    if (msgBytes == null)
      return false;

    // generate a password
    String password = genPassword();
    byte[] passBytes = password.getBytes();

    // use password to encrypt the message
    byte[] encryptedMsgBytes = encryptMsgBytes(msgBytes, password);
    if (encryptedMsgBytes == null)
      return false;

    byte[] stego = buildStego(passBytes, encryptedMsgBytes);

    // access the image's data as a byte array
    BufferedImage im = loadImage(imFnm);
    if (im == null)
      return false;
    byte imBytes[] = accessBytes(im);

    if (!singleHide(imBytes, stego))   // im is modified with the stego
      return false;

    // store the modified image in <fnm>Msg.png
    String fnm = getFileName(imFnm);
    return writeImageToFile( fnm + "Msg.png", im);
  }  // end of hide()
    


  private static byte[] readMsgBytes(String fnm)
  // Read in fnm, returning it a byte array.
  {
    //String inputText = readTextFile(fnm);
    String inputText = readTextFile(fnm);
    if ((inputText null) || (inputText.length() 0))
      return null;

    return inputText.getBytes();
  }  // end of readMsgBytes()


 public static String loadFile(File f) {
     StringWriter out = new StringWriter();
    try {
       BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
       
       int b;
       while ((b=in.read()) != -1)
           out.write(b);
       out.flush();
       out.close();
       in.close();
      
    }
    catch (IOException ie)
    {
         ie.printStackTrace(); 
    }
    System.out.println("Read in " + f);
    
   return out.toString();
}
  private static String readTextFile(String fnm)
  // read in fnm, returning it as a single string
  {
    BufferedReader br = null;
    StringWriter out = null;
 StringBuilder  stringBuilder = new StringBuilder();
    try {
      
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(fnm));
       out = new StringWriter();
       int b;
       while ((b=in.read()) != -1)
       out.write(b);
       out.toString();
       out.flush();
       out.close();
       in.close();
      
    }
    catch (IOException ie)
    {
         ie.printStackTrace(); 
    }
        System.out.println("Read in " + fnm);
        
         return out.toString();
    
   
    
  }  // end of readTextFile()
    


  private static String genPassword()
  // return a password of random characters of length PASSWORD_LEN
  {
    String availChars = "abcdefghjklmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";   // chars available for password

    StringBuffer sb = new StringBuffer(PASSWORD_LEN);
    for (int i=0; i 
       <size of encrypted binary message> 
       <encrypted binary message>
  */
  { 
    byte[] lenBs = intToBytes(encryptedMsgBytes.length);

    int totalLen = passBytes.length + lenBs.length + encryptedMsgBytes.length;
    byte[] stego = new byte[totalLen];    // for holding the resulting stego

    // combine the 3 fields into one byte array
    // public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
    int destPos = 0;
    System.arraycopy(passBytes, 0, stego, destPos, passBytes.length);        // password
    destPos += passBytes.length;
    System.arraycopy(lenBs, 0, stego, destPos, lenBs.length);   // length of encrypted binary message
    destPos += lenBs.length;
    System.arraycopy(encryptedMsgBytes, 0, stego, destPos, encryptedMsgBytes.length);   // encrypted binary message

    // System.out.println("Num. pixels to store fragment " + i + ": " + totalLen*DATA_SIZE);
    return stego;
  }  // end of buildStego()



  private static byte[] intToBytes(int i)
  // split integer i into a MAX_INT_LEN-element byte array
  {
    // 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);

    // for (int j=0; j < integerBs.length; j++)
    //  System.out.println(" integerBs[ " + j + "]: " + integerBs[j]);

    return integerBs;
  }  // end of intToBytes()



  private static BufferedImage loadImage(String imFnm)
  // read the image from the imFnm file
  {
    BufferedImage im = null;
    try {
      im = ImageIO.read( new File(imFnm) );
      System.out.println("Read " + imFnm);
    } 
    catch (IOException e) 
    { System.out.println("Could not read image from " + imFnm);  }

    return im;
  }   // end of loadImage()



  private static byte[] accessBytes(BufferedImage image)
  // access the data bytes in the image
  {
    WritableRaster raster = image.getRaster();
    DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer();
    return buffer.getData();
  }  // end of accessBytes()



  private static boolean singleHide(byte[] imBytes, byte[] stego)
  // store stego in image bytes
  {
    int imLen = imBytes.length;
    System.out.println("Byte length of image: " + imLen);

    int totalLen = stego.length;
    System.out.println("Total byte length of info: " + 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;
    }

    hideStego(imBytes, stego, 0);  // hide at start of image
    return true;
  }  // end of singleHide()
    


  private static void hideStego(byte[] imBytes, byte[] stego, int offset)
  // store stego in image starting at byte posn offset
  {
    for (int i = 0; i < stego.length; i++) {       // loop through stego
      int byteVal = stego[i];
      for(int j=7; j >= 0; j--) {    // loop through the 8 bits of each 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++;
      }
    }
  }  // end of hideStego()




  private static String getFileName(String fnm)
  // extract the name from the filename without its suffix
  {
    int extPosn = fnm.lastIndexOf('.');
    if (extPosn == -1) {
      System.out.println("No extension found for " + fnm);
      return fnm;   // use the original file name
    }

    return fnm.substring(0, extPosn);
  }  // end of getFileName()




  private static boolean writeImageToFile(String outFnm, BufferedImage im)
  // save the im image in a PNG file called outFnm
  {
    if (!canOverWrite(outFnm))
      return false;

    try {
      ImageIO.write(im, "png", new File(outFnm));
      System.out.println("Image written to PNG file: " + outFnm);
      return true;
    } 
    catch(IOException e)
    { System.out.println("Could not write image to " + outFnm); 
      return false;
    }
  } // end of writeImageToFile();



  private static boolean canOverWrite(String fnm)
  /* If fnm already exists, get a response from the
     user about whether it should be overwritten or not. */
  {
    File f = new File(fnm);
    if (!f.exists())
      return true;     // can overewrite since the file is new

    // prompt the user about whether the file can be overwritten 
    Scanner in = new Scanner(System.in);
    String response;
    System.out.print("File " + fnm + " already exists. ");
    while (true) {
      System.out.print("Overwrite (y|n)? ");
      response = in.nextLine().trim().toLowerCase();
      if (response.startsWith("n"))  // no
        return false;
      else if (response.startsWith("y"))  // yes
        return true;
    }
  }  // end of canOverWrite()



  // --------------------------- reveal a message -----------------------------------


  public static boolean reveal(String imFnm)
  /* Retrieve the hidden message from imFnm from the beginning
     of the image.  */
  {
    // get the image's data as a byte array
    BufferedImage im = loadImage(imFnm);
    if (im == null)
      return false;
    byte[] imBytes = accessBytes(im);
    int imLen = imBytes.length;
    System.out.println("Byte Length of image: " + imLen);

    String msg = extractMsg(imBytes, 0);
    if (msg != null) {
      String fnm = getFileName(imFnm);
      return writeStringToFile(fnm + ".txt", msg);  // save message in a text file
    }
    else {
      System.out.println("No message found");
      return false;
    }
  }  // end of reveal()
    


  private static String extractMsg(byte[] imBytes, int offset)
  /* retrieve hidden message from the image by extracting its password and
     the encrypted binary message length, and using them to build the message.
  */
  {
    String password = getPassword(imBytes, offset);
    if (password == null)
      return null;

    offset += PASSWORD_LEN*DATA_SIZE;   // move past password
    int msgLen = getMsgLength(imBytes, offset);   // get the encrypted message length 
    if (msgLen == -1)
      return null;
        
    offset += MAX_INT_LEN*DATA_SIZE;   // move past message length
    return getMessage(imBytes, msgLen, password, offset);
  }  // end of extractMsg()



  private static String getPassword(byte[] imBytes, int offset)
  // extract the password from the image
  {
    byte[] passBytes = extractHiddenBytes(imBytes, PASSWORD_LEN, offset); 
    if (passBytes == null)
      return null;
    String password = new String(passBytes);

    // check the password is all characters
    if (isPrintable(password)) {
    // System.out.println("Found password: "" + password + """);
      return password;
    }
    else
      return null;
  }  // end of getPassword()




  private static int getMsgLength(byte[] imBytes, int offset)
  // retrieve encrypted binary message length from the image
  {
    byte[] lenBytes = extractHiddenBytes(imBytes, MAX_INT_LEN, offset);
              // get the binary message length as a byte array
    if (lenBytes == null)
      return -1;

    // for (int j=0; j < lenBytes.length; j++)
    //  System.out.println(" lenBytes[ " + j + "]: " + lenBytes[j]);

    // convert the byte array into an integer
    int msgLen = ((lenBytes[0] & 0xff) << 24) | 
                 ((lenBytes[1] & 0xff) << 16) | 
                 ((lenBytes[2] & 0xff) << 8) | 
                  (lenBytes[3] & 0xff);
    // System.out.println("Message length: " + msgLen);

    if ((msgLen <= 0) || (msgLen > imBytes.length))  {
      System.out.println("Incorrect message length");
      return -1;
    }
    // else
    //  System.out.println("Revealed encrypted message length: " + msgLen);

    return msgLen;
  }  // end of getMsgLength()




  private static String getMessage(byte[] imBytes, int msgLen, String password, int offset)
  /* Return a message. First extract an encrypted binary message
     of size msgLen from the image, decrypt it with the password, and 
     convert it to a string 
  */
  {
    byte[] enMsgBytes = extractHiddenBytes(imBytes, msgLen, offset); 
           // the encrypted message is msgLen bytes long
    if (enMsgBytes == null)
      return null;

    // decrypt the message using the password    
    BasicBinaryEncryptor bbe = new BasicBinaryEncryptor();
    bbe.setPassword(password);
    byte[] msgBytes = null;
    try {
      msgBytes = bbe.decrypt(enMsgBytes);
      // System.out.println("Decrypted message length: " + msgBytes.length);
    }
    catch(Exception e)   // in case the decryption fails
    { System.out.println("Problem decrypting message");
      return null;
    }

    String msg = new String(msgBytes);

    // check the message is all characters
    if (isPrintable(msg)) {
    // System.out.println("Found message: "" + msg + """);
      return msg;
    }
    else
      return null;
  }  // end of getMessage()



  private static byte[] extractHiddenBytes(byte[] imBytes, int size, int offset)
  // extract 'size' hidden data bytes, starting from 'offset' in the image bytes
  {
    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 each hidden byte
      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 1 left; store right most bit of image byte
        offset++;
      }
    }
    return hiddenBytes;
  }  // end of extractHiddenBytes()



  private static boolean isPrintable(String str)
  // is the string printable?
  {
    for (int i=0; i < str.length(); i++)
      if (!isPrintable(str.charAt(i))) {
        System.out.println("Unprintable character found");
        return false;
      }
    return true;
  }  // end of isPrintable()



  private static boolean isPrintable(int ch)
  // is ch a 7-bit ASCII character that could (sensibly) be printed?
  {
    if (Character.isWhitespace(ch) && (ch < 127))  // whitespace, 7-bit
      return true;
    else if ((ch > 32) && (ch < 127))
      return true;

    return false;
  }  // end of isPrintable()



  private static boolean writeStringToFile(String outFnm, String msgStr)
  // write the message string into the outFnm text file
  {
    if (!canOverWrite(outFnm))
      return false;

    try {
      FileWriter out = new FileWriter( new File(outFnm) );
      out.write(msgStr);
      out.close();
      System.out.println("Message written to " + outFnm);
      return true;
    }
    catch(IOException e)
    {  System.out.println("Could not write message to " + outFnm);  
       return false;
    }
  }  // end of writeStringToFile()


}  // end of StegCrypt class

j'aime bien m'aider à résoudre ce problème
merci d'avance

3 réponses

pikamo Messages postés 34 Date d'inscription dimanche 21 mars 2010 Statut Membre Dernière intervention 21 novembre 2012
21 nov. 2012 à 21:08
problème résolu merci
j'ai téléchargé ces JARs
-org.apache.commons.lang
-jasypt-1.5
-icu4j-3.4.4
3
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
21 nov. 2012 à 09:53
Salut,

Tu as mal configuré ton classpath, il faut que tu ajours les jars qui te permettent de tatouer tes données.

Tu as ajouté quels jars ? N'aurais tu pas oublié les dépendances ?
0
pikamo Messages postés 34 Date d'inscription dimanche 21 mars 2010 Statut Membre Dernière intervention 21 novembre 2012
21 nov. 2012 à 20:41
salut

merci j'ai installé le jar "jasypt-1.3"...
mais le promléme est dans cette classe
 private static String readTextFile(String fnm)

car avec ce ligne
System.out.println(sb.toString());

donner le nom du image
lena.png
 le nom du image est lena.png
donner le nom du texte
b.doc
 le nom du texte est b.doc
 le type est StegCrypt 
Read in b.doc

PK  [Conte!nt_Types].xml 


la resultat de System.out.println(sb.toString()); est des petites rectangles

Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
0
Rejoignez-nous