【Java】Swing+IO流实现一个简单的文件加密程序(demo版)
留着参考
EncrytService
package com.my.service; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; public class EncryptService {
// 默认密匙路径
private static String DEFAULT_KEY_URL = ".//KEY";
// 临时文件路径
private static String DEFAULT_TEMP_URL = ".//TEMP";
// 读取密匙
private int key[] = new int[128];
private void readKey() {
File keyFile = new File(DEFAULT_KEY_URL); try {
FileInputStream localKey = new FileInputStream(keyFile);
for (int i = 0; i < 128; ++i) {
key[i] = localKey.read();
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 生成新的密匙
public void makeKey() {
try {
File keyFile = new File(DEFAULT_KEY_URL); FileOutputStream fos = new FileOutputStream(keyFile); for (int i = 0; i < 128; ++i) {
fos.write((int)(Math.random()*128));
}
readKey();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 加密文件
public void encryptFile(File file) {
try {
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(new File(DEFAULT_TEMP_URL)); int length = fis.available();
for (int i = 0; i < length; ++i) {
fos.write(fis.read() + key[i%128]);
}
fis.close();
fos.close();
FileInputStream fileInputStream = new FileInputStream(new File(DEFAULT_TEMP_URL));
FileOutputStream fileOutputStream = new FileOutputStream(file);
int length2 = fileInputStream.available();
for (int i = 0; i < length2; ++i) {
fileOutputStream.write(fileInputStream.read());
}
fileInputStream.close();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 解密文件
public void decryptFile(File file) {
try {
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(new File(DEFAULT_TEMP_URL)); int length = fis.available();
for (int i = 0; i < length; ++i) {
fos.write(fis.read() - key[i%128]);
}
fis.close();
fos.close();
FileInputStream fileInputStream = new FileInputStream(new File(DEFAULT_TEMP_URL));
FileOutputStream fileOutputStream = new FileOutputStream(file);
int length2 = fileInputStream.available();
for (int i = 0; i < length2; ++i) {
fileOutputStream.write(fileInputStream.read());
}
fileInputStream.close();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Main
package com.my.ui; import com.my.service.EncryptService; import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File; public class Main extends JFrame implements ActionListener {
private EncryptService encryptService = new EncryptService();
// 设置默认大小
private static final int DEFAULT_WIDTH = 396;
private static final int DEFAULT_HEIGHT = 145;
// 组件
private JFileChooser chooser;
private JButton buttonEncrypt;
private JButton buttonDecrypt;
private JButton buttonMakeKey;
JTextField fileText;
JTextField keyText;
// 文件路径
private String filePath;
private String keyPath;
// 初始化加密页面
public Main() {
setTitle("文件加密程序");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setResizable(false);
setLocationRelativeTo(null); JPanel panUser = new JPanel();
// 创建组件
fileText = new JTextField();
fileText.setEditable(false);
keyText = new JTextField();
keyText.setEditable(false);
JButton btnFile = new JButton("....");
btnFile.setFocusPainted(false);
JButton btnKey = new JButton("...");
btnKey.setFocusPainted(false);
btnKey.setEnabled(false);
// 布局
panUser.setLayout(new GridLayout(2, 3));
panUser.add(new JLabel("源文件路径:"));
panUser.add(fileText);
panUser.add(btnFile);
panUser.add(new JLabel("密匙路径:"));
panUser.add(keyText);
panUser.add(btnKey); buttonEncrypt = new JButton("加密");
buttonEncrypt.setFocusPainted(false);
buttonDecrypt = new JButton("解密");
buttonDecrypt.setFocusPainted(false);
buttonMakeKey = new JButton("生成新的密匙");
buttonMakeKey.setFocusPainted(false); JPanel panBtn = new JPanel();
panBtn.setLayout(new FlowLayout());
panBtn.add(buttonEncrypt);
panBtn.add(buttonDecrypt);
panBtn.add(buttonMakeKey); setLayout(new BorderLayout());
add(panUser, BorderLayout.CENTER);
add(panBtn, BorderLayout.SOUTH); // 注册事件监听
btnFile.addActionListener(this);
btnKey.addActionListener(this);
buttonMakeKey.addActionListener(this);
buttonEncrypt.addActionListener(this);
buttonDecrypt.addActionListener(this); chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
} public static void main(String[] args) {
JFrame frame = new Main();
frame.setVisible(true);
} @Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("....")) {
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
filePath = chooser.getSelectedFile().getPath();
fileText.setText(filePath);
}
}
if (e.getActionCommand().equals("...")) {
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
keyPath = chooser.getSelectedFile().getPath();
keyText.setText(keyPath);
}
}
if (e.getActionCommand().equals("加密")) {
encryptService.encryptFile(new File(filePath));
System.out.println("加密成功");
}
if (e.getActionCommand().equals("解密")) {
encryptService.decryptFile(new File(filePath));
System.out.println("解密成功");
}
if (e.getActionCommand().equals("生成新的密匙")) {
encryptService.makeKey();
keyText.setText(new File("").getAbsolutePath() + "\\KEY");
System.out.println("成功生成新的密匙");
}
}
}
【Java】Swing+IO流实现一个简单的文件加密程序(demo版)的更多相关文章
- 【Java】Swing+IO流实现一个简单的文件加密程序(较完整版)
留着参考 beans package com.my.bean; import java.io.Serializable; public class EncryptedFile implements S ...
- Java基础-IO流对象之随机访问文件(RandomAccessFile)
Java基础-IO流对象之随机访问文件(RandomAccessFile) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.RandomAccessFile简介 此类的实例支持对 ...
- 使用IO流实现一个简单的小Dome
(一) 在电脑D盘下创建一个文件为HelloWorld.txt文件,判断他是文件还是目录,在创建一个目录IOTest,之后将HelloWorld.txt移动到IOTest目录下去:之后遍历IOTest ...
- 【Java】IO流简单分辨
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5827509.html Java的IO流体系十分庞大,并且体系层次稍复杂,很容易记混或记错.在此,我把平时经常用 ...
- Java基础——IO流
今天刚刚看完java的io流操作,把主要的脉络看了一遍,不能保证以后使用时都能得心应手,但是最起码用到时知道有这么一个功能可以实现,下面对学习进行一下简单的总结: IO流主要用于硬板.内存.键盘等处理 ...
- Java之IO流详解
IO流 Input/Output 完成输入/输出 应用程序运行时——数据在内存中 ←→ 把数据写入硬盘(磁带) 内存中的数据不可持久保存的 输入:从外部存储器(硬盘.磁带.U盘)把数据读入内存. ...
- JAVA中IO流总结
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42119261 我想你对JAVA的IO流有所了解,平时使用的 ...
- Java的IO流——(七)
目录结构:
- Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream)
Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 之前我已经分享过很多的J ...
随机推荐
- smartJS 0.1 API 讲解 - PromiseEvent
上篇简单的介绍smartjs了一些通用方法的api.这篇介绍基础的PromiseEvent(这个名字一直没想好,以前准备用callbacks的,但避免与jquery混淆,st的命名空间可以直接挂到$上 ...
- C++ 检查Windows服务运行状态
检查Windows服务运行状态 C++ Code 1234567891011121314151617181920212223242526272829303132333435363738394041 ...
- 浅析Java与C#的事件处理机制
http://www.cnblogs.com/OOAbooke/archive/2012/02/18/2356899.html
- 好用的 Visual Studio插件
Image Optimizer(图片优化)插件 使用行业标准优化任何JPEG,PNG和gif(包括动画gif).可以做有损和无损的优化.
- 最基础的PHP分类查询程序
最初级的PHP分类查询程序 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...
- dubbo框架的介绍,应用
http://www.cnblogs.com/Javame/p/3632473.html
- SQL系统函数——系统信息
1.查看信息1.1.查看编号和名称select @@SERVERNAME--SQL SERVER服务器的连接字符串,如:computername\instancenameselect @@SERVIC ...
- ryu启动问题总结
在Mininet中启动ryu控制器,首先切换到ryu中的app目录下: cd ryu/ryu/app 启动ryu: ryu-manager simple_switch.py 遇到了如下的错误提示: 这 ...
- [Algorithms] Counting Sort
Counting sort is a linear time sorting algorithm. It is used when all the numbers fall in a fixed ra ...
- 关于redux应用
redux 有点类似flux.但是我觉得远比flux要复杂.因为他非常的绕.一般搭配使用是redux 和react-redux 使用. 主要的思路就是: 写action:动作类型 写reducer:动 ...