基于对JAR包内资源的加密以及拓展ClassLoader来构建的一套程序加密启动,动态解密运行的方案,避免源码泄露或反编译。
JDK 1.7 +
<project> <!-- 设置 jitpack.io 仓库 --> <repositories> <repository> <id>jitpack.io</id> <url>https://www.jitpack.io</url> </repository> </repositories> <!-- 添加 XJar 依赖 --> <dependencies> <dependency> <groupId>com.github.core-lib</groupId> <artifactId>xjar</artifactId> <version>LATEST_VERSION</version> </dependency> </dependencies> </project>
// Spring-Boot Jar包加密 public static void main(String[] args) { String password = "io.xjar"; File plaintext = new File("/path/to/read/plaintext.jar"); File encrypted = new File("/path/to/save/encrypted.jar"); XBoot.encrypt(plaintext, encrypted, password); }
// Spring-Boot Jar包解密 public static void main(String[] args) { String password = "io.xjar"; File encrypted = new File("/path/to/read/encrypted.jar"); File decrypted = new File("/path/to/save/decrypted.jar"); XBoot.decrypt(encrypted, decrypted, password); }
// Jar包加密 public static void main(String[] args) { String password = "io.xjar"; File plaintext = new File("/path/to/read/plaintext.jar"); File encrypted = new File("/path/to/save/encrypted.jar"); XJar.encrypt(plaintext, encrypted, password); }
// Jar包解密 public static void main(String[] args) { String password = "io.xjar"; File encrypted = new File("/path/to/read/encrypted.jar"); File decrypted = new File("/path/to/save/decrypted.jar"); XJar.decrypt(encrypted, decrypted, password); }
// 命令行运行JAR java -jar /path/to/encrypted.jar // 在提示输入密码的时候输入密码后按回车即可正常启动,也可以通过传参的方式直接启动 java -jar /path/to/encrypted.jar --xjar.password=PASSWORD
// 只加密自身项目及相关模块的源码不加密第三方依赖,可以通过XJarArchiveEntryFilter来定制需要加密的JAR包内资源 public static void main(String[] args) { String password = "io.xjar"; File plaintext = new File("/path/to/read/plaintext.jar"); File encrypted = new File("/path/to/save/encrypted.jar"); XBoot.encrypt(plaintext, encrypted, password, new XJarArchiveEntryFilter() { @Override public boolean filter(JarArchiveEntry entry) { return entry.getName().startsWith("/BOOT-INF/classes/") || entry.getName().startsWith("/BOOT-INF/lib/jar-need-encrypted"); } }); }
Apache-2.0
QQ 646742615 不会钓鱼的兔子