jar包加密程序,防止反编译

本窗体程序基于开源项目xjar
https://gitee.com/core-lib/xjar/tree/2.1.1/src/main/java/io/xjar

1.pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lingxiao</groupId>
    <artifactId>m</artifactId>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11</version>
        </dependency>
        <dependency>
            <groupId>org.kordamp.bootstrapfx</groupId>
            <artifactId>bootstrapfx-core</artifactId>
            <version>0.2.4</version>
        </dependency>

        <dependency>
            <groupId>com.github.core-lib</groupId>
            <artifactId>xjar</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-compress</artifactId>
            <version>1.19</version>
        </dependency>
        <dependency>
            <groupId>com.github.core-lib</groupId>
            <artifactId>loadkit</artifactId>
            <version>v1.0.1</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>com.zenjava</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>8.8.3</version>
                <configuration>
                    <vendor>lingxiao</vendor>
                    <mainClass>com.lingxiao.Mm</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

其中xjar和loadkit我这里maven下载不下来可以自己找源码地址下载后编译打包到本地maven仓库
xjar地址:https://gitee.com/core-lib/xjar/tree/2.1.1/src/main/java/io/xjar
loadkit地址:https://gitee.com/core-lib/loadkit

2.启动类

package com.lingxiao;
import io.xjar.XKit;
import io.xjar.boot.XBoot;
import io.xjar.jar.XJarAntEntryFilter;
import io.xjar.key.XKey;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class Mm extends Application {
    public void start(Stage primaryStage) throws Exception {
        FlowPane pane = new FlowPane();
        pane.setPadding(new Insets(11,500,13,14));
        pane.setHgap(5);
        pane.setVgap(5);

        Label lab1 = new Label("指定加密前jar包路径(例如:D:\\work\\zike\\target\\zike.jar)");
        Label lab2 = new Label("指定加密密码");
        /*Label lab3 = new Label("指定需要加密的资源(例如:com/lingxiao/**)");*/
        Label lab4 = new Label("加密后JAR包输出路径(例如:D:\\work\\zike\\target\\zikeMm.jar)");

        TextField text1 = new TextField();
        text1.setPrefWidth(320);
        TextField text2 = new TextField();
        /*TextField text3 = new TextField();
        text3.setPrefWidth(320);*/
        TextField text4 = new TextField();
        text4.setPrefWidth(320);
        TextArea mess = new TextArea();
        mess.setPrefWidth(400);
        mess.setPrefHeight(120);

        Button button = new Button("加密");

        button.setOnAction(e-> {
                String value1 = text1.getText();
                String value2 = text2.getText();
                /*String value3 = text3.getText();*/
                String value4 = text4.getText();
            Alert alert = new Alert(Alert.AlertType.WARNING);
            Alert alertError = new Alert(Alert.AlertType.ERROR);
            Alert alertOk = new Alert(Alert.AlertType.INFORMATION);
            if(value1 == null || value1.trim().isEmpty()){
                alert.setContentText("指定加密的jar包路径输入有误");
                alert.showAndWait();
                return;
            }
            if(value2 == null || value2.trim().isEmpty()){
                alert.setContentText("指定加密密码输入有误");
                alert.showAndWait();
                return;
            }
            /*if(value3 == null || value3.trim().isEmpty()){
                alert.setContentText("指定需要加密的资源输入有误");
                alert.showAndWait();
                return;
            }*/
            if(value4 == null || value4.trim().isEmpty()){
                alert.setContentText("指定加密后JAR包输出路径输入有误");
                alert.showAndWait();
                return;
            }
            try {
                XKey xKey = XKit.key(value2);
                XBoot.encrypt(value1, value4, xKey);
            } catch (Exception ex) {
                ex.printStackTrace();
                alertError.setContentText(ex.getMessage());
                alertError.showAndWait();
                return;
            }
            String path = value4.substring(0,value4.lastIndexOf("\\")==-1?0:value4.lastIndexOf("\\")+1)+"password.key";
            File file = new File(path);
            //判断文件是否存在
            try {
                if (file.createNewFile()) {
                    //创建一个用于操作文件的字节输出流对象,创建就必须明确数据存储目的地
                    FileOutputStream fos = new FileOutputStream(file);
                    //写入
                    fos.write(("password: "+value2).getBytes(StandardCharsets.UTF_8));
                    //刷新并关闭流
                    fos.flush();
                    fos.close();
                }
            } catch (Exception eio) {
                eio.printStackTrace();
                alertError.setContentText(eio.getMessage());
                alertError.showAndWait();
                return;
            }

            String jar = value4.substring(value4.lastIndexOf("\\")==-1?0:value4.lastIndexOf("\\")+1);
            String mes = "(1)命令行启动,输入密码\n"+
                    "java -jar "+jar+" \n"+
                    "(2)传参启动\n" +
                    "java -jar "+jar+" --xjar.password="+value2+"\n"+
                    "(3)后台启动\n" +
                    "nohup java -jar "+jar+" --xjar.keyfile=password.key";
            alertOk.setContentText("加密成功\n");
            alertOk.showAndWait();
            mess.setText(mes);

        });
        pane.getChildren().addAll(lab1,text1,lab2,text2,lab4,text4,button,mess);
        Scene scene = new Scene(pane, 480, 480);
        primaryStage.setTitle("lingxiaoMm-v1.0");
        primaryStage.setScene(scene);
        primaryStage.sizeToScene();
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch();
    }
}

3.测试启动

jar包加密程序,防止反编译_第1张图片

4.使用

指定加密前jar包路径(例如:D:\work\zike\target\zike.jar)
指定加密密码
加密后JAR包输出路径(例如:D:\work\zike\target\zikeMm.jar)
jar包加密程序,防止反编译_第2张图片
jar包加密程序,防止反编译_第3张图片jar包加密程序,防止反编译_第4张图片

(1)命令行启动,输入密码
java -jar zikeMm.jar
(2)传参启动
java -jar zikeMm.jar --xjar.password=123
(3)后台启动
nohup java -jar zikeMm.jar --xjar.keyfile=password.key

备注:
(1)输入启动密码错误无法启动
(2)加密后反编译失败
(3)可以直接打包为EXE程序
打包命令:

mvn jfx:native

打包后
在这里插入图片描述
APP包为jar包需要自己配置运行环境
native包为EXE程序自带运行jre环境
jar包加密程序,防止反编译_第5张图片

你可能感兴趣的:(其他,jar,java,maven)