Skip to content

Commit

Permalink
Merge pull request #3 from xxxxue/fix_bug
Browse files Browse the repository at this point in the history
fix: 修复编译 java 代码字符串报错的bug
  • Loading branch information
xxxxue authored Jul 21, 2023
2 parents 29ddcb0 + a3b1b30 commit bbb102e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 88 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,16 @@ jar
Main-Class
```

StrUtils.class ( 使用 javac 编译 )

```bash
javac .\StrUtils.java
```



# 注意事项

## 自己的软件加载DEX,运行后界面乱码闪退 BUG

> IDEA中顶部菜单 -- 帮助 -- 编辑自定义 VM选项 -- 内容最底下加入下面的代码.
Expand Down
7 changes: 6 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

// 原始依赖
Expand Down Expand Up @@ -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'
Expand Down
Binary file added resources/StrUtils.class
Binary file not shown.
15 changes: 15 additions & 0 deletions resources/StrUtils.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
111 changes: 24 additions & 87 deletions src/org/mozilla/mycode/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);" +
" }" +
" } ";

/**
* 工作的目录
*/
Expand All @@ -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 {

Expand All @@ -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"));
Expand All @@ -95,29 +63,21 @@ 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) {
System.err.println("命令行参数解析失败: " + e.getMessage());
}
}

/**
* 获取时间 字符串
*/
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 源码
Expand All @@ -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<String, byte[]> 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
Expand All @@ -202,17 +133,14 @@ 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)); // 设置目录
InputStream ins = p.getInputStream();
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();
}
}
Expand Down Expand Up @@ -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);
}

}

0 comments on commit bbb102e

Please sign in to comment.