diff --git a/README.md b/README.md index 54dc787..b564244 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,16 @@ jar Main-Class ``` +StrUtils.class ( 使用 javac 编译 ) + +```bash +javac .\StrUtils.java +``` + + + # 注意事项 + ## 自己的软件加载DEX,运行后界面乱码闪退 BUG > IDEA中顶部菜单 -- 帮助 -- 编辑自定义 VM选项 -- 内容最底下加入下面的代码. diff --git a/build.gradle b/build.gradle index 8be67d3..963b15e 100644 --- a/build.gradle +++ b/build.gradle @@ -60,7 +60,6 @@ sourceSets { dependencies { // 添加依赖 implementation 'cn.hutool:hutool-all:5.3.6' - implementation group: 'com.itranswarp', name: 'compiler', version: '1.0' implementation 'commons-cli:commons-cli:1.5.0' // 原始依赖 @@ -226,6 +225,12 @@ jar { from ("resources/dx-29.0.3.jar"){ into "" } + + // 将 StrUtils.class 打包到 最终jar 的根目录 + from ("resources/StrUtils.class"){ + into "" + } + from sourceSets.main.output excludes = ["org/mozilla/javascript/engine/**", "META-INF/services/**"] // Class ImplementationVersion uses 'Implementation-Title' diff --git a/resources/StrUtils.class b/resources/StrUtils.class new file mode 100644 index 0000000..43b731d Binary files /dev/null and b/resources/StrUtils.class differ diff --git a/resources/StrUtils.java b/resources/StrUtils.java new file mode 100644 index 0000000..41cb426 --- /dev/null +++ b/resources/StrUtils.java @@ -0,0 +1,15 @@ +package defpackage; + +public class StrUtils { + public static String d(String data) { + int l = data.length() / 2; + byte[] b = new byte[l]; + for (int i = 0; i < l; i++) { + b[i] = Integer.valueOf(data.substring(i * 2, (i * 2) + 2), 16).byteValue(); + } + for (int i2 = 0; i2 < b.length; i2++) { + b[i2] = (byte) (b[i2] - 1); + } + return new String(b); + } +} \ No newline at end of file diff --git a/src/org/mozilla/mycode/Main.java b/src/org/mozilla/mycode/Main.java index d36f969..0e6ab88 100644 --- a/src/org/mozilla/mycode/Main.java +++ b/src/org/mozilla/mycode/Main.java @@ -3,49 +3,17 @@ import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.resource.ResourceUtil; import cn.hutool.core.util.CharsetUtil; -import com.itranswarp.compiler.JavaStringCompiler; +import org.apache.commons.cli.*; import org.mozilla.javascript.CompilerEnvirons; import org.mozilla.javascript.Context; import org.mozilla.javascript.optimizer.ClassCompiler; -import org.apache.commons.cli.*; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; +import java.io.*; import java.nio.file.Paths; import java.text.SimpleDateFormat; import java.util.Date; -import java.util.Map; public class Main { - - /** - * 解密函数 - */ - static final String _javaSourceCode = "package defpackage;" + - "import org.mozilla.classfile.ByteCode;" + - "import org.mozilla.classfile.ClassFileWriter;" + - "public class StrUtils{" + - " public static String d(String data)" + - " {" + - " int l = data.length() / 2;" + - " byte[] b = new byte[l];" + - " for (int i = 0; i < l; i++)" + - " {" + - " b[i] = Integer.valueOf(data.substring(i * 2, (i * 2) + 2), 16).byteValue();" + - " }" + - " for (int i2 = 0; i2 < b.length; i2++)" + - " {" + - " b[i2] = (byte) (b[i2] - 1);" + - " }" + - " return new String(b);" + - " }" + - " } "; - /** * 工作的目录 */ @@ -61,6 +29,7 @@ public class Main { */ public static Boolean _isNoEncryptString = false; public static String _dxFileName = "dx-29.0.3.jar"; + public static String _strUtilsClassFileName = "StrUtils.class"; public static void main(String[] args) throws Exception { @@ -79,7 +48,6 @@ public static void main(String[] args) throws Exception { String inputFilePath = cmd.getOptionValue("f"); String outputDirPath = cmd.getOptionValue("o"); - // 输出目录 再创建一个唯一的文件夹 (使用当前时间精确到秒) _outDirPath = Paths.get(outputDirPath, getTimeString()).toString(); _level = Integer.valueOf(cmd.getOptionValue('l', "9")); @@ -95,12 +63,13 @@ public static void main(String[] args) throws Exception { System.out.println("==================================="); System.out.println("==================================="); - // 将 当前 jar 程序中的 dx.jar 释放到 outputDir 目录 - byte[] fileData = ResourceUtil.readBytes(_dxFileName); - FileUtil.writeBytes(fileData, Paths.get(_outDirPath, _dxFileName).toString()); + // 将 当前 jar 中的 dx.jar 释放到 outputDir 目录 + FileUtil.writeBytes(ResourceUtil.readBytes(_dxFileName), Paths.get(_outDirPath, _dxFileName).toString()); + // 读取 代码文本 String code = FileUtil.readUtf8String(inputFilePath); + // 开始转 dex toDexFile(code); } catch (ParseException e) { @@ -108,16 +77,7 @@ public static void main(String[] args) throws Exception { } } - /** - * 获取时间 字符串 - */ - public static String getTimeString() { - Date date = new Date(); - SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_HHmmss"); - return formatter.format(date); - } - - static void toDexFile(String code) throws Exception { + static void toDexFile(String code) { //创建 Rhino 编译环境 相关参数.. CompilerEnvirons compilerEnv = new CompilerEnvirons(); compilerEnv.setGeneratingSource(false); //编译后,不添加 js 源码 @@ -141,49 +101,20 @@ static void toDexFile(String code) throws Exception { for (int j = 0; j != compiled.length; j += 2) { - JavaStringCompiler javaStringCompiler = new JavaStringCompiler(); - - // 字符串 转为 java class 文件 - Map results = javaStringCompiler.compile("StrUtils.java", _javaSourceCode); - - //解密工具类的 数据 - byte[] utilsBytes = results.get("defpackage.StrUtils"); - + // 生成 解密工具类 文件 + byte[] utilsBytes = ResourceUtil.readBytes(_strUtilsClassFileName); String utilsClassPath = Paths.get(_outDirPath, "class", "defpackage", "StrUtils.class").toString(); - File utilsFile = new File(utilsClassPath); - utilsFile.getParentFile().mkdirs(); //创建文件夹 + FileUtil.writeBytes(utilsBytes, utilsClassPath); - try (FileOutputStream fos = new FileOutputStream(utilsFile)) { - fos.write(utilsBytes); - } catch (FileNotFoundException e) { - System.out.println("utils 文件未找到!"); - e.printStackTrace(); - } catch (IOException e) { - System.out.println("utils 文件保存失败!"); - e.printStackTrace(); - } + // 生成 自己的代码 文件 String classPath = Paths.get(_outDirPath, "class", "aaa.class").toString(); - - // js 转为 class byte[] bytes = (byte[]) compiled[(j + 1)]; - File file = new File(classPath); - file.getParentFile().mkdirs(); //创建文件夹 - - try (FileOutputStream fos = new FileOutputStream(file)) { - fos.write(bytes); - } catch (FileNotFoundException e) { - System.out.println("文件未找到!"); - e.printStackTrace(); - } catch (IOException e) { - System.out.println("文件保存失败!"); - e.printStackTrace(); - } + FileUtil.writeBytes(bytes, classPath); + // 将多个 class 合并为一个 jar String jarFileName = "code.jar"; - //将两个 class 打包为 一个jar cmdExec("jar cvf " + jarFileName + " -C class ."); - System.out.println("开始转dex,js代码越多,耗时越长,请耐心等待"); //将 jar 转为 dex @@ -202,7 +133,6 @@ static void toDexFile(String code) throws Exception { * 执行cmd命令 */ public static void cmdExec(String cmd) { - Runtime run = Runtime.getRuntime(); try { Process p = run.exec(cmd, new String[]{}, new File(_outDirPath)); // 设置目录 @@ -210,9 +140,7 @@ public static void cmdExec(String cmd) { InputStream ers = p.getErrorStream(); new Thread(new inputStreamThread(ins)).start(); p.waitFor(); - } catch (IOException e) { - e.printStackTrace(); - } catch (InterruptedException e) { + } catch (IOException | InterruptedException e) { e.printStackTrace(); } } @@ -241,4 +169,13 @@ public void run() { } } + /** + * 获取时间 字符串 + */ + public static String getTimeString() { + Date date = new Date(); + SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_HHmmss"); + return formatter.format(date); + } + } \ No newline at end of file