留着参考

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版)的更多相关文章

  1. 【Java】Swing+IO流实现一个简单的文件加密程序(较完整版)

    留着参考 beans package com.my.bean; import java.io.Serializable; public class EncryptedFile implements S ...

  2. Java基础-IO流对象之随机访问文件(RandomAccessFile)

    Java基础-IO流对象之随机访问文件(RandomAccessFile) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.RandomAccessFile简介 此类的实例支持对 ...

  3. 使用IO流实现一个简单的小Dome

    (一) 在电脑D盘下创建一个文件为HelloWorld.txt文件,判断他是文件还是目录,在创建一个目录IOTest,之后将HelloWorld.txt移动到IOTest目录下去:之后遍历IOTest ...

  4. 【Java】IO流简单分辨

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5827509.html Java的IO流体系十分庞大,并且体系层次稍复杂,很容易记混或记错.在此,我把平时经常用 ...

  5. Java基础——IO流

    今天刚刚看完java的io流操作,把主要的脉络看了一遍,不能保证以后使用时都能得心应手,但是最起码用到时知道有这么一个功能可以实现,下面对学习进行一下简单的总结: IO流主要用于硬板.内存.键盘等处理 ...

  6. Java之IO流详解

    IO流 Input/Output 完成输入/输出 应用程序运行时——数据在内存中  ←→ 把数据写入硬盘(磁带)  内存中的数据不可持久保存的  输入:从外部存储器(硬盘.磁带.U盘)把数据读入内存. ...

  7. JAVA中IO流总结

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42119261 我想你对JAVA的IO流有所了解,平时使用的 ...

  8. Java的IO流——(七)

    目录结构:

  9. Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream)

    Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 之前我已经分享过很多的J ...

随机推荐

  1. .NET程序调试技巧(一):快速定位异常的一些方法

    作为一个程序员,解BUG是我们工作中常做的工作,甚至可以说解决问题能力是一个人工作能力的重要体现.因为这体现了一个程序员的技术水平.技术深度.经验等等. 那么在我们解决BUG的过程中,定位问题是非常重 ...

  2. Duilib教程-控件练习

    一.控件消息的响应. 在HelloDuilib例子中,程序不能退出,在这里,我将添加一个关闭按钮,当点击它时,调用PostQuitMessage进行退出. 首先在界面的右上角添加一个关闭按钮,并取名为 ...

  3. Camlistore名词解释

    Terminology 名词解释 To stay sane and communicate effectively we try hard to use consistent terminology ...

  4. [hihoCoder] KMP算法

    Each time we find a match, increase the global counter by 1. For KMP, algorithm, you may refer to th ...

  5. 160621、Java注解教程及自定义注解

    Java注解提供了关于代码的一些信息,但并不直接作用于它所注解的代码内容.在这个教程当中,我们将学习Java的注解,如何定制注解,注解的使用以及如何通过反射解析注解. Java1.5引入了注解,当前许 ...

  6. jquery ajax 传数据到后台乱码的处理方法

    前台页面先对中文进行编码,如下红色字体: function saveCommentTemplate() { $.ajax({ cache : false, type:'get', dataType:' ...

  7. 《挑战程序设计竞赛》2.6 数学问题-辗转相除法 AOJ0005 POJ2429 1930(1)

    AOJ0005 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0005 题意 给定两个数,求其最大公约数GCD以及最小公倍数LCM. ...

  8. 找不到ifconfig命令

    对于新安装的系统,可能会缺少ifconfig命令,这是因为少安装了net-tools工具,所以只要安装上即可. yum install net-tools -y

  9. 面向对象 - 1.封装之如何实现属性的隐藏/2.封装的意义/3.封装与扩展性/4.property的使用

    1.封装之如何实现属性的隐藏封装: __x=1 # 把数据属性隐藏 (如何实现隐藏) 类定义阶段 __开头发生了变形 __x --> _A__x特点: 1.在类外部无法直接:obj.__Attr ...

  10. 转!!xss漏洞

    参考资料 https://blog.csdn.net/jiangzhexi/article/details/56841793 http://www.freebuf.com/articles/web/4 ...