2025-05-13 21:30:03 +08:00
|
|
|
|
---
|
2025-07-14 20:35:23 +08:00
|
|
|
|
icon: mdi:shield-lock
|
2025-05-13 21:30:03 +08:00
|
|
|
|
date: 2025-05-13
|
|
|
|
|
category:
|
|
|
|
|
- JAVA
|
|
|
|
|
- 加密
|
|
|
|
|
tag:
|
|
|
|
|
- xjar
|
|
|
|
|
title: XJar
|
|
|
|
|
---
|
|
|
|
|
|
2025-07-14 20:35:23 +08:00
|
|
|
|
|
2025-05-13 21:30:03 +08:00
|
|
|
|
XJar:保护您的Java应用程序免受反编译和源码泄露
|
|
|
|
|
<!-- more -->
|
|
|
|
|
# XJar:保护您的Java应用程序免受反编译和源码泄露
|
|
|
|
|
|
|
|
|
|
介绍一个强大且实用的工具——**XJar**。它专为Spring Boot JAR和原生JAR提供安全加密运行支持,能够有效防止源码泄露和反编译的风险。无论你是想保护个人项目还是企业级应用,XJar都能为你提供一个简单而高效的解决方案。
|
|
|
|
|
|
|
|
|
|
GitHub: https://github.com/core-lib/xjar
|
|
|
|
|
|
|
|
|
|
## 什么是XJar?
|
|
|
|
|
|
|
|
|
|
XJar是一个开源工具,通过对JAR包内的资源进行加密,并结合扩展的ClassLoader,构建了一套程序加密启动和动态解密运行的机制。它特别适合Spring Boot项目,同时也支持原生JAR,旨在帮助开发者保护Java应用程序的安全性。
|
|
|
|
|
|
|
|
|
|
## 功能特性
|
|
|
|
|
|
|
|
|
|
XJar提供了以下核心功能,让它在加密工具中脱颖而出:
|
|
|
|
|
|
|
|
|
|
- **无代码侵入**:无需修改源代码,只需对编译好的JAR包进行加密即可。
|
|
|
|
|
- **完全内存解密**:资源在运行时动态解密,减少源码或字节码泄露的风险。
|
|
|
|
|
- **支持所有JDK内置加解密算法**:灵活选择适合你的加密算法。
|
|
|
|
|
- **资源加密灵活性**:支持选择需要加密的字节码或其他资源文件。
|
|
|
|
|
- **Maven插件支持**:集成到构建流程,加密更便捷。
|
|
|
|
|
- **Go启动器**:动态生成Go语言启动器,保护密码不泄露。
|
|
|
|
|
|
|
|
|
|
## 环境依赖
|
|
|
|
|
|
|
|
|
|
- **JDK版本**:1.7及以上
|
|
|
|
|
|
|
|
|
|
## 使用步骤
|
|
|
|
|
|
|
|
|
|
下面我将详细介绍如何使用XJar加密你的JAR包,步骤清晰易懂。
|
|
|
|
|
|
|
|
|
|
### 1. 添加依赖
|
|
|
|
|
|
|
|
|
|
首先,在你的Maven项目中添加XJar依赖,并配置jitpack.io仓库:
|
|
|
|
|
|
|
|
|
|
```xml
|
|
|
|
|
<project>
|
|
|
|
|
<!-- 设置jitpack.io仓库 -->
|
|
|
|
|
<repositories>
|
|
|
|
|
<repository>
|
|
|
|
|
<id>jitpack.io</id>
|
|
|
|
|
<url>https://jitpack.io</url>
|
|
|
|
|
</repository>
|
|
|
|
|
</repositories>
|
|
|
|
|
<!-- 添加XJar依赖 -->
|
|
|
|
|
<dependencies>
|
|
|
|
|
<dependency>
|
|
|
|
|
<groupId>com.github.core-lib</groupId>
|
|
|
|
|
<artifactId>xjar</artifactId>
|
|
|
|
|
<version>4.0.2</version>
|
|
|
|
|
</dependency>
|
|
|
|
|
</dependencies>
|
|
|
|
|
</project>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
> **小贴士**:如果只是用JUnit测试加密过程,可以将`<scope>`设置为`test`。
|
|
|
|
|
|
|
|
|
|
### 2. 加密源码
|
|
|
|
|
|
|
|
|
|
使用XJar提供的`XCryptos.encryption()`方法对JAR包进行加密:
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
XCryptos.encryption()
|
|
|
|
|
.from("/path/to/read/plaintext.jar") // 待加密JAR包路径
|
|
|
|
|
.use("io.xjar") // 加密密码
|
|
|
|
|
.include("/io/xjar/**/*.class") // 需要加密的字节码
|
|
|
|
|
.include("/mapper/**/*Mapper.xml") // 需要加密的资源文件
|
|
|
|
|
.exclude("/static/**/*") // 排除静态文件
|
|
|
|
|
.exclude("/conf/*") // 排除配置文件
|
|
|
|
|
.to("/path/to/save/encrypted.jar"); // 输出加密后的JAR包
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
- `include`和`exclude`支持ANT表达式或正则表达式,灵活控制加密范围。
|
|
|
|
|
- 当两者同时使用时,加密范围为`include`内排除`exclude`后的资源。
|
|
|
|
|
|
|
|
|
|
### 3. 编译脚本
|
|
|
|
|
|
|
|
|
|
加密完成后,XJar会在输出目录生成一个`xjar.go`文件。这是Go语言编写的启动器源码,需要编译为可执行文件:
|
|
|
|
|
|
|
|
|
|
- **Windows**:编译后生成`xjar.exe`
|
|
|
|
|
- **Linux**:编译后生成`xjar`
|
|
|
|
|
|
|
|
|
|
编译需要Go环境,但运行时无需Go支持。注意:启动器带有防篡改校验,无法通用。
|
|
|
|
|
|
|
|
|
|
### 4. 启动运行
|
|
|
|
|
|
|
|
|
|
使用编译好的启动器运行加密后的JAR包:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
./xjar java -Xms256m -Xmx1024m -jar /path/to/encrypted.jar
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
- 启动器需放在Java命令之前。
|
|
|
|
|
- 仅支持`-jar`方式启动,不支持`-cp`或`-classpath`。
|
|
|
|
|
- 使用`nohup`时,需写为:`nohup ./xjar java -jar /path/to/encrypted.jar`。
|
|
|
|
|
|
|
|
|
|
## 注意事项
|
|
|
|
|
|
|
|
|
|
在使用XJar时,以下问题可能影响你的体验,我整理了解决方案供参考:
|
|
|
|
|
|
|
|
|
|
### 1. Spring Boot Maven插件兼容性
|
|
|
|
|
|
|
|
|
|
XJar不支持`spring-boot-maven-plugin`的`executable = true`和`embeddedLaunchScript`配置,需删除:
|
|
|
|
|
|
|
|
|
|
```xml
|
|
|
|
|
<plugin>
|
|
|
|
|
<groupId>org.springframework.boot</groupId>
|
|
|
|
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
|
|
|
|
<!-- 删除以下配置 -->
|
|
|
|
|
<!-- <configuration>
|
|
|
|
|
<executable>true</executable>
|
|
|
|
|
<embeddedLaunchScript>...</embeddedLaunchScript>
|
|
|
|
|
</configuration> -->
|
|
|
|
|
</plugin>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 2. Spring Boot + JPA(Hibernate)报错
|
|
|
|
|
|
|
|
|
|
若使用Hibernate,启动可能报错。解决方法:
|
|
|
|
|
|
|
|
|
|
1. 克隆[XJar-Agent-Hibernate](https://github.com/core-lib/xjar-agent-hibernate),编译生成`xjar-agent-hibernate-${version}.jar`。
|
|
|
|
|
|
|
|
|
|
2. 启动命令添加代理:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
./xjar java -javaagent:xjar-agent-hibernate-${version}.jar -jar your-app.jar
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 3. 静态文件加载问题
|
|
|
|
|
|
|
|
|
|
加密静态文件可能导致浏览器加载失败,建议排除加密:
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
.exclude("/static/**/*")
|
|
|
|
|
.exclude("/META-INF/resources/**/*")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 4. JDK 9+模块化问题
|
|
|
|
|
|
|
|
|
|
在JDK 9及以上版本,需添加参数:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
./xjar java --add-opens java.base/jdk.internal.loader=ALL-UNNAMED -jar /path/to/encrypted.jar
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 5. 阿里云Maven镜像问题
|
|
|
|
|
|
|
|
|
|
使用阿里云镜像时,需在`mirrorOf`中排除jitpack.io:
|
|
|
|
|
|
|
|
|
|
```xml
|
|
|
|
|
<mirror>
|
|
|
|
|
<id>alimaven</id>
|
|
|
|
|
<mirrorOf>central,!jitpack.io</mirrorOf>
|
|
|
|
|
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
|
|
|
|
|
</mirror>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 插件集成
|
|
|
|
|
|
|
|
|
|
XJar提供了[xjar-maven-plugin](https://github.com/core-lib/xjar-maven-plugin),可自动完成加密过程。
|
|
|
|
|
|
|
|
|
|
### 配置示例
|
|
|
|
|
|
|
|
|
|
```xml
|
|
|
|
|
<project>
|
|
|
|
|
<pluginRepositories>
|
|
|
|
|
<pluginRepository>
|
|
|
|
|
<id>jitpack.io</id>
|
|
|
|
|
<url>https://jitpack.io</url>
|
|
|
|
|
</pluginRepository>
|
|
|
|
|
</pluginRepositories>
|
|
|
|
|
<build>
|
|
|
|
|
<plugins>
|
|
|
|
|
<plugin>
|
|
|
|
|
<groupId>com.github.core-lib</groupId>
|
|
|
|
|
<artifactId>xjar-maven-plugin</artifactId>
|
|
|
|
|
<version>4.0.2</version>
|
|
|
|
|
<executions>
|
|
|
|
|
<execution>
|
|
|
|
|
<goals>
|
|
|
|
|
<goal>build</goal>
|
|
|
|
|
</goals>
|
|
|
|
|
<phase>package</phase>
|
|
|
|
|
<configuration>
|
|
|
|
|
<password>io.xjar</password>
|
|
|
|
|
</configuration>
|
|
|
|
|
</execution>
|
|
|
|
|
</executions>
|
|
|
|
|
</plugin>
|
|
|
|
|
</plugins>
|
|
|
|
|
</build>
|
|
|
|
|
</project>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 执行方式
|
|
|
|
|
|
|
|
|
|
- **自动构建**:`mvn clean package -Dxjar.password=io.xjar`
|
|
|
|
|
- **手动执行**:`mvn xjar:build -Dxjar.password=io.xjar`
|
|
|
|
|
|
|
|
|
|
> **强烈建议**:不要在`pom.xml`中写死密码,通过命令行传递更安全!
|
|
|
|
|
|
|
|
|
|
## 参数说明
|
|
|
|
|
|
|
|
|
|
以下是`xjar-maven-plugin`的主要参数:
|
|
|
|
|
|
|
|
|
|
| 参数名称 | 命令参数 | 参数说明 | 参数类型 | 缺省值 |
|
|
|
|
|
| --------- | ---------------- | ------------ | -------- | -------------------------- |
|
|
|
|
|
| password | -Dxjar.password | 密码字符串 | String | 必须 |
|
|
|
|
|
| algorithm | -Dxjar.algorithm | 加密算法名称 | String | AES/CBC/PKCS5Padding |
|
|
|
|
|
| keySize | -Dxjar.keySize | 密钥长度 | int | 128 |
|
|
|
|
|
| ivSize | -Dxjar.ivSize | 密钥向量长度 | int | 128 |
|
|
|
|
|
| sourceDir | -Dxjar.sourceDir | 源JAR目录 | File | ${project.build.directory} |
|
|
|
|
|
| targetDir | -Dxjar.targetDir | 目标JAR目录 | File | ${project.build.directory} |
|
|
|
|
|
|
|
|
|
|
更多详情见:[xjar-maven-plugin](https://github.com/core-lib/xjar-maven-plugin)。
|
|
|
|
|
|
|
|
|
|
## 结尾
|
|
|
|
|
|
|
|
|
|
XJar以其无侵入性、灵活性和高效性,成为保护Java应用的绝佳选择。无论是个人项目还是商业产品,它都能帮你轻松加密JAR包,确保代码安全。赶快试试吧!
|