目錄
2.1 導入pomx (可以的話直接看3) 不行接着往下 2.2
2.3 跳過2.1 的廢話,直接下載 xjar 和 loadkit 包,並且安裝到本地
3 編寫代碼對已有spring boot jar 進行加密操作
1 Xjar 介紹
Spring Boot JAR 安全加密運行工具,同時支持的原生JAR。
基於對JAR包內資源的加密以及拓展ClassLoader來構建的一套程序加密啓動,動態解密運行的方案,避免源碼泄露或反編譯。
功能特性
無需侵入代碼,只需要把編譯好的JAR包通過工具加密即可。
完全內存解密,杜絕源碼以及字節碼泄露或反編譯。
支持所有JDK內置加解密算法。
可選擇需要加解密的字節碼或其他資源文件,避免計算資源浪費。
2 如何使用 xjar v2.06
2.1 導入pomx (可以的話直接看3) 不行接着往下 2.2
<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>v2.0.5</version>
</dependency>
</dependencies>
</project>
2.2 自己去maven 下載jar
https://mvnrepository.com/artifact/com.github.core-lib/xjar/v2.0.6
下載jar
將xjar安裝到本地倉庫, 執行maven 命令 修改xjar文件路徑
mvn install:install-file -Dfile=G:/xjar-v2.0.6.jar -DgroupId=com.github.core-lib -DartifactId=xjar -Dversion=v2.0.6 -Dpackaging=jar
引入maven
<dependency>
<groupId>com.github.core-lib</groupId>
<artifactId>xjar</artifactId>
<version>v2.0.6</version>
</dependency>
很遺憾的告訴你,你可能回遇到異常
缺少jar包, 我怎末知道的呢, 感謝有百度,繼續添加一下maven
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.18</version>
</dependency>
還是回遇到異常,心態崩了, 只能接着問問度娘了,
發現還是缺少jar 包,loadkit
https://mvnrepository.com/artifact/com.github.core-lib/loadkit/v1.0.0
如果開始的xjar 你引入不了, 這個應該你也引入不了, 只能手動下載 安裝到本地倉庫了
mvn install:install-file -Dfile=G:/loadkit-v1.0.0.jar -DgroupId=com.github.core-lib -DartifactId=loadkit -Dversion=v1.0.0 -Dpackaging=jar
<dependency>
<groupId>com.github.core-lib</groupId>
<artifactId>loadkit</artifactId>
<version>v1.0.0</version>
</dependency>
好了,到這坑都踩完了, 下面是完整的pom文件
2.3 跳過2.1 的廢話,直接下載 xjar 和 loadkit 包,並且安裝到本地
mvn install:install-file -Dfile=G:/loadkit-v1.0.0.jar -DgroupId=com.github.core-lib -DartifactId=loadkit -Dversion=v1.0.0 -Dpackaging=jar
mvn install:install-file -Dfile=G:/xjar-v2.0.6.jar -DgroupId=com.github.core-lib -DartifactId=xjar -Dversion=v2.0.6 -Dpackaging=jar
<dependencies>
<dependency>
<groupId>com.github.core-lib</groupId>
<artifactId>xjar</artifactId>
<version>v2.0.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.18</version>
</dependency>
<dependency>
<groupId>com.github.core-lib</groupId>
<artifactId>loadkit</artifactId>
<version>v1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 用來創建超級JAR包的Maven shade插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
3 編寫代碼對已有spring boot jar 進行加密操作
public static void main(String[] args) throws Exception {
// Spring-Boot Jar包加密
String password = "123456";
XKey xKey = XKit.key(password);
XBoot.encrypt("C:/Users/Lenovo/Desktop/webapp.jar", "C:/Users/Lenovo/Desktop/webapp-xjar.jar", xKey, XConstants.MODE_DANGER);
System.out.println("Successfully generated encrypted jar");
}
本地啓動,需要輸入密碼纔可以,
// 危險加密模式,即不需要輸入密碼即可啓動的加密方式,這種方式META-INF/MANIFEST.MF中會保留密鑰,請謹慎使用!
String password = "io.xjar";
XKey xKey = XKit.key(password);
XBoot.encrypt("/path/to/read/plaintext.jar", "/path/to/save/encrypted.jar", xKey, XConstants.MODE_DANGER);
// Spring-Boot Jar包解密
String password = "io.xjar";
XKey xKey = XKit.key(password);
XBoot.decrypt("/path/to/read/encrypted.jar", "/path/to/save/decrypted.jar", xKey);
啓動方式
// 命令行運行JAR 然後在提示輸入密碼的時候輸入密碼後按回車即可正常啓動
java -jar /path/to/encrypted.jar
// 也可以通過傳參的方式直接啓動,不太推薦這種方式,因爲泄露的可能性更大!
java -jar /path/to/encrypted.jar --xjar.password=PASSWORD
// 對於 nohup 或 javaw 這種後臺啓動方式,無法使用控制檯來輸入密碼,推薦使用指定密鑰文件的方式啓動
nohup java -jar /path/to/encrypted.jar --xjar.keyfile=/path/to/xjar.key > nohup.out 2>&1 &
xjar.key 文件內容爲 properties 文件,只需填寫兩個參數
password: 123456
hold: 1
感覺他的加密方式,應該是讓xjar 使用自定義的類加載器,去將所有的類進行單獨的類加載
4 懶人方案
如果你只是需要對jar 進行加密操作,可以直接使用我的jar
https://github.com/yumingzhu/xjarDemo
下載target目錄下的xjarDemo-1.0-SNAPSHOT.jar,
執行一下命令後面三個參數,分別爲,加密的密碼,jar包,加密後生成的jar包
java -cp xjarDemo-1.0-SNAPSHOT.jar XjarDemo 123456 C:/Users/Lenovo/Desktop/webapp.jar C:/Users/Lenovo/Desktop/webapp-xjar.jar
5 java 反編譯工具
我使用了 jd,luyten xJad 三個工具, 正常反編譯的jar 都可以得到代碼, 用xjar 加密的 spring boot jar 都查看不了, 不知道以後會不會被破解,我把這三個工具都上傳到我的百度雲盤,有興趣可以測試一下,
鏈接:https://pan.baidu.com/s/1yQa3ZEfQQ0ne_oL4yzChmQ
提取碼:0sm5