【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 ...
随机推荐
- OpenWrt 安装usb支持
(一)下载软件 1)komd-usb-ohci kmod-usb2 kmod-usb-storage kmod-usb-core 这些是USB驱动包 2) kmod-nls-base kmod-nls ...
- js压缩 uglify
grunt-contrib-uglify uglify是一个文件压缩插件,项目地址:https://github.com/gruntjs/grunt-contrib-uglify 本文将以一个DEMO ...
- 直接下载jdk压缩包方式安装
分为下面5个步骤 1.官网下载JDK 2.检查是否安装jdk,解压缩,放到指定目录 3.配置环境变量 4.设置系统默认JDK 5. 测试jdk 1.官网下载JDK 地址: http://ww ...
- 第一只python爬虫
import urllib.request response = urllib.request.urlopen("http://www.baidu.com") html = res ...
- HDU3308(LCIS) 线段树好题
题目链接:传送门 题目大意:给你n个数,m个操作.操作有两种:1.U x y 将数组第x位变为y 2. Q x y 问数组第x位到第y位连续最长子序列的长度.对于每次询问,输出一个答案 题目思路: ...
- 3162 抄书问题(划分dp)
3162 抄书问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 现在要把M本有顺序的书分给K个人复制( ...
- css3动画效果:3 3D动画
立方体旋转动画效果 css #container{ width: 400px; height: 400px; ; ; -webkit-perspective-origin:50% 225px; per ...
- 高性能Web开发系列
1. 高性能WEB开发基础 http://www.uml.org.cn/net/201404225.asp 2. 高性能WEB开发进阶(上) http://www.uml.org.cn/net/201 ...
- Pipeline inbound
精进篇:netty源码死磕7 巧夺天工--Pipeline入站流程详解 1. Pipeline的入站流程 在讲解入站处理流程前,先脑补和铺垫一下两个知识点: (1)如何向Pipeline添加一个Ha ...
- 微信公众号 待发货-物流中-已收货 foreach break continue
w <?php $warr = array(1,2,3); $w_break = 0; foreach($warr AS $w){ if($w==2)break; $w_break += $w; ...