• 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

    欢迎访问我的博客! http://www.cnblogs.com/1187163927ch/
  • 相关阅读:
    用于爬取知乎某个话题下的精华问题中所有回答的爬虫
    BSP -- 图书共享系统(Book Sharing Platform)
    【已解决】WPS2018 从第三页开始插入页眉页码(即前两页不要页眉页码)
    【编译原理】大白话讲解 First 集和 Follow 集的构造算法
    如果
    HTTP协议(1)------->资源和URL
    JavaWeb框架_Struts2_(八)----->Struts2的国际化
    深入理解java虚拟机----->垃圾收集器与内存分配策略(下)
    深入理解java虚拟机----->垃圾收集器与内存分配策略(上)
    JavaWeb框架_Struts2_(七)----->文件的上传和下载
  • 原文地址:https://www.cnblogs.com/1187163927ch/p/8761190.html
Copyright © 2020-2023  润新知