thieumao

SourceCrypter.java

Oct 14th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.50 KB | None | 0 0
  1. package sourcecrypter;
  2.  
  3. import javax.crypto.*;
  4. import java.security.*;
  5. import java.io.*;
  6. import java.util.*;
  7. import javax.tools.*;
  8.  
  9. public class SourceCrypter {
  10.  
  11.     private SecretKey key;
  12.     private DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
  13.     private String inputClassName = "";
  14.     private String crypterClassName;
  15.     //Decrypt Method
  16.     private List<String> dm = new ArrayList<String>();
  17.     //Main Method
  18.     private List<String> mm = new ArrayList<String>();
  19.     //JavaSourceFromString Class
  20.     private List<String> jsfs = new ArrayList<String>();
  21.     //Header
  22.     private List<String> imports = new ArrayList<String>();
  23.     //Everything else
  24.     private String byteKey = "private static byte[] byteKey = {";
  25.     private String encCode = "private static byte[] encSource = {";
  26.     private String keyString = "private static SecretKey key = new SecretKeySpec(byteKey, \"AES\");";
  27.  
  28.     //fileName must be different than sourceFile!!!!
  29.     public SourceCrypter(String sourceFile, String fileName, boolean compile) {
  30.         try {
  31.             crypterClassName = fileName;
  32.  
  33.             if (!sourceFile.toLowerCase().endsWith(".java")) {
  34.                 System.out.println("Crypter only works with *.java files!");
  35.                 return;
  36.             }
  37.  
  38.             System.out.println("Read " + sourceFile);
  39.             FileReader fr = new FileReader(sourceFile);
  40.             BufferedReader br = new BufferedReader(fr);
  41.  
  42.             //Read File
  43.             String fileString = "";
  44.             String line = "";
  45.             while ((line = br.readLine()) != null) {
  46.                 if (line.isEmpty()) {
  47.                     continue;
  48.                 } else if (inputClassName.isEmpty() && line.contains(" class ")) {
  49.                     int start_index = line.indexOf(" class ") + 7;
  50.                     int end_index = start_index + 1;
  51.                     while (end_index < line.length() - 1 && Character.isLetter(line.charAt(end_index))) {
  52.                         end_index++;
  53.                     }
  54.                     inputClassName = line.substring(start_index, end_index);//get classname as subString!
  55.                 } else if (line.contains(" ")) {
  56.                     line = line.replace(" ", "");
  57.                 }
  58.                 fileString = fileString + line;
  59.             }
  60.             //Close Streams
  61.             br.close();
  62.             fr.close();
  63.  
  64.             System.out.println("Generate AES Key...");
  65.             generateKey();
  66.             System.out.println("Add AES Key to SourceString...");
  67.             byte[] tmpKey = key.getEncoded();
  68.             for (int i = 0; i < tmpKey.length; i++) {
  69.                 byteKey = byteKey + tmpKey[i] + ",";
  70.             }
  71.             byteKey = byteKey + "};";
  72.  
  73.             System.out.println("Encrypt SourceCode...");
  74.             byte[] encryptedSource = encryptString(fileString);
  75.             System.out.println("Add encrypted SourceCode...");
  76.             for (int i = 0; i < encryptedSource.length; i++) {
  77.                 encCode = encCode + encryptedSource[i] + ",";
  78.             }
  79.             encCode = encCode + "};";
  80.  
  81.             System.out.println("Init Crypter SourceCode...");
  82.             init();
  83.  
  84.             System.out.println("Write CrypterClass File....");
  85.             FileWriter fw = new FileWriter(crypterClassName + ".java");
  86.             BufferedWriter bw = new BufferedWriter(fw);
  87.             for (String tmp : imports) {
  88.                 bw.write(tmp);
  89.             }
  90.             bw.write("public class " + crypterClassName + "{");
  91.             bw.write(byteKey);
  92.             bw.write(encCode);
  93.             bw.write(keyString);
  94.             for (String tmp : mm) {
  95.                 bw.write(tmp);
  96.             }
  97.             for (String tmp : dm) {
  98.                 bw.write(tmp);
  99.             }
  100.             bw.write("}");
  101.             for (String tmp : jsfs) {
  102.                 bw.write(tmp);
  103.             }
  104.             bw.close();
  105.             fw.close();
  106.  
  107.             if (compile) {
  108.                 System.out.println("Compiling " + crypterClassName + ".java...");
  109.                 compileFile(new File(crypterClassName + ".java"));
  110.                 for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
  111.                     System.out.println("Error on Line " + diagnostic.getLineNumber());
  112.                 }
  113.             }
  114.             System.out.println("Done!");
  115.         } catch (Throwable e) {
  116.             e.printStackTrace();
  117.         }
  118.     }
  119.  
  120.     private void init() {
  121.         //Init imports
  122.         imports.add("import java.util.*;");
  123.         imports.add("import java.net.*;");
  124.         imports.add("import java.io.*;");
  125.         imports.add("import javax.tools.*;");
  126.         imports.add("import javax.tools.JavaCompiler.*;");
  127.         imports.add("import javax.crypto.*;");
  128.         imports.add("import javax.crypto.spec.*;");
  129.  
  130.         //Init Main Method
  131.         mm.add("public static void main(String[] argv){");
  132.         mm.add("try{");
  133.         mm.add("JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();");
  134.         mm.add("DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<javafileobject>();");
  135.         mm.add("String sourceCode = decryptSource(encSource);");
  136.         mm.add("StringWriter writer = new StringWriter();");
  137.         mm.add("PrintWriter out = new PrintWriter(writer);");
  138.         mm.add("out.println(sourceCode);");
  139.         mm.add("out.close();");
  140.         mm.add("JavaFileObject file = new JavaSourceFromString(\"" + inputClassName + "\", writer.toString());");
  141.         mm.add("Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(file);");
  142.         mm.add("CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, compilationUnits);");
  143.         mm.add("boolean success = task.call();");
  144.         mm.add("for(Diagnostic diagnostic : diagnostics.getDiagnostics()){");
  145.         mm.add("System.out.println(\"Error on Line \"+diagnostic.getLineNumber());}");
  146.         mm.add("if(success){");
  147.         mm.add("Class.forName(\"" + inputClassName + "\").getD eclaredMethod(\"main\", new Class[]{String[].class}).invoke(null,new Object[]{null});}");
  148.         mm.add("}catch(Throwable e){e.printStackTrace();}}");
  149.  
  150.         //Init Decryption Method
  151.         dm.add("private static String decryptSource(byte[] input){");
  152.         dm.add("try{");
  153.         dm.add("Cipher cipher = Cipher.getInstance(\"AES\");");
  154.         dm.add("cipher.init(Cipher.DECRYPT_MODE, key);");
  155.         dm.add("byte[] output = cipher.doFinal(input);");
  156.         dm.add("return new String(output);");
  157.         dm.add("}catch(Throwable e){");
  158.         dm.add("e.printStackTrace();");
  159.         dm.add("return null;}}");
  160.  
  161.         //Init JavaSourceFromString Class
  162.         jsfs.add("class JavaSourceFromString extends SimpleJavaFileObject{");
  163.         jsfs.add("final String code;");
  164.         jsfs.add("JavaSourceFromString(String name, String code){");
  165.         jsfs.add("super(URI.create(\"String:///\"+name.replace('.','/')+Kind.SOURCE.extension),Kind.SOURCE);");
  166.         jsfs.add("this.code = code;}");
  167.         jsfs.add("@Override ");
  168.         jsfs.add("public CharSequence getCharContent(boolean ignoreEncodingErrors){");
  169.         jsfs.add("return code;}}");
  170.     }
  171.  
  172.     private void generateKey() {
  173.         try {
  174.             KeyGenerator keygen = KeyGenerator.getInstance("AES");
  175.             SecureRandom random = new SecureRandom();
  176.             keygen.init(random);
  177.             key = keygen.generateKey();
  178.         } catch (Throwable e) {
  179.             e.printStackTrace();
  180.         }
  181.     }
  182.  
  183.     private byte[] encryptString(String words) {
  184.         try {
  185.             byte[] input = words.getBytes();
  186.  
  187.             Cipher cipher = Cipher.getInstance("AES");
  188.             cipher.init(Cipher.ENCRYPT_MODE, key);
  189.  
  190.             byte[] output = cipher.doFinal(input);
  191.             return output;
  192.         } catch (Throwable e) {
  193.             e.printStackTrace();
  194.             return null;
  195.         }
  196.     }
  197.  
  198.     private void compileFile(File file) {
  199.         try {
  200.             JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  201.             StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
  202.             Iterable<? extends JavaFileObject> compilationUnit = fileManager.getJavaFileObjects(file);
  203.             compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnit).call();
  204.             fileManager.close();
  205.         } catch (Throwable e) {
  206.             e.printStackTrace();
  207.         }
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment