JAVA版exe可执行加密软件

1187163927ch 2018-04-09 原文

JAVA版exe可执行加密软件

1.现在eclipse(myeclipse)中插入以下代码

  1.1 MainForm

    

 1 package cee.hui.myfile;
 2 import javax.swing.*;
 3 import java.awt.*;
 4 import java.util.logging.Logger;
 5 
 6 import cee.hui.myfile.CheckCode;
 7 public class MainForm extends JFrame {
 8   /**
 9    * 构造界面
10    * 
11    * @author chenh
12    */
13   private static final long serialVersionUID = 1L;
14   /* 主窗体里面的若干元素 */
15   private JFrame mainForm = new JFrame("TXT文件加密"); // 主窗体,标题为“TXT文件加密”
16   private JLabel label1 = new JLabel("请选择待加密或解密的文件:");
17   private JLabel label2 = new JLabel("请选择加密或解密后的文件存放位置:");
18   public static JTextField sourcefile = new JTextField(); // 选择待加密或解密文件路径的文本域
19   public static JTextField targetfile = new JTextField(); // 选择加密或解密后文件路径的文本域
20   public static JButton buttonBrowseSource = new JButton("浏览"); // 浏览按钮
21   public static JButton buttonBrowseTarget = new JButton("浏览"); // 浏览按钮
22   public static JButton buttonEncrypt = new JButton("加密"); // 加密按钮
23   public static JButton buttonDecrypt = new JButton("解密"); // 解密按钮
24   public MainForm() {
25     Container container = mainForm.getContentPane();
26     /* 设置主窗体属性 */
27     mainForm.setSize(400, 270);// 设置主窗体大小
28     mainForm.setTitle("陈辉专用加密,解压软件 QQ:1187163927");
29     mainForm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);// 设置主窗体关闭按钮样式
30     mainForm.setLocationRelativeTo(null);// 设置居于屏幕中央
31     mainForm.setResizable(false);// 设置窗口不可缩放
32     mainForm.setLayout(null);
33     mainForm.setVisible(true);// 显示窗口
34     /* 设置各元素位置布局 */
35     label1.setBounds(30, 10, 300, 30);
36     sourcefile.setBounds(50, 50, 200, 30);
37     buttonBrowseSource.setBounds(270, 50, 60, 30);
38     label2.setBounds(30, 90, 300, 30);
39     targetfile.setBounds(50, 130, 200, 30);
40     buttonBrowseTarget.setBounds(270, 130, 60, 30);
41     buttonEncrypt.setBounds(100, 180, 60, 30);
42     buttonDecrypt.setBounds(200, 180, 60, 30);
43     /* 为各元素绑定事件监听器 */
44     buttonBrowseSource.addActionListener(new BrowseAction()); // 为源文件浏览按钮绑定监听器,点击该按钮调用文件选择窗口
45     buttonBrowseTarget.addActionListener(new BrowseAction()); // 为目标位置浏览按钮绑定监听器,点击该按钮调用文件选择窗口
46     buttonEncrypt.addActionListener(new EncryptAction()); // 为加密按钮绑定监听器,单击加密按钮会对源文件进行加密并输出到目标位置
47     buttonDecrypt.addActionListener(new DecryptAction()); // 为解密按钮绑定监听器,单击解密按钮会对源文件进行解密并输出到目标位置
48     sourcefile.getDocument().addDocumentListener(new TextFieldAction());// 为源文件文本域绑定事件,如果文件是.txt类型,则禁用解密按钮;如果是.chenh文件,则禁用加密按钮。
49     sourcefile.setEditable(false);// 设置源文件文本域不可手动修改
50     targetfile.setEditable(false);// 设置目标位置文本域不可手动修改
51     container.add(label1);
52     container.add(label2);
53     container.add(sourcefile);
54     container.add(targetfile);
55     container.add(buttonBrowseSource);
56     container.add(buttonBrowseTarget);
57     container.add(buttonEncrypt);
58     container.add(buttonDecrypt);
59   }
60   public static void main(String[] args) {
61      CheckCode checkCode = new CheckCode();
62      boolean result = false;
63      do {
64          try {
65              result = checkCode.Check(checkCode.CodeInput());
66         } catch (Exception e) {
67             result = checkCode.Check(checkCode.CodeInput());
68         }
69     } while (!result);
70       if (result){
71           new MainForm();
72     }
73            
74 }
75 }

View Code

  1.2  BrowseAction   

    

 1 package cee.hui.myfile;
 2 import java.awt.event.ActionEvent;
 3 import java.awt.event.ActionListener;
 4 import javax.swing.JFileChooser;
 5 import javax.swing.filechooser.FileNameExtensionFilter;
 6 public class BrowseAction implements ActionListener {
 7   public void actionPerformed(ActionEvent e) {
 8     if (e.getSource().equals(MainForm.buttonBrowseSource)) {
 9       JFileChooser fcDlg = new JFileChooser();
10       fcDlg.setDialogTitle("请选择待加密或解密的文件...");
11       FileNameExtensionFilter filter = new FileNameExtensionFilter(
12           "文本文件(*.txt;*.chenh)", "txt", "chenh");
13       fcDlg.setFileFilter(filter);
14       int returnVal = fcDlg.showOpenDialog(null);
15       if (returnVal == JFileChooser.APPROVE_OPTION) {
16         String filepath = fcDlg.getSelectedFile().getPath();
17         MainForm.sourcefile.setText(filepath);
18       }
19     } else if (e.getSource().equals(MainForm.buttonBrowseTarget)) {
20       JFileChooser fcDlg = new JFileChooser();
21       fcDlg.setDialogTitle("请选择加密或解密后的文件存放目录");
22       fcDlg.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
23       int returnVal = fcDlg.showOpenDialog(null);
24       if (returnVal == JFileChooser.APPROVE_OPTION) {
25         String filepath = fcDlg.getSelectedFile().getPath();
26         MainForm.targetfile.setText(filepath);
27       }
28     }
29   }
30 }

View Code

  1.3  CheckCode

    

 1 package cee.hui.myfile;
 2 
 3 import javax.swing.JOptionPane;
 4 
 5 //授权码
 6 public class CheckCode {
 7 public boolean Check(String code) {
 8     if (code.equals("chenhui")) {return true;}
 9         return false;
10 }
11 public String CodeInput() {
12     return JOptionPane.showInputDialog("请输入邀请码","请QQ联系:1187163927");
13 }
14 }

View Code

  1.4 DecryptAction    

    

 1 package cee.hui.myfile;
 2 import java.awt.event.ActionEvent;
 3 import java.awt.event.ActionListener;
 4 import java.io.File;
 5 import java.io.FileReader;
 6 import java.io.FileWriter;
 7 import java.io.IOException;
 8 import javax.swing.JOptionPane;
 9 public class DecryptAction implements ActionListener {
10   public void actionPerformed(ActionEvent e) {
11     // TODO Auto-generated method stub
12     if (MainForm.sourcefile.getText().isEmpty()) {
13       JOptionPane.showMessageDialog(null, "请选择待解密文件!");
14     }
15     else if (MainForm.targetfile.getText().isEmpty()) {
16       JOptionPane.showMessageDialog(null, "请选择解密后文件存放目录!");
17     }
18     else {
19       String sourcepath = MainForm.sourcefile.getText();
20       String targetpath = MainForm.targetfile.getText();
21       File file = new File(sourcepath);
22       String filename = file.getName();
23       File dir = new File(targetpath);
24       if (file.exists() && dir.isDirectory()) {
25         File result = new File(getFinalFile(targetpath, filename));
26         if (!result.exists()) {
27           try {
28             result.createNewFile();
29           } catch (IOException e1) {
30             JOptionPane.showMessageDialog(null,
31                 "目标文件创建失败,请检查目录是否为只读!");
32           }
33         }
34         try {
35           FileReader fr = new FileReader(file);
36           FileWriter fw = new FileWriter(result);
37           int ch = 0;
38           while ((ch = fr.read()) != -1) {
39             // System.out.print(Encrypt(ch));
40             fw.write(Decrypt(ch));
41           }
42           fw.close();
43           fr.close();
44           JOptionPane.showMessageDialog(null, "解密成功!");
45         } catch (Exception e1) {
46           JOptionPane.showMessageDialog(null, "未知错误!");
47         }
48       }
49       else if (!file.exists()) {
50         JOptionPane.showMessageDialog(null, "待解密文件不存在!");
51       } else {
52         JOptionPane.showMessageDialog(null, "解密后文件存放目录不存在!");
53       }
54     }
55   }
56   public char Decrypt(int ch) {
57     // double x = 0 - Math.pow(ch, 2);
58     int x = ch - 1;
59     return (char) (x);
60   }
61   public String getFinalFile(String targetpath, String filename) {
62     int length = filename.length();
63     String finalFileName = filename.substring(0, length - 4);
64     String finalFile = targetpath + "\\" + finalFileName + ".txt";
65     return finalFile;
66   }
67 }

View Code

  1.5  EncryptAction

    

 1 package cee.hui.myfile;
 2 import java.awt.event.ActionEvent;
 3 import java.awt.event.ActionListener;
 4 import java.io.File;
 5 import java.io.FileReader;
 6 import java.io.FileWriter;
 7 import java.io.IOException;
 8 import javax.swing.JOptionPane;
 9 public class EncryptAction implements ActionListener {
10   public void actionPerformed(ActionEvent e) {
11     // TODO Auto-generated method stub
12     if (MainForm.sourcefile.getText().isEmpty()) {
13       JOptionPane.showMessageDialog(null, "请选择待加密文件!");
14     }
15     else if (MainForm.targetfile.getText().isEmpty()) {
16       JOptionPane.showMessageDialog(null, "请选择加密后文件存放目录!");
17     }
18     else {
19       String sourcepath = MainForm.sourcefile.getText();
20       String targetpath = MainForm.targetfile.getText();
21       File file = new File(sourcepath);
22       String filename = file.getName();
23       File dir = new File(targetpath);
24       if (file.exists() && dir.isDirectory()) {
25         File result = new File(getFinalFile(targetpath, filename));
26         if (!result.exists()) {
27           try {
28             result.createNewFile();
29           } catch (IOException e1) {
30             JOptionPane.showMessageDialog(null,
31                 "目标文件创建失败,请检查目录是否为只读!");
32           }
33         }
34         try {
35           FileReader fr = new FileReader(file);
36           FileWriter fw = new FileWriter(result);
37           int ch = 0;
38           while ((ch = fr.read()) != -1) {
39             // System.out.print(Encrypt(ch));
40             fw.write(Encrypt(ch));
41           }
42           fw.close();
43           fr.close();
44           JOptionPane.showMessageDialog(null, "加密成功!");
45         } catch (Exception e1) {
46           JOptionPane.showMessageDialog(null, "未知错误!");
47         }
48       }
49       else if (!file.exists()) {
50         JOptionPane.showMessageDialog(null, "待加密文件不存在!");
51       } else {
52         JOptionPane.showMessageDialog(null, "加密后文件存放目录不存在!");
53       }
54     }
55   }
56   public char Encrypt(int ch) {
57     int x = ch + 1;
58     return (char) (x);
59   }
60   public String getFinalFile(String targetpath, String filename) {
61     int length = filename.length();
62     String finalFileName = filename.substring(0, length - 4);
63     String finalFile = targetpath + "\\" + finalFileName + ".chenh";
64     return finalFile;
65   }
66 }

View Code

  1.6  TextFieldAction

    

 1 package cee.hui.myfile;
 2 import javax.swing.event.DocumentEvent;
 3 import javax.swing.event.DocumentListener;
 4 public class TextFieldAction implements DocumentListener {
 5   public void insertUpdate(DocumentEvent e) {
 6     // TODO Auto-generated method stub
 7     ButtonAjust();
 8   }
 9   public void removeUpdate(DocumentEvent e) {
10     // TODO Auto-generated method stub
11     ButtonAjust();
12   }
13   public void changedUpdate(DocumentEvent e) {
14     // TODO Auto-generated method stub
15     ButtonAjust();
16   }
17   public void ButtonAjust() {
18     String file = MainForm.sourcefile.getText();
19     if (file.endsWith("txt")) {
20       MainForm.buttonDecrypt.setEnabled(false);
21       MainForm.buttonEncrypt.setEnabled(true);
22     }
23     if (file.endsWith("chenh")) {
24       MainForm.buttonEncrypt.setEnabled(false);
25       MainForm.buttonDecrypt.setEnabled(true);
26     }
27   }
28 }

View Code

对于本博客所写出运行的结果,稍微懂一点java代码的程序员即可改掉程序运行出来的第一句弹出框,因为未连接网络数据库,程序中只是测试写死的邀请码。 

将以上文件编译好成可执行文件后,导出成可执行的JAR文件

具体步骤如下:File(文件) ->  Export(导出) -> Java -> Runnable JAR File(可执行的JAR文件) -> Next(下一步) 

在Launch configuration(配置启动文件):选择MainForm

在Export destination(导出路径)选择你需要放的文件路径。

在Library handing 选择 Package requared libraries into generated JAR

如下图:

直接点击Finish(完成)。

至此,你已经完成加密文件的大半,但此文件只限于JAR文件,并不是太美观。

 你可以使用 jar转exe文件将jar文件转为exe。

下载连接

jar转exe

jar测试文件

exe测试文件

以上涉及到的邀请码都为:chenhui

 

发表于 2018-04-09 17:15 Lovemywx2 阅读() 评论() 编辑 收藏

 

版权声明:本文为1187163927ch原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/1187163927ch/p/8761190.html

JAVA版exe可执行加密软件的更多相关文章

  1. 数据库设计杂谈

    注:本人开发经验尚浅,下文主要谈的是自己的一些想法,不足之处请指出。 最近半年时间都花在管理系统的开放上面,对 […]...

  2. java 常用线程池介绍

    一、线程池简介 线程池就是预先创建好多n个空闲线程,节省了每次使用线程时都要去创建的时间,使用时只要从线程池中取出,用完之后再还给线程池。就像现在的共享经济一样,需要的时候只要去“借”,用完之后只需还回去就行。“池”的概念都是为了节省时间...

  3. 一篇文章让你了解GC垃圾回收器

    简单了解GC垃圾回收器 了解GC之前我们首先要了解GC是要做什么的?顾名思义回收垃圾,什么是垃圾呢? GC回收 […]...

  4. java基础(一) 深入解析基本类型

    。   浮点数使用 IEEE(电气和电子工程师协会)格式。 浮点数类型使用 符号位、指数、有效位数(尾数)来表 […]...

  5. Java int类型转String类型两种方式性能差异

    Java int类型转String类型两种方式性能差异 2018-08-30 15:10 by gene199 […]...

  6. Java 开发环境配置–eclipse工具进行java开发

    Java 开发环境配置 在本章节中我们将为大家介绍如何搭建Java开发环境。 Windows 上安装开发环境 […]...

  7. 全面了解 Java 原子变量类

    ...

  8. 从零单排学Redis【铂金二】

    前言 只有光头才能变强 好的,今天我们要上【铂金二】了,如果还没有上铂金的,赶紧先去蹭蹭经验再回来(不然不带你 […]...

随机推荐

  1. 头部导航栏也是动态的,板块里面的内容根据头部导航栏动态展现数据

    大家请看上面这幅图,它惟妙惟肖,栩栩如生,真好看。嘿,醒一下,给大家讲解一下这张图的构造哈,头部三个导航栏,哦 […]...

  2. 查看和修改linux系统时间

    一、查看和修改Linux的时区1. 查看当前时区 命令 : “date -R” 2. […]...

  3. 基于Apache Hudi构建数据湖的典型应用场景介绍

    1. 传统数据湖存在的问题与挑战 传统数据湖解决方案中,常用Hive来构建T+1级别的数据仓库,通过HDFS存 […]...

  4. 核心支付业务

    仅以我所见的第三方支付公司为例,讲一下核心的支付业务与流程。  核心的支付业务:   一、组织架构:      […]...

  5. HTML5学习总结-08 应用缓存(Application Cache)

    一 应用缓存(Application Cache) 1 应用缓存   HTML5 引入了应用程序缓存,这意味着 […]...

  6. iOS屏幕适配 支持新手机 iPhone XR iPhone XS 超简单

    随着苹果爸爸发布了 超牛叉的iPhone iPhone X 、iPhone XR、iPhone XS 、iPh […]...

  7. Python 学习笔记(1)Python容器:列表、元组、字典与集合

    Python 学习笔记(1)Python容器:列表、元组、字典与集合 Python容器:列表、元组、字典与集合 […]...

  8. 阿里云ECS服务器windows环境下配置redis

    一、下载解压redis github下载地址:https://github.com/MSOpenTech/re […]...

展开目录

目录导航