Sep 16, 2010 05:50 PM
(This post was last modified: Sep 16, 2010 05:59 PM by MrZander.)
Store an EXE as an image!
Tutorial and Code by MrZander
Hello HF, here is my second tutorial on this site.
After seeing a vb.NET example of storing a string as an image, I decided to tackle this.
This is CLI, I didn't bother making a GUI.
First is our imports:
EXE To Image
Now, get the location of the EXE and open read all of the bytes
This process takes a while depending on size of file!
Next, get the location to save the image and create it
Finally, save it!
This is obviously customizable, change the name of the executable and so on.
Image to EXE
Here is where the fun starts!
Retrieving the EXE from the image:
First, get the location of the image using a scanner
Next, We created a new BufferedImage and populate it with the image selected. Then we get the attributes of the image (Height and Width).
Now for the meat of it, we read the [b]RED value of each pixel in the image.[/b]
And Finally!
We get the directory to export to and we write all the bytes in bytearray b[].
Here is an example of hjsplit(a file splitter) stored as an Image
Spoiler (Click to View)
And there we have it!
Thanks for reading and hope it helps.
FULL SOURCE:
Spoiler (Click to View)
Tutorial and Code by MrZander
Hello HF, here is my second tutorial on this site.
After seeing a vb.NET example of storing a string as an image, I decided to tackle this.
This is CLI, I didn't bother making a GUI.
First is our imports:
Code
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;
import javax.imageio.ImageIO;EXE To Image
Now, get the location of the EXE and open read all of the bytes
This process takes a while depending on size of file!
Code
public static void filetoimage(){
try{
String dir="";
Scanner s = new Scanner(System.in);
System.out.println("Enter the location of your exe.");
dir = s.next();
FileInputStream fstream = new
FileInputStream(dir);
DataInputStream in = new DataInputStream(fstream);
byte b[]= new byte[in.available()];
int count=0;
System.out.println("Reading bytes, please wait . . .");
while (in.available()!=0){
b[count]=(in.readByte());
count=count+1;
}
in.close();Next, get the location to save the image and create it
Code
imagebytes=b;
System.out.println("Enter the DIRECTORY to save your file to.");
saveto = s.next();
int c = 0;
int c2=1;
int size = (int) Math.sqrt(b.length)+2;
int r, g, b2, rgb;
BufferedImage buff = new BufferedImage(size,size, BufferedImage.TYPE_INT_RGB);
System.out.println("Creating image . . . 0/"+Integer.toString(b.length));
for(int y = 0;y<size;y++){
System.out.println("Creating image . . . "+Integer.toString(c)+"/"+Integer.toString(b.length));
for(int x=0;x<size;x++){
if(c<b.length-1){
r=b[c];
g=c2;
b2=c2;
rgb = 0xff<<24 | r<<16 | g<<8 | b2;
buff.setRGB(x, y, rgb); //ALL BYTE DATA IS STORED IN THE RED VALUE
c=c+1;
c2 = c2+1;
if(c2 == 253){
c2=1; //c2 is simply used to change the color of Green and Blue, looks prettier :P
}
}
}
}This is obviously customizable, change the name of the executable and so on.
Code
System.out.println("Saving image.");
ImageIO.write(buff, "png", new File(saveto+"image.png"));
System.out.println("Image saved:");
System.out.println(saveto+"image.png");
System.out.println("Finished.");
}
catch (Exception e){
System.err.println(e.getMessage());
}
}Image to EXE
Here is where the fun starts!
Retrieving the EXE from the image:
First, get the location of the image using a scanner
Code
public static void imagetofile(){
String dir="";
Scanner s = new Scanner(System.in);
System.out.println("Enter the location of your image.");
dir = s.next();Next, We created a new BufferedImage and populate it with the image selected. Then we get the attributes of the image (Height and Width).
Code
try
{
BufferedImage image = ImageIO.read(new File(dir));
byte b[] = new byte[image.getHeight()*image.getWidth()];
int xmax = image.getWidth()-1;
int ymax = image.getHeight()-1;
int count=0;
System.out.println("Opening image. . . ");Now for the meat of it, we read the [b]RED value of each pixel in the image.[/b]
Code
for(int y = 0;y<=ymax;y++){
System.out.println(Integer.toString(count)+"/"+Integer.toString(b.length));
for(int x = 0;x<=xmax;x++){
int pixel = image.getRGB(x,y);
int red = (pixel >> 16) & 0xff;
b[count]=(byte)red;
count=count+1;
}
}And Finally!
We get the directory to export to and we write all the bytes in bytearray b[].
Code
System.out.println("Enter the DIRECTORY to save the exe to: ");
String saveto = s.next();
try
{
FileOutputStream fos = new FileOutputStream(saveto+"executable.exe");
fos.write(b);
fos.close();
}
catch(Exception ex)
{
System.out.println("FileNotFoundException : " + ex);
}
System.out.println("File saved:");
System.out.println(saveto+"executable.exe");
System.out.println("Finished!");
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}Here is an example of hjsplit(a file splitter) stored as an Image
Spoiler (Click to View)
And there we have it!
Thanks for reading and hope it helps.
FULL SOURCE:
Spoiler (Click to View)

![[Image: image.png]](https://dl.dropbox.com/u/9086539/image.png)


