Guest User

Hate3.java

a guest
Jul 16th, 2021
969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.90 KB | None | 0 0
  1. // HOW TO USE:
  2. //
  3. //     java -jar hate3.jar INPUT_DIRECTORY OUTPUT_DIRECTORY fd1c1b2f34a0d1d246be3ba9bc5af022e83375f315a0216085d3013a il
  4. //
  5. // SOURCE
  6. //
  7. // https://github.com/veronicakulikova/playground/blob/master/CodePlayground/hate3/src/main/java/dev/frag/hate3/Hate3.java
  8.  
  9. package dev.frag.hate3;
  10.  
  11. import javax.crypto.*;
  12. import javax.crypto.spec.IvParameterSpec;
  13. import javax.crypto.spec.SecretKeySpec;
  14. import java.io.File;
  15. import java.io.FileInputStream;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import java.nio.charset.StandardCharsets;
  19. import java.nio.file.Path;
  20. import java.nio.file.Paths;
  21. import java.security.InvalidAlgorithmParameterException;
  22. import java.security.InvalidKeyException;
  23. import java.security.NoSuchAlgorithmException;
  24. import java.util.EnumSet;
  25. import java.util.Set;
  26.  
  27. import static java.lang.System.err;
  28. import static java.lang.System.out;
  29.  
  30. public class Hate3 {
  31.  
  32.     private static final int[] CONST_IMG = {'d', 'r', 'o', 'i', 'd', 'p', 'n', 'g'};
  33.     private static final int[] CONST_LUA = {'d', 'r', 'o', 'i', -1, 'l', 'u', 'a'};
  34.     private static final Cipher LOCL_IMG_CIPHER;
  35.     private static final Cipher LOCL_LUA_CIPHER;
  36.     // #NeedToArticulateSpadeStory
  37.     private static final String VERSION = "1.1.0";
  38.     private static final String SYNTAX = "java -jar hate3.jar";
  39.     private static final String USAGE = "Usage:%n" +
  40.             "\t%s <srcdir> <targdir> <key> [flags:i|l]%n" +
  41.             "Where flags indicate:%n" +
  42.             "\ti: images%n" +
  43.             "\tl: lua%n" +
  44.             "e.g.%n" +
  45.             "\tjava -jar hate3.jar /Volumes/Terrigen/Fitz/LoNi /Users/RV/Documents/LoNiOut keyhere il%n" +
  46.             "Hate Tool #3, version %s%n" +
  47.             "Written by Jake%n";
  48.  
  49.     static {
  50.         Cipher imgCipher = null;
  51.         Cipher luaCipher = null;
  52.         try {
  53.             imgCipher = Cipher.getInstance("Blowfish/ECB/NoPadding");
  54.             luaCipher = Cipher.getInstance("Blowfish/CBC/NoPadding");
  55.         } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
  56.             err.println("[FAIL] Could not find Blowfish/ECB/NoPadding cipher.\n" +
  57.                     "Initialization has failed.");
  58.             e.printStackTrace();
  59.             System.exit(117); // MCC coming to PC~~~
  60.         }
  61.         LOCL_IMG_CIPHER = imgCipher;
  62.         LOCL_LUA_CIPHER = luaCipher;
  63.     }
  64.  
  65.     private static void printUsage() {
  66.         out.printf(USAGE, SYNTAX, VERSION);
  67.     }
  68.  
  69.     public static void main(String[] args) {
  70.         if (args.length != 3 && args.length != 4) {
  71.             printUsage();
  72.             System.exit(119);
  73.         }
  74.         try {
  75.             File rootSrcDir = new File(args[0]).getAbsoluteFile();
  76.             Set<Format> formats = args.length == 4 ? EnumSet.noneOf(Format.class) : null;
  77.             if (args.length == 4)
  78.                 args[3].toLowerCase().chars().forEach(v -> {
  79.                     switch (v) {
  80.                         case 'i':
  81.                             formats.add(Format.IMAGE);
  82.                             break;
  83.                         case 'l':
  84.                             formats.add(Format.LUA);
  85.                             break;
  86.                         default:
  87.                             err.printf("[FAIL] Unrecognized flag %c%n", v);
  88.                             printUsage();
  89.                             System.exit(121);
  90.                     }
  91.                 });
  92.             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);
  93.         } catch (IOException | InvalidKeyException | BadPaddingException | ShortBufferException | IllegalBlockSizeException | InvalidAlgorithmParameterException e) {
  94.             err.printf("[FAIL] Encountered exception: %s%n", e.getMessage() == null ? "<no message provided>" : e.getMessage());
  95.             e.printStackTrace();
  96.             System.exit(120);
  97.         }
  98.     }
  99.  
  100.     private static void operateRecurse(File rootSrcDir, File rootTarDir, File curFile, SecretKeySpec keyspec, Set<Format> formats) throws IOException, InvalidKeyException, BadPaddingException, ShortBufferException, IllegalBlockSizeException, InvalidAlgorithmParameterException {
  101.         out.print('.');
  102.  
  103.         // Check directory
  104.         File[] files = curFile.listFiles();
  105.  
  106.         if (files != null)
  107.             for (File f : files)
  108.                 operateRecurse(rootSrcDir, rootTarDir, f, keyspec, formats);
  109.  
  110.         // Pre-kick
  111.         if (!curFile.exists() || !curFile.isFile() || curFile.length() < 8 || curFile.length() > Integer.MAX_VALUE)
  112.             return;
  113.  
  114.         // Start
  115.         byte[] srcBuf;
  116.         byte[] targBuf;
  117.         int targLen;
  118.         Format format;
  119.         byte[] temp8;
  120.         Path rel;
  121.         try (FileInputStream fis = new FileInputStream((curFile))) {
  122.             fis.getChannel().position(curFile.length() - 8);
  123.  
  124.             // Check constant (images)
  125.             temp8 = new byte[8];
  126.             int rem = 8, read;
  127.             do {
  128.                 read = fis.read(temp8, 8 - rem, rem);
  129.                 if (read >= 0)
  130.                     rem -= read;
  131.             } while (rem > 0 && read >= 0);
  132.  
  133.             // Kick
  134.             if (rem != 0)
  135.                 return;
  136.  
  137.             // Format det
  138.             if (checkArr8(temp8, CONST_IMG))
  139.                 format = Format.IMAGE;
  140.             else if (checkArr8(temp8, CONST_LUA))
  141.                 format = Format.LUA;
  142.             else
  143.                 return;
  144.             if (formats != null && !formats.contains(format))
  145.                 return;
  146.  
  147.             rel = rootSrcDir.toPath().relativize(curFile.toPath());
  148.             out.printf("%n[INFO] Selected %s%n", rel.toString());
  149.  
  150.             targLen = (int) curFile.length() - 8;
  151.  
  152.             // Get buffers
  153.             out.printf("[INFO] \tAcquiring buffers...%n");
  154.             srcBuf = new byte[targLen];
  155.             targBuf = new byte[targLen];
  156.  
  157.             // Read
  158.             out.printf("[INFO] \tReading file into buffer...%n");
  159.             fis.getChannel().position(0);
  160.             rem = targLen;
  161.             do {
  162.                 read = fis.read(srcBuf, targLen - rem, rem);
  163.                 if (read >= 0)
  164.                     rem -= read;
  165.             } while (rem > 0 && read >= 0);
  166.         }
  167.         // Close input
  168.  
  169.         out.printf("[INFO] \tDecrypting...%n");
  170.  
  171.         // Decrypt
  172.         switch (format) {
  173.             case IMAGE:
  174.                 LOCL_IMG_CIPHER.init(Cipher.DECRYPT_MODE, keyspec);
  175.                 LOCL_IMG_CIPHER.doFinal(srcBuf, 0, targLen, targBuf, 0);
  176.                 break;
  177.             case LUA:
  178.                 LOCL_LUA_CIPHER.init(Cipher.DECRYPT_MODE, keyspec, new IvParameterSpec(new byte[8]));
  179.                 LOCL_LUA_CIPHER.doFinal(srcBuf, 0, targLen, targBuf, 0);
  180.                 break;
  181.         }
  182.  
  183.         // Ops
  184.         if (format == Format.LUA)
  185.             targLen -= Byte.toUnsignedInt(temp8[4]);
  186.  
  187.         // Write
  188.         File targ = Paths.get(rootTarDir.getAbsolutePath(), rel.toString()).toFile();
  189.         File parent = targ.getParentFile();
  190.         if (!parent.exists() && !parent.mkdirs()) {
  191.             err.printf("[FAIL] Failed to create directory %s%n", parent.getAbsolutePath());
  192.             System.exit(118);
  193.         }
  194.         out.printf("[INFO] \tWriting %s...%n", targ.getAbsolutePath());
  195.  
  196.         FileOutputStream fos = new FileOutputStream(targ, false);
  197.         fos.write(targBuf, 0, targLen);
  198.  
  199.         // Close output
  200.         fos.close();
  201.  
  202.         // OK
  203.         out.printf("[INFO] \tDone.%n");
  204.     }
  205.  
  206.     private static boolean checkArr8(byte[] data, int[] con) {
  207.         for (int i = 0; i < 8; i++)
  208.             if (con[i] != -1 && Byte.toUnsignedInt(data[i]) != con[i])
  209.                 return false;
  210.         return true;
  211.     }
  212.  
  213.     private enum Format {
  214.         IMAGE,
  215.         LUA
  216.     }
  217. }
  218.  
Advertisement
Add Comment
Please, Sign In to add comment