Skip to content

Instantly share code, notes, and snippets.

@GeoffYart
Created May 23, 2017 21:55
Show Gist options
  • Save GeoffYart/274949b299b8b787ff639d79a1d3fbaf to your computer and use it in GitHub Desktop.
Save GeoffYart/274949b299b8b787ff639d79a1d3fbaf to your computer and use it in GitHub Desktop.
A simple, 2 end bit steganography program
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.imageio.ImageIO;
public class Steggysteg {
private static Scanner scanner = new Scanner(System.in);
private static BufferedImage readImage(String path) {
BufferedImage image = null;
try {
image = ImageIO.read(new File(path));
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
@SuppressWarnings("resource")
private static BufferedImage encode(BufferedImage image, String path) {
//Reads file
BufferedReader reader = null;
ArrayList<String> lines = new ArrayList<String>();
try {
reader = new BufferedReader(new FileReader(path));
for (String line = ""; line != null; line = reader.readLine()) {
lines.add(line + "\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//Translates to binary
ArrayList<String[]> binary = new ArrayList<String[]>();
for (int i = 0; i < lines.size(); i++) {
String[] array = new String[lines.get(i).length()];
byte[] bytes = lines.get(i).getBytes();
for (int j = 0; j < bytes.length; j++) {
array[j] = Integer.toBinaryString((int)bytes[j]);
while (array[j].length() < 8) {
array[j] = "0" + array[j];
}
}
binary.add(array);
}
//Alters pixels
int x = 0, y = 0;
for (int lineNum = 0; lineNum < binary.size(); lineNum++) {
for (int charNum = 0; charNum < binary.get(lineNum).length; charNum++) {
for (int bitNum = 0; bitNum < 8; bitNum += 2) {
String biPix = Integer.toBinaryString(image.getRGB(x, y));
biPix = biPix.substring(0, biPix.length() - 2);
biPix += binary.get(lineNum)[charNum].substring(bitNum, bitNum + 2);
image.setRGB(x, y, (int)Long.parseLong(biPix, 2));
x++;
if (x == image.getWidth()) {
x = 0;
if (y == image.getHeight() - 1) {
System.out.println("No more space on image.");
break;
}
y++;
}
}
}
}
return image;
}
private static void save(BufferedImage image, String path) {
File outputfile = new File(path);
try {
ImageIO.write(image, "png", outputfile);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void decode(BufferedImage image, String path) {
int size = image.getWidth() * image.getHeight();
size -= size % 8;
ArrayList<String> strings = new ArrayList<String>();
for (int i = 0; i < size; i += 4) {
StringBuilder binaryChar = new StringBuilder();
for (int j = 0; j < 4; j++) {
int x = (i + j) % image.getWidth();
int y = (i + j) / image.getWidth();
String str = Integer.toBinaryString(image.getRGB(x, y));
binaryChar.append(str.substring(str.length() - 2));
}
strings.add(binaryChar.toString());
}
StringBuilder out = new StringBuilder();
for (int i = 0; i < strings.size(); i++) {
String letter = "" + (char)(byte)Integer.parseInt(strings.get(i), 2);
strings.set(i, letter);
out.append(strings.get(i));
}
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(path));
writer.write(out.toString());
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void clear(BufferedImage image, String path) {
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
String biPix = Integer.toBinaryString(image.getRGB(x, y));
biPix = biPix.substring(0, biPix.length() - 2) + "00";
image.setRGB(x, y, (int)Long.parseLong(biPix, 2));
}
}
save(image, path);
}
public static void main(String[] args) {
while (true) {
String choice = "";
while (!choice.equals("1") && !choice.equals("2")
&& !choice.equals("3") && !choice.equals("4")) {
System.out.print("1. Encode\n2. Decode\n3. Clear\n4. Quit\nChoice: ");
choice = scanner.nextLine();
}
if (choice.equals("4")) {
System.exit(1);
} else if (choice.equals("1")) {
System.out.print("Picture path: ");
BufferedImage image = readImage(scanner.nextLine());
System.out.print("File path: ");
image = encode(image, scanner.nextLine());
System.out.print("Save path: ");
save(image, scanner.nextLine());
} else if (choice.equals("2")) {
System.out.print("Picture path: ");
BufferedImage image = readImage(scanner.nextLine());
System.out.print("Cleartext path: ");
decode(image, scanner.nextLine());
} else {
System.out.print("Picture path: ");
String path = scanner.nextLine();
clear(readImage(path), path);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment