Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // HOW TO USE:
- //
- // java -jar hate3.jar INPUT_DIRECTORY OUTPUT_DIRECTORY fd1c1b2f34a0d1d246be3ba9bc5af022e83375f315a0216085d3013a il
- //
- // SOURCE
- //
- // https://github.com/veronicakulikova/playground/blob/master/CodePlayground/hate3/src/main/java/dev/frag/hate3/Hate3.java
- package dev.frag.hate3;
- import javax.crypto.*;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.security.InvalidAlgorithmParameterException;
- import java.security.InvalidKeyException;
- import java.security.NoSuchAlgorithmException;
- import java.util.EnumSet;
- import java.util.Set;
- import static java.lang.System.err;
- import static java.lang.System.out;
- public class Hate3 {
- private static final int[] CONST_IMG = {'d', 'r', 'o', 'i', 'd', 'p', 'n', 'g'};
- private static final int[] CONST_LUA = {'d', 'r', 'o', 'i', -1, 'l', 'u', 'a'};
- private static final Cipher LOCL_IMG_CIPHER;
- private static final Cipher LOCL_LUA_CIPHER;
- // #NeedToArticulateSpadeStory
- private static final String VERSION = "1.1.0";
- private static final String SYNTAX = "java -jar hate3.jar";
- private static final String USAGE = "Usage:%n" +
- "\t%s <srcdir> <targdir> <key> [flags:i|l]%n" +
- "Where flags indicate:%n" +
- "\ti: images%n" +
- "\tl: lua%n" +
- "e.g.%n" +
- "\tjava -jar hate3.jar /Volumes/Terrigen/Fitz/LoNi /Users/RV/Documents/LoNiOut keyhere il%n" +
- "Hate Tool #3, version %s%n" +
- "Written by Jake%n";
- static {
- Cipher imgCipher = null;
- Cipher luaCipher = null;
- try {
- imgCipher = Cipher.getInstance("Blowfish/ECB/NoPadding");
- luaCipher = Cipher.getInstance("Blowfish/CBC/NoPadding");
- } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
- err.println("[FAIL] Could not find Blowfish/ECB/NoPadding cipher.\n" +
- "Initialization has failed.");
- e.printStackTrace();
- System.exit(117); // MCC coming to PC~~~
- }
- LOCL_IMG_CIPHER = imgCipher;
- LOCL_LUA_CIPHER = luaCipher;
- }
- private static void printUsage() {
- out.printf(USAGE, SYNTAX, VERSION);
- }
- public static void main(String[] args) {
- if (args.length != 3 && args.length != 4) {
- printUsage();
- System.exit(119);
- }
- try {
- File rootSrcDir = new File(args[0]).getAbsoluteFile();
- Set<Format> formats = args.length == 4 ? EnumSet.noneOf(Format.class) : null;
- if (args.length == 4)
- args[3].toLowerCase().chars().forEach(v -> {
- switch (v) {
- case 'i':
- formats.add(Format.IMAGE);
- break;
- case 'l':
- formats.add(Format.LUA);
- break;
- default:
- err.printf("[FAIL] Unrecognized flag %c%n", v);
- printUsage();
- System.exit(121);
- }
- });
- operateRecurse(rootSrcDir, new File(args[1]).getAbsoluteFile(), rootSrcDir, new SecretKeySpec(args[2].substring(0, Math.min(args[2].length(), 56)).getBytes(StandardCharsets.US_ASCII), "Blowfish"), formats);
- } catch (IOException | InvalidKeyException | BadPaddingException | ShortBufferException | IllegalBlockSizeException | InvalidAlgorithmParameterException e) {
- err.printf("[FAIL] Encountered exception: %s%n", e.getMessage() == null ? "<no message provided>" : e.getMessage());
- e.printStackTrace();
- System.exit(120);
- }
- }
- private static void operateRecurse(File rootSrcDir, File rootTarDir, File curFile, SecretKeySpec keyspec, Set<Format> formats) throws IOException, InvalidKeyException, BadPaddingException, ShortBufferException, IllegalBlockSizeException, InvalidAlgorithmParameterException {
- out.print('.');
- // Check directory
- File[] files = curFile.listFiles();
- if (files != null)
- for (File f : files)
- operateRecurse(rootSrcDir, rootTarDir, f, keyspec, formats);
- // Pre-kick
- if (!curFile.exists() || !curFile.isFile() || curFile.length() < 8 || curFile.length() > Integer.MAX_VALUE)
- return;
- // Start
- byte[] srcBuf;
- byte[] targBuf;
- int targLen;
- Format format;
- byte[] temp8;
- Path rel;
- try (FileInputStream fis = new FileInputStream((curFile))) {
- fis.getChannel().position(curFile.length() - 8);
- // Check constant (images)
- temp8 = new byte[8];
- int rem = 8, read;
- do {
- read = fis.read(temp8, 8 - rem, rem);
- if (read >= 0)
- rem -= read;
- } while (rem > 0 && read >= 0);
- // Kick
- if (rem != 0)
- return;
- // Format det
- if (checkArr8(temp8, CONST_IMG))
- format = Format.IMAGE;
- else if (checkArr8(temp8, CONST_LUA))
- format = Format.LUA;
- else
- return;
- if (formats != null && !formats.contains(format))
- return;
- rel = rootSrcDir.toPath().relativize(curFile.toPath());
- out.printf("%n[INFO] Selected %s%n", rel.toString());
- targLen = (int) curFile.length() - 8;
- // Get buffers
- out.printf("[INFO] \tAcquiring buffers...%n");
- srcBuf = new byte[targLen];
- targBuf = new byte[targLen];
- // Read
- out.printf("[INFO] \tReading file into buffer...%n");
- fis.getChannel().position(0);
- rem = targLen;
- do {
- read = fis.read(srcBuf, targLen - rem, rem);
- if (read >= 0)
- rem -= read;
- } while (rem > 0 && read >= 0);
- }
- // Close input
- out.printf("[INFO] \tDecrypting...%n");
- // Decrypt
- switch (format) {
- case IMAGE:
- LOCL_IMG_CIPHER.init(Cipher.DECRYPT_MODE, keyspec);
- LOCL_IMG_CIPHER.doFinal(srcBuf, 0, targLen, targBuf, 0);
- break;
- case LUA:
- LOCL_LUA_CIPHER.init(Cipher.DECRYPT_MODE, keyspec, new IvParameterSpec(new byte[8]));
- LOCL_LUA_CIPHER.doFinal(srcBuf, 0, targLen, targBuf, 0);
- break;
- }
- // Ops
- if (format == Format.LUA)
- targLen -= Byte.toUnsignedInt(temp8[4]);
- // Write
- File targ = Paths.get(rootTarDir.getAbsolutePath(), rel.toString()).toFile();
- File parent = targ.getParentFile();
- if (!parent.exists() && !parent.mkdirs()) {
- err.printf("[FAIL] Failed to create directory %s%n", parent.getAbsolutePath());
- System.exit(118);
- }
- out.printf("[INFO] \tWriting %s...%n", targ.getAbsolutePath());
- FileOutputStream fos = new FileOutputStream(targ, false);
- fos.write(targBuf, 0, targLen);
- // Close output
- fos.close();
- // OK
- out.printf("[INFO] \tDone.%n");
- }
- private static boolean checkArr8(byte[] data, int[] con) {
- for (int i = 0; i < 8; i++)
- if (con[i] != -1 && Byte.toUnsignedInt(data[i]) != con[i])
- return false;
- return true;
- }
- private enum Format {
- IMAGE,
- LUA
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment