Created
November 12, 2014 23:03
-
-
Save kylepls/8574a1a0086086325225 to your computer and use it in GitHub Desktop.
Encode crap I guess...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package useful.encryption; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Random; | |
import java.util.Scanner; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.security.SecureRandom; | |
import javax.crypto.Cipher; | |
import javax.crypto.spec.SecretKeySpec; | |
import javax.imageio.ImageIO; | |
/** | |
* Created by Kyle at Oct 29, 2014 | |
*/ | |
public class Encripter { | |
public static final int blue = -16771881; | |
public static void main(String[] args) { | |
System.out.println("E/D?"); | |
Scanner s = new Scanner(System.in); | |
String n = s.nextLine(); | |
if (n.equalsIgnoreCase("e")) { | |
System.out.println("Enter a string:"); | |
String in = s.nextLine(); | |
s.close(); | |
System.out.println(); | |
Data d = null; | |
try { | |
d = encrypt(in); | |
System.out.print("Salt: (" + d.getSaltString().length() + ")"); | |
System.out.println(d.getSaltString()); | |
System.out.println(); | |
System.out.print("Encrypted: "); | |
System.out.println(d.getDataString()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} else { | |
System.out.println("Enter a string to decrypt..."); | |
String string = s.nextLine(); | |
System.out.println("Grabbing salt from out.jpg..."); | |
//System.out.println("Enter the salt"); | |
//String salt = s.nextLine(); | |
System.out.println("Decrypting..."); | |
String salt = null; | |
try { | |
BufferedImage i = ImageIO.read(new File("out.png")); | |
int x = 0; | |
int y = 0; | |
List<Byte> bytes = new ArrayList<>(); | |
System.out.println(x + " " + y + " " + i.getMinY()); | |
while (i.getRGB(x, y) != blue) { | |
if (x > i.getWidth()) { | |
y++; | |
x = 0; | |
} | |
bytes.add((byte) Math.abs((Math.abs(blue)+i.getRGB(x, y)))); | |
//System.out.println("Got: " + salty[run] + " / " + i.getRGB(x, y)); | |
x++; | |
} | |
byte[] b = new byte[bytes.size()]; | |
for (int j = 0; j < b.length; j++) { | |
b[j] = bytes.get(j); | |
} | |
salt = new String(b); | |
System.out.println("Salt: " + salt); | |
} catch (IOException e1) { | |
e1.printStackTrace(); | |
} | |
System.out.println(); | |
try { | |
System.out.println(decrypt(fromString(string), fromString(salt))); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
public static Data encrypt(String message) throws Exception { | |
final Random r = new SecureRandom(); | |
byte[] salt = new byte[16]; | |
r.nextBytes(salt); | |
SecretKeySpec key = new SecretKeySpec(salt, "AES"); | |
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE"); | |
cipher.init(Cipher.ENCRYPT_MODE, key); | |
Data d = new Data(); | |
d.data = cipher.doFinal(message.getBytes()); | |
d.salt = salt; | |
System.out.println("Encrypting salt to image..."); | |
BufferedImage i = ImageIO.read(new File("blue.jpg")); | |
for (int x = 0; x < i.getWidth(); x++) { | |
for (int y = 0; y < i.getHeight(); y++) { | |
i.setRGB(x, y, blue); | |
} | |
} | |
int x = 0; | |
int y = 0; | |
for (byte b : d.getSaltString().getBytes()) { | |
if (x > i.getWidth()) { | |
x = 0; | |
y++; | |
} | |
i.setRGB(x, y, blue-b); | |
x++; | |
} | |
ImageIO.write(i, "png", new File("out.png")); | |
return d; | |
} | |
public static String decrypt(byte[] message, byte[] salt) throws Exception { | |
SecretKeySpec key = new SecretKeySpec(salt, "AES"); | |
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE"); | |
cipher.init(Cipher.DECRYPT_MODE, key); | |
return new String(cipher.doFinal(message)); | |
} | |
public static byte[] fromString(String s) { | |
List<Byte> res = new ArrayList<Byte>(); | |
for (String string : s.split(";")) { | |
res.add(Byte.parseByte(string)); | |
} | |
byte[] bytes = new byte[res.size()]; | |
for (int i = 0; i < bytes.length; i++) { | |
bytes[i] = res.get(i); | |
} | |
return bytes; | |
} | |
public static class Data { | |
byte[] salt; | |
byte[] data; | |
public String getSaltString() { | |
StringBuilder bu = new StringBuilder(); | |
for (byte b : salt) { | |
bu.append(b+";"); | |
} | |
return bu.toString(); | |
} | |
public String getDataString() { | |
StringBuilder bu = new StringBuilder(); | |
for (byte b : data) { | |
bu.append(b+";"); | |
} | |
return bu.toString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment