Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sourcecrypter;
- import javax.crypto.*;
- import java.security.*;
- import java.io.*;
- import java.util.*;
- import javax.tools.*;
- public class SourceCrypter {
- private SecretKey key;
- private DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
- private String inputClassName = "";
- private String crypterClassName;
- //Decrypt Method
- private List<String> dm = new ArrayList<String>();
- //Main Method
- private List<String> mm = new ArrayList<String>();
- //JavaSourceFromString Class
- private List<String> jsfs = new ArrayList<String>();
- //Header
- private List<String> imports = new ArrayList<String>();
- //Everything else
- private String byteKey = "private static byte[] byteKey = {";
- private String encCode = "private static byte[] encSource = {";
- private String keyString = "private static SecretKey key = new SecretKeySpec(byteKey, \"AES\");";
- //fileName must be different than sourceFile!!!!
- public SourceCrypter(String sourceFile, String fileName, boolean compile) {
- try {
- crypterClassName = fileName;
- if (!sourceFile.toLowerCase().endsWith(".java")) {
- System.out.println("Crypter only works with *.java files!");
- return;
- }
- System.out.println("Read " + sourceFile);
- FileReader fr = new FileReader(sourceFile);
- BufferedReader br = new BufferedReader(fr);
- //Read File
- String fileString = "";
- String line = "";
- while ((line = br.readLine()) != null) {
- if (line.isEmpty()) {
- continue;
- } else if (inputClassName.isEmpty() && line.contains(" class ")) {
- int start_index = line.indexOf(" class ") + 7;
- int end_index = start_index + 1;
- while (end_index < line.length() - 1 && Character.isLetter(line.charAt(end_index))) {
- end_index++;
- }
- inputClassName = line.substring(start_index, end_index);//get classname as subString!
- } else if (line.contains(" ")) {
- line = line.replace(" ", "");
- }
- fileString = fileString + line;
- }
- //Close Streams
- br.close();
- fr.close();
- System.out.println("Generate AES Key...");
- generateKey();
- System.out.println("Add AES Key to SourceString...");
- byte[] tmpKey = key.getEncoded();
- for (int i = 0; i < tmpKey.length; i++) {
- byteKey = byteKey + tmpKey[i] + ",";
- }
- byteKey = byteKey + "};";
- System.out.println("Encrypt SourceCode...");
- byte[] encryptedSource = encryptString(fileString);
- System.out.println("Add encrypted SourceCode...");
- for (int i = 0; i < encryptedSource.length; i++) {
- encCode = encCode + encryptedSource[i] + ",";
- }
- encCode = encCode + "};";
- System.out.println("Init Crypter SourceCode...");
- init();
- System.out.println("Write CrypterClass File....");
- FileWriter fw = new FileWriter(crypterClassName + ".java");
- BufferedWriter bw = new BufferedWriter(fw);
- for (String tmp : imports) {
- bw.write(tmp);
- }
- bw.write("public class " + crypterClassName + "{");
- bw.write(byteKey);
- bw.write(encCode);
- bw.write(keyString);
- for (String tmp : mm) {
- bw.write(tmp);
- }
- for (String tmp : dm) {
- bw.write(tmp);
- }
- bw.write("}");
- for (String tmp : jsfs) {
- bw.write(tmp);
- }
- bw.close();
- fw.close();
- if (compile) {
- System.out.println("Compiling " + crypterClassName + ".java...");
- compileFile(new File(crypterClassName + ".java"));
- for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
- System.out.println("Error on Line " + diagnostic.getLineNumber());
- }
- }
- System.out.println("Done!");
- } catch (Throwable e) {
- e.printStackTrace();
- }
- }
- private void init() {
- //Init imports
- imports.add("import java.util.*;");
- imports.add("import java.net.*;");
- imports.add("import java.io.*;");
- imports.add("import javax.tools.*;");
- imports.add("import javax.tools.JavaCompiler.*;");
- imports.add("import javax.crypto.*;");
- imports.add("import javax.crypto.spec.*;");
- //Init Main Method
- mm.add("public static void main(String[] argv){");
- mm.add("try{");
- mm.add("JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();");
- mm.add("DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<javafileobject>();");
- mm.add("String sourceCode = decryptSource(encSource);");
- mm.add("StringWriter writer = new StringWriter();");
- mm.add("PrintWriter out = new PrintWriter(writer);");
- mm.add("out.println(sourceCode);");
- mm.add("out.close();");
- mm.add("JavaFileObject file = new JavaSourceFromString(\"" + inputClassName + "\", writer.toString());");
- mm.add("Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(file);");
- mm.add("CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, compilationUnits);");
- mm.add("boolean success = task.call();");
- mm.add("for(Diagnostic diagnostic : diagnostics.getDiagnostics()){");
- mm.add("System.out.println(\"Error on Line \"+diagnostic.getLineNumber());}");
- mm.add("if(success){");
- mm.add("Class.forName(\"" + inputClassName + "\").getD eclaredMethod(\"main\", new Class[]{String[].class}).invoke(null,new Object[]{null});}");
- mm.add("}catch(Throwable e){e.printStackTrace();}}");
- //Init Decryption Method
- dm.add("private static String decryptSource(byte[] input){");
- dm.add("try{");
- dm.add("Cipher cipher = Cipher.getInstance(\"AES\");");
- dm.add("cipher.init(Cipher.DECRYPT_MODE, key);");
- dm.add("byte[] output = cipher.doFinal(input);");
- dm.add("return new String(output);");
- dm.add("}catch(Throwable e){");
- dm.add("e.printStackTrace();");
- dm.add("return null;}}");
- //Init JavaSourceFromString Class
- jsfs.add("class JavaSourceFromString extends SimpleJavaFileObject{");
- jsfs.add("final String code;");
- jsfs.add("JavaSourceFromString(String name, String code){");
- jsfs.add("super(URI.create(\"String:///\"+name.replace('.','/')+Kind.SOURCE.extension),Kind.SOURCE);");
- jsfs.add("this.code = code;}");
- jsfs.add("@Override ");
- jsfs.add("public CharSequence getCharContent(boolean ignoreEncodingErrors){");
- jsfs.add("return code;}}");
- }
- private void generateKey() {
- try {
- KeyGenerator keygen = KeyGenerator.getInstance("AES");
- SecureRandom random = new SecureRandom();
- keygen.init(random);
- key = keygen.generateKey();
- } catch (Throwable e) {
- e.printStackTrace();
- }
- }
- private byte[] encryptString(String words) {
- try {
- byte[] input = words.getBytes();
- Cipher cipher = Cipher.getInstance("AES");
- cipher.init(Cipher.ENCRYPT_MODE, key);
- byte[] output = cipher.doFinal(input);
- return output;
- } catch (Throwable e) {
- e.printStackTrace();
- return null;
- }
- }
- private void compileFile(File file) {
- try {
- JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
- StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
- Iterable<? extends JavaFileObject> compilationUnit = fileManager.getJavaFileObjects(file);
- compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnit).call();
- fileManager.close();
- } catch (Throwable e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment