Skip to content

Instantly share code, notes, and snippets.

@lk86
Created February 5, 2016 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lk86/98d418ebd1725087e433 to your computer and use it in GitHub Desktop.
Save lk86/98d418ebd1725087e433 to your computer and use it in GitHub Desktop.
Misc50
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintStream;
public class Hide
{
public Hide() {}
protected void steg(String paramString1, String paramString2)
{
BufferedImage localBufferedImage1 = loadImage(paramString2 + ".png");
BufferedImage localBufferedImage2 = steg(paramString1, localBufferedImage1);
try
{
javax.imageio.ImageIO.write(localBufferedImage2, "png", new java.io.File(paramString2 + "_out.png"));
}
catch (IOException localIOException) {
throw new RuntimeException("Unable to write image!");
}
}
protected BufferedImage steg(String paramString, BufferedImage paramBufferedImage)
{
paramString = paramString.length() + ":" + paramString;
if (paramString.length() * 8 > paramBufferedImage.getWidth() * paramBufferedImage.getHeight()) {
System.out.println("There won't be enough space to store this message!");
System.out.println("Message length: " + paramString.length() + " bytes. " + "Image can hold a maximum of " + paramBufferedImage.getWidth() * paramBufferedImage.getHeight() / 8);
throw new RuntimeException("There won't be enough space to store this message!");
}
byte[] arrayOfByte1 = paramString.getBytes();
Point localPoint = new Point(0, 0);
for (int k : arrayOfByte1) {
for (int m = 0; m < 8; m++) {
if ((k & 0x80) == 128) {
paramBufferedImage.setRGB(x, y, setLeastSignificantBit(paramBufferedImage.getRGB(x, y), true));
}
else {
paramBufferedImage.setRGB(x, y, setLeastSignificantBit(paramBufferedImage.getRGB(x, y), false));
}
k <<= 1;
movePointer(localPoint, paramBufferedImage);
}
}
return paramBufferedImage;
}
protected int setLeastSignificantBit(int paramInt, boolean paramBoolean)
{
paramInt >>= 1;
paramInt <<= 1;
if (paramBoolean) {
paramInt++;
}
return paramInt;
}
protected void movePointer(Point paramPoint, BufferedImage paramBufferedImage)
{
if (x == paramBufferedImage.getWidth() - 1) {
x = -1;
y += 1;
}
x += 1;
if (y == paramBufferedImage.getHeight()) {
throw new RuntimeException("Pointer moved beyond the end of the image");
}
}
private BufferedImage loadImage(String paramString)
{
try {
return javax.imageio.ImageIO.read(new java.io.File(paramString));
}
catch (IOException localIOException)
{
System.out.println("Unable to load \"" + paramString + "\"");
System.exit(0);
}
return null;
}
public static void main(String[] paramArrayOfString)
{
if (paramArrayOfString.length < 2) {
System.out.println("Input Arguments Is Not Valid.");
System.out.println("Run By 'java -jar Hide.jar `file-path` `message`'");
return;
}
System.out.println("Welcome!\nDecrypt The Image And Capture The Flag!");
Hide localHide = new Hide();
localHide.steg(paramArrayOfString[1], paramArrayOfString[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment