Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (2)

Powered by Vanilla. Made with Bootstrap.
[Java] ImageSteg V0.2, hide one image within another
  • Deque
    Posts: 78
    Hello guys,

    I made a little steganography program. It uses the last 2-4 bits of every pixelcolor to save one image in another. Because the information is in the least significant bits you can't see the hidden image with the eye.

    Of course the image quality suffers since some bits are lost in both pictures. Depending on the quality you choose in the program either the hidden image looks good and the image you see looks bad (good quality setting) or the other way around (bad quality setting). Once you have prepared an image, you can reveal the hidden image again. You have to choose the same quality option for extraction that has done the hiding, otherwise you will get whoosh.

    Screenshot:
    [spoiler]http://s7.directupload.net/images/110620/bsa9epnd.png[/spoiler]


    The main algorithm (excerpt):
    [spoiler]
    package control;

    import gui.MainFrame;

    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.Transparency;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.lang.reflect.InvocationTargetException;

    import javax.swing.SwingUtilities;

    public class ImageSteno {

    public static final int ALPHA = 0;
    public static final int RED = 1;
    public static final int GREEN = 2;
    public static final int BLUE = 3;

    private BufferedImage mask;
    private BufferedImage toHide;
    private BufferedImage toExtract;

    private Quality quality = Quality.GOOD;

    public static void main(String[] args) throws IOException {

    try {
    SwingUtilities.invokeAndWait(new Runnable() {
    @Override
    public void run() {
    new MainFrame();
    }
    });
    } catch (InterruptedException e) {
    e.printStackTrace();
    } catch (InvocationTargetException e) {
    e.printStackTrace();
    }
    }

    public void setQuality(Quality quality) {
    this.quality = quality;
    }

    public BufferedImage extractHiddenImage() throws NoImageException {
    if (toExtract == null) {
    throw new NoImageException();
    }
    return extractHiddenImage(toExtract);
    }

    public BufferedImage extractHiddenImage(BufferedImage img) {
    BufferedImage imgCopy = copyImage(img);
    for (int x = 0; x < imgCopy.getWidth(); x++) {
    for (int y = 0; y < imgCopy.getHeight(); y++) {
    int rgb = imgCopy.getRGB(x, y);
    rgb = extractRGB(rgb);
    imgCopy.setRGB(x, y, rgb);
    }
    }
    return imgCopy;
    }

    public static BufferedImage copyImage(BufferedImage image) {
    BufferedImage destImage = new BufferedImage(image.getWidth(),
    image.getHeight(), BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = destImage.createGraphics();
    g.drawImage(image, null, 0, 0);
    g.dispose();
    return destImage;
    }

    public BufferedImage hideImage() throws NoImageException {
    if (mask == null || toHide == null) {
    throw new NoImageException();
    }
    BufferedImage maskCopy = getScaledInstance(copyImage(mask),
    toHide.getWidth(), toHide.getHeight(),
    RenderingHints.VALUE_INTERPOLATION_BICUBIC, false);

    for (int x = 0; x < toHide.getWidth(); x++) {
    for (int y = 0; y < toHide.getHeight(); y++) {
    int destRGB = toHide.getRGB(x, y);
    int oldRGB = maskCopy.getRGB(x, y);
    int newRGB = computeNewRGB(oldRGB, destRGB);
    maskCopy.setRGB(x, y, newRGB);
    }
    }
    return maskCopy;
    }

    private int extractRGB(int rgb) {
    int[] rgbArr = getRGBArray(rgb);
    for (int i = 1; i <= 3; i++) {
    rgbArr[i] = rgbArr[i] & quality.leastBitsSum;
    rgbArr[i] = rgbArr[i] << quality.shift;
    }
    return getRGBFromArray(rgbArr);
    }

    private int computeNewRGB(int oldRGB, int destRGB) {
    int[] dest = getRGBArray(destRGB);
    int[] old = getRGBArray(oldRGB);
    for (int i = 1; i <= 3; i++) {
    old[i] = old[i] & quality.cuttingMask;
    dest[i] = dest[i] >> quality.shift;
    old[i] += dest[i];
    }
    return getRGBFromArray(old);
    }

    private int getRGBFromArray(int[] rgbArr) {
    return new Color(rgbArr[RED], rgbArr[GREEN], rgbArr[BLUE]).getRGB();
    }

    private int[] getRGBArray(int rgb) {
    return new int[] { (rgb >> 24) & 0xff, (rgb >> 16) & 0xff,
    (rgb >> 8) & 0xff, rgb & 0xff };
    }
    }

    public enum Quality {

    GOOD(4), MEDIUM(3), BAD(2);

    public int leastBitsSum;
    public int shift;
    public int cuttingMask;

    private Quality(int bits){
    computeLeastBitsSum(bits);
    computeCuttingMask(bits);
    computeShift(bits);
    }

    private void computeShift(int bits) {
    shift = 8 - bits;
    }

    private void computeLeastBitsSum(int bits) {
    leastBitsSum = 0;
    for(int i = 0; i < bits; i++){
    leastBitsSum += Math.pow(2, i);
    }
    }

    private void computeCuttingMask(int bits) {
    cuttingMask = 255 - leastBitsSum;
    }
    }

    [/spoiler]


    LOC:
    600 without empty lines and comments
    733 with empty lines and comments

    License:
    [spoiler]

    Copyright 2011 Deque at http://haxme.org, http://poisonhack.info/, http://www.iexploit.org/. All rights reserved.

    Redistribution and use in source and binary forms, with or without modification, are
    permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this list of
    conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright notice, this list
    of conditions and the following disclaimer in the documentation and/or other materials
    provided with the distribution.

    THIS SOFTWARE IS PROVIDED BY Deque at http://haxme.org, http://poisonhack.info/, http://www.iexploit.org/ ``AS IS'' AND ANY EXPRESS OR IMPLIED
    WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
    ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
    ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

    The views and conclusions contained in the software and documentation are those of the
    authors and should not be interpreted as representing official policies, either expressed
    or implied, of Deque at http://haxme.org, http://poisonhack.info, http://www.iexploit.org//.

    [/spoiler]


    Have fun.

    Deque

    Download: http://www.mediafire.com/?na7d13i32gc7981
  • Sh3llc0d3
    Posts: 1,910
    Hi Deque, looks an great program from a crypto/coding point of view!
  • Deque
    Posts: 78
    Thanks, Sh3llc0d3. I plan to improve it by adding a cipher and give the option to hide arbitrary files in images.
  • Sh3llc0d3
    Posts: 1,910
    Well I'm not a java coder but I've gotta admit its unusal to see such a program and quite refreshing to see some technical/algorithmic coding can't wait to see version 2 :)
  • Xin
    Posts: 3,251
    Looks like a really good quality stegography. You should add support for making images really small to hide it further.
    Xin
  • Deque
    Posts: 78
    Hi Xinapse,
    do you mean compression?