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

  1.1 MainForm

    

 package cee.hui.myfile;
import javax.swing.*;
import java.awt.*;
import java.util.logging.Logger; import cee.hui.myfile.CheckCode;
public class MainForm extends JFrame {
/**
* 构造界面
*
* @author chenh
*/
private static final long serialVersionUID = 1L;
/* 主窗体里面的若干元素 */
private JFrame mainForm = new JFrame("TXT文件加密"); // 主窗体,标题为“TXT文件加密”
private JLabel label1 = new JLabel("请选择待加密或解密的文件:");
private JLabel label2 = new JLabel("请选择加密或解密后的文件存放位置:");
public static JTextField sourcefile = new JTextField(); // 选择待加密或解密文件路径的文本域
public static JTextField targetfile = new JTextField(); // 选择加密或解密后文件路径的文本域
public static JButton buttonBrowseSource = new JButton("浏览"); // 浏览按钮
public static JButton buttonBrowseTarget = new JButton("浏览"); // 浏览按钮
public static JButton buttonEncrypt = new JButton("加密"); // 加密按钮
public static JButton buttonDecrypt = new JButton("解密"); // 解密按钮
public MainForm() {
Container container = mainForm.getContentPane();
/* 设置主窗体属性 */
mainForm.setSize(400, 270);// 设置主窗体大小
mainForm.setTitle("陈辉专用加密,解压软件 QQ:1187163927");
mainForm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);// 设置主窗体关闭按钮样式
mainForm.setLocationRelativeTo(null);// 设置居于屏幕中央
mainForm.setResizable(false);// 设置窗口不可缩放
mainForm.setLayout(null);
mainForm.setVisible(true);// 显示窗口
/* 设置各元素位置布局 */
label1.setBounds(30, 10, 300, 30);
sourcefile.setBounds(50, 50, 200, 30);
buttonBrowseSource.setBounds(270, 50, 60, 30);
label2.setBounds(30, 90, 300, 30);
targetfile.setBounds(50, 130, 200, 30);
buttonBrowseTarget.setBounds(270, 130, 60, 30);
buttonEncrypt.setBounds(100, 180, 60, 30);
buttonDecrypt.setBounds(200, 180, 60, 30);
/* 为各元素绑定事件监听器 */
buttonBrowseSource.addActionListener(new BrowseAction()); // 为源文件浏览按钮绑定监听器,点击该按钮调用文件选择窗口
buttonBrowseTarget.addActionListener(new BrowseAction()); // 为目标位置浏览按钮绑定监听器,点击该按钮调用文件选择窗口
buttonEncrypt.addActionListener(new EncryptAction()); // 为加密按钮绑定监听器,单击加密按钮会对源文件进行加密并输出到目标位置
buttonDecrypt.addActionListener(new DecryptAction()); // 为解密按钮绑定监听器,单击解密按钮会对源文件进行解密并输出到目标位置
sourcefile.getDocument().addDocumentListener(new TextFieldAction());// 为源文件文本域绑定事件,如果文件是.txt类型,则禁用解密按钮;如果是.chenh文件,则禁用加密按钮。
sourcefile.setEditable(false);// 设置源文件文本域不可手动修改
targetfile.setEditable(false);// 设置目标位置文本域不可手动修改
container.add(label1);
container.add(label2);
container.add(sourcefile);
container.add(targetfile);
container.add(buttonBrowseSource);
container.add(buttonBrowseTarget);
container.add(buttonEncrypt);
container.add(buttonDecrypt);
}
public static void main(String[] args) {
CheckCode checkCode = new CheckCode();
boolean result = false;
do {
try {
result = checkCode.Check(checkCode.CodeInput());
} catch (Exception e) {
result = checkCode.Check(checkCode.CodeInput());
}
} while (!result);
if (result){
new MainForm();
} }
}

  1.2  BrowseAction   

    

 package cee.hui.myfile;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
public class BrowseAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(MainForm.buttonBrowseSource)) {
JFileChooser fcDlg = new JFileChooser();
fcDlg.setDialogTitle("请选择待加密或解密的文件...");
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"文本文件(*.txt;*.chenh)", "txt", "chenh");
fcDlg.setFileFilter(filter);
int returnVal = fcDlg.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String filepath = fcDlg.getSelectedFile().getPath();
MainForm.sourcefile.setText(filepath);
}
} else if (e.getSource().equals(MainForm.buttonBrowseTarget)) {
JFileChooser fcDlg = new JFileChooser();
fcDlg.setDialogTitle("请选择加密或解密后的文件存放目录");
fcDlg.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = fcDlg.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String filepath = fcDlg.getSelectedFile().getPath();
MainForm.targetfile.setText(filepath);
}
}
}
}

  1.3  CheckCode

    

 package cee.hui.myfile;

 import javax.swing.JOptionPane;

 //授权码
public class CheckCode {
public boolean Check(String code) {
if (code.equals("chenhui")) {return true;}
return false;
}
public String CodeInput() {
return JOptionPane.showInputDialog("请输入邀请码","请QQ联系:1187163927");
}
}

  1.4 DecryptAction    

    

 package cee.hui.myfile;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;
public class DecryptAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (MainForm.sourcefile.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请选择待解密文件!");
}
else if (MainForm.targetfile.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请选择解密后文件存放目录!");
}
else {
String sourcepath = MainForm.sourcefile.getText();
String targetpath = MainForm.targetfile.getText();
File file = new File(sourcepath);
String filename = file.getName();
File dir = new File(targetpath);
if (file.exists() && dir.isDirectory()) {
File result = new File(getFinalFile(targetpath, filename));
if (!result.exists()) {
try {
result.createNewFile();
} catch (IOException e1) {
JOptionPane.showMessageDialog(null,
"目标文件创建失败,请检查目录是否为只读!");
}
}
try {
FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(result);
int ch = 0;
while ((ch = fr.read()) != -1) {
// System.out.print(Encrypt(ch));
fw.write(Decrypt(ch));
}
fw.close();
fr.close();
JOptionPane.showMessageDialog(null, "解密成功!");
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "未知错误!");
}
}
else if (!file.exists()) {
JOptionPane.showMessageDialog(null, "待解密文件不存在!");
} else {
JOptionPane.showMessageDialog(null, "解密后文件存放目录不存在!");
}
}
}
public char Decrypt(int ch) {
// double x = 0 - Math.pow(ch, 2);
int x = ch - 1;
return (char) (x);
}
public String getFinalFile(String targetpath, String filename) {
int length = filename.length();
String finalFileName = filename.substring(0, length - 4);
String finalFile = targetpath + "\\" + finalFileName + ".txt";
return finalFile;
}
}

  1.5  EncryptAction

    

 package cee.hui.myfile;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;
public class EncryptAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (MainForm.sourcefile.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请选择待加密文件!");
}
else if (MainForm.targetfile.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "请选择加密后文件存放目录!");
}
else {
String sourcepath = MainForm.sourcefile.getText();
String targetpath = MainForm.targetfile.getText();
File file = new File(sourcepath);
String filename = file.getName();
File dir = new File(targetpath);
if (file.exists() && dir.isDirectory()) {
File result = new File(getFinalFile(targetpath, filename));
if (!result.exists()) {
try {
result.createNewFile();
} catch (IOException e1) {
JOptionPane.showMessageDialog(null,
"目标文件创建失败,请检查目录是否为只读!");
}
}
try {
FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(result);
int ch = 0;
while ((ch = fr.read()) != -1) {
// System.out.print(Encrypt(ch));
fw.write(Encrypt(ch));
}
fw.close();
fr.close();
JOptionPane.showMessageDialog(null, "加密成功!");
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "未知错误!");
}
}
else if (!file.exists()) {
JOptionPane.showMessageDialog(null, "待加密文件不存在!");
} else {
JOptionPane.showMessageDialog(null, "加密后文件存放目录不存在!");
}
}
}
public char Encrypt(int ch) {
int x = ch + 1;
return (char) (x);
}
public String getFinalFile(String targetpath, String filename) {
int length = filename.length();
String finalFileName = filename.substring(0, length - 4);
String finalFile = targetpath + "\\" + finalFileName + ".chenh";
return finalFile;
}
}

  1.6  TextFieldAction

    

 package cee.hui.myfile;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class TextFieldAction implements DocumentListener {
public void insertUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
ButtonAjust();
}
public void removeUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
ButtonAjust();
}
public void changedUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
ButtonAjust();
}
public void ButtonAjust() {
String file = MainForm.sourcefile.getText();
if (file.endsWith("txt")) {
MainForm.buttonDecrypt.setEnabled(false);
MainForm.buttonEncrypt.setEnabled(true);
}
if (file.endsWith("chenh")) {
MainForm.buttonEncrypt.setEnabled(false);
MainForm.buttonDecrypt.setEnabled(true);
}
}
}

对于本博客所写出运行的结果,稍微懂一点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

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

  1. JAVA 调用exe程序执行对应的文件 (个人用于编译Java文件)

    需求: 需要利用Java程序,来调用计算机本身的黑窗口,来将特定的Java文件编译成对应的字节码文件. 实现思路: 通过调用Java的Runtime类,每个 Java 应用程序都有一个 Runtime ...

  2. Java项目生成可执行jar包、exe文件以及在Windows下的安装文件

    1.如何通过eclipse将Java项目生成可执行jar包 首先把在eclipse下的java项目导出jar file 下一步 下一步 下一步 最后点击完成,便生成了可执行的jar文件.可以在刚刚选择 ...

  3. Selenium执行完毕未关闭chromedriver/geckodriver进程的解决办法(java版+python版)

    selenium操作chrome浏览器需要有ChromeDriver驱动来协助.webdriver中关浏览器关闭有两个方法,一个叫quit,一个叫close. 1 /** 2 * Close the ...

  4. 【转载】java版打字练习软件

    网上找到一个java版的打字的游戏 import java.applet.Applet; import java.applet.AudioClip; import java.awt.Dimension ...

  5. 第九篇 :微信公众平台开发实战Java版之如何实现自定义分享内容

    第一部分:微信JS-SDK介绍 微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包. 通过使用微信JS-SDK,网页开发者可借助微信高效地使用拍照.选图.语音.位置等手机系统 ...

  6. 打包java程序生成exe

    打包java程序生成exe 目标 我们知道c++的控制台程序编译运行以后就会生成一个exe可执行文件,在电脑上可以直接双击运行,但是java是跨平台的语言,编译运行以后的字节码文件.class是和平台 ...

  7. 玩玩微信公众号Java版之七:自定义微信分享

    前面已经学会了微信网页授权,现在微信网页的功能也可以开展起来啦! 首先,我们先来学习一下分享,如何在自己的页面获取分享接口及让小伙伴来分享呢? 今天的主人公: 微信 JS-SDK, 对应官方链接为:微 ...

  8. 【转】搭建Java版WebService

    原文地址:http://www.cnblogs.com/jasoncc/archive/2011/12/22/2296052.html Hi,大家好! 今天主要和大家分享,如何搭建一个Web服务,做A ...

  9. 常见排序算法题(java版)

    常见排序算法题(java版) //插入排序:   package org.rut.util.algorithm.support;   import org.rut.util.algorithm.Sor ...

随机推荐

  1. JMockit常用操作

    JMockit常用操作 2017-11-30 转自:http://blog.csdn.net/foreverling/article/details/51234149 目录 1 基本概念  1.1 常 ...

  2. handsontable 事件汇总

    Hook插件 afterChange (changes: Array, source: String):1个或多个单元格的值被改变后调用 changes:是一个2维数组包含row,prop,oldVa ...

  3. python打造社工脚本

    0x00前言: 大家都知道图片是有Exif信息的.里面包含着 你的GPS信息.还有拍摄时间等等的敏感信息. 0x01准备: exifread requests 0x02思路: 读取图片的Exif信息. ...

  4. 为 Debian 8 或 Debian 9(64 位)安装 .NET Core

    在 Debian 8 或 Debian 9(64 位)上安装 .NET Core 的具体步骤: 备注:必须有用户控制目录,才能通过 tar.gz 在 Linux 系统上进行安装. 1.准备一台刚安装的 ...

  5. 安装anaconda与tensorflow

    在安装Anaconda之前,有的已经安装过一个Python版本了,但是又不想删除这个Python版本,该怎么办呢? 安装anaconda与tensorflow一条龙 jupyter notebook ...

  6. 20165230 预备作业3 Linux安装及学习

    20165230 预备作业3 Linux安装及学习 安装Linux操作系统 通过学习实践基于VirtualBox虚拟机安装Ubuntu图文教程,开始了虚拟机的安装,根据教程按着步骤一步一步的完成. 遇 ...

  7. C++模板类与Qt信号槽混用

    一.正文 目前正在做一个视频处理相关的项目.项目的技术栈是这样的,UI层采用Qt来实现基本的数据展示和交互,底层音视频采用的是一套基于FFmpeg的视频处理框架.这是一套类似Microsoft Med ...

  8. 2017年PHP程序员未来路在何方——韩天峰

    PHP 从诞生到现在已经有20多年历史,从Web时代兴起到移动互联网退潮,互联网领域各种编程语言和技术层出不穷, Node.js . GO . Python 不断地在挑战 PHP 的地位.这些技术的推 ...

  9. php数组排序和查找的算法

    1.php算法 // 算法 // 1.冒泡排序 => 思路:​每次循环排列出一个最大的数 // echo '<pre>'; $arr = [ 1,43,54,62,21,66,32, ...

  10. 工作流Activiti5.13学习笔记(一)

    了解工作流 1.工作流(Workflow),就是“业务过程的部分或整体在计算机应用环境下的自动化”,它主要解决的是“使在多个参与者之间按照某种预定义的规则传递文档.信息或任务的过程自动进行,从而实现某 ...