java:編寫jar包加密工具,防止反編譯


懶人方案

網盤:

鏈接:https://pan.baidu.com/s/1x4OB1IF2HZGgtLhd1Kr_AQ
提取碼:glx7

網盤內是已生成可用工具,下載可以直接使用,使用前看一下READ.txt文件。

 

注意:此加密工具會把Class完全加密,jd-gui-1.4.0只能反編譯出jar結構,但是看不到class文件源碼!

 

Maven依賴

 

<?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.zy.java</groupId>
    <artifactId>Util</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.github.core-lib</groupId>
            <artifactId>xjar</artifactId>
            <version>v1.1.4</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>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.6.RELEASE</version>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

 

 

 

java代碼

package com.zy.java;

import io.xjar.XConstants;
import io.xjar.XKit;
import io.xjar.boot.XBoot;
import io.xjar.key.XKey;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.security.NoSuchAlgorithmException;

public class Practice{

    private static String password = "你的加密密碼";
    private static JButton openItem, saveItem;
    private static FileDialog openDia, saveDia;// 定義“打開、保存”對話框
    private static String filePathOld;//舊文件路徑
    private static String filePathNew;//新文件路徑
    private static String fileName;//文件名
    static JFrame f = null;
    static JTextField locationText, typeText;

    public static void main(String[] args) {

        int gap = 5;
        f = new JFrame("加密程序");
        ImageIcon icon = new ImageIcon("src\\images\\2.ico");
        f.setIconImage(icon.getImage());// 給窗體設置圖標方法
        f.setSize(550, 310);
        f.setLocation(200, 200);
        f.setLayout(null);

        JPanel pInput = new JPanel();
        pInput.setBounds(gap, gap, 520, 60);
        pInput.setLayout(new GridLayout(2, 3, gap, gap));


        JLabel location = new JLabel("JAR路徑:");
        locationText = new JTextField(10);
        openItem = new JButton("選取");// 創建“打開"菜單項

        JLabel type = new JLabel("保存路徑:");
        typeText = new JTextField(10);
        saveItem = new JButton("選取");// 創建“保存"菜單項

        openDia = new FileDialog(f, "打開", FileDialog.LOAD);
        saveDia = new FileDialog(f, "保存", FileDialog.SAVE);

        JButton b = new JButton("生成");

        pInput.add(location);
        pInput.add(locationText);
        pInput.add(openItem);
        pInput.add(type);
        pInput.add(typeText);
        pInput.add(saveItem);

        //文本域
        final JTextArea ta = new JTextArea();
        ta.setLineWrap(true);
        b.setBounds(220, 60 + 30, 80, 30);
        ta.setBounds(gap, 80 + 60, 523, 120);

        f.add(pInput);
        f.add(b);
        f.add(ta);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

        myEvent();
        //鼠標監聽
        b.addActionListener(new ActionListener() {
            boolean checkedpass = true;

            public void actionPerformed(ActionEvent e) {
                XKey xKey = null;
                System.out.println(" --- : "+fileName);
                String newPath = filePathNew+"\\"+fileName+"-xjar.jar";
                String oldPath = filePathOld;
                try {
                    xKey = XKit.key(password);
                    XBoot.encrypt(oldPath, newPath, xKey, XConstants.MODE_DANGER);
                    ta.append(" Success! ");
                } catch (NoSuchAlgorithmException eg) {
                    eg.printStackTrace();
                } catch (Exception eg) {
                    eg.printStackTrace();
                }
                checkedpass = true;
                checkEmpty(locationText, "JAR路徑");
                checkEmpty(typeText, "保存路徑");

                if (checkedpass) {
                    String model = "加密jar包保存路徑 :%s";
                    String result = String.format(model, newPath);
                    ta.setText("");
                    ta.append(result);
                }

            }

            //檢驗是否為空
            private void checkEmpty(JTextField tf, String msg) {
                if (!checkedpass)
                    return;
                String value = tf.getText();
                if (value.length() == 0) {
                    JOptionPane.showMessageDialog(f, msg + " 不能為空");
                    tf.grabFocus();
                    checkedpass = false;
                }
            }
        });
    }

    //選取按鈕監聽
    private static void myEvent() {

        // 選取監聽
        openItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                openDia.setVisible(true);//顯示打開文件對話框
                String dirpath = openDia.getDirectory();//獲取打開文件路徑並保存到字符串中。
                String Name = openDia.getFile();//獲取打開文件名稱並保存到字符串中
                System.out.println(" --- : " + Name.split(".jar")[0]);
                fileName = Name.split(".jar")[0];
                filePathOld = dirpath+Name;
                if (dirpath == null || Name == null)//判斷路徑和文件是否為空
                    return;

                locationText.setText(dirpath+Name);
            }
        });

        // 選取監聽
        saveItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                chooser.showDialog(new JLabel(), "選擇");
                File file = chooser.getSelectedFile();
                if (file == null) {
                    System.out.println(" 路徑 :"+file.getAbsoluteFile().toString());
                    typeText.setText(file.getAbsoluteFile().toString());
                    filePathNew = file.getAbsoluteFile().toString();
                }else{
                    System.out.println(" 路徑 :"+file.getAbsoluteFile().toString());
                    typeText.setText(file.getAbsoluteFile().toString());
                    filePathNew = file.getAbsoluteFile().toString();
                }
            }
        });
        // 窗體關閉監聽
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

 

代碼先進行打包,Maven ——> clean ——> install

 

 

 

代碼運行測試

選擇jar文件,保存路徑,點擊生成即可。

 

下載exe生成器

1.

exe4j : https://www.apponic.com/phome/download/95409-2-1593674778-a86aa6a3165855674c85c5a9dc7c8dbb/

下載安裝完成后,界面如圖,
第一步完成注冊,Name,Company隨便填就是了,注冊碼:A-XVK258563F-1p4lv7mg7sav (網上很多啊,隨便搜一個就行)

 

 

 

2.

點擊 next ,勾選JAR in EXE

 

 

 

3.

點擊 next ,填寫打包后的 exe程序名稱和保存路徑

 

 

 

4.

點擊 next ,設置程序方式,兼容系統64位

 

 

 

5.

點擊 next ,添加jar包,選擇運行的主類的main方法

 

 

 

 

 

 

 

 

 

6.

點擊 next ,設置JDK運行版本

 

 

 

7.

復制jre,放到jre的空文件夾下:D:/jre/jre

 

 

 

 

 

 

8.

加載jre環境

 

 

 

 

 

 

 

 

 

9.

一直點 next ,等待安裝完成。

 

 

 

 

 10.

嘗試運行exe文件

 

 

 

 

  成功!!

 

如果報錯 java.lang.NoClassDefFoundError: BOOT-INF/classes/com/**/java/**** (wrong name: com/**/java/****) 返回第5步修改啟動類進行嘗試。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM