package day20.file;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
/** Create 2009-2-23 */
public class FileCode_GUI extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private Container content;
private JLabel label[] = new JLabel[5];
private JTextField text[] = new JTextField[3];
private JButton button[] = new JButton[4];
private JCheckBox box;
private File file1 = null;
private File file2 = null;
// 密码算子
private int code = 0;
public FileCode_GUI() {
this.setTitle("文件加密器1.0");
content = this.getContentPane();
this.setBounds(300, 200, 300, 210);
this.setLayout(new BorderLayout(10, 10));
// 加入标签
String labstr[] = { "文件加密器", " 源文件", "目标文件", "加密算子", "(0-255)" };
for (int i = 0; i < label.length; i++) {
label[i] = new JLabel(labstr[i]);
label[i].setHorizontalAlignment(SwingConstants.RIGHT);
}
// 文本框
for (int i = 0; i < text.length; i++) {
text[i] = new JTextField(14);
text[i].setText("");
}
// 设置不可编辑
text[0].setEditable(false);
text[1].setEditable(false);
// 按钮
String but[] = { "浏览1", "浏览2", "确定", "清空" };
for (int i = 0; i < button.length; i++) {
button[i] = new JButton(but[i]);
button[i].addActionListener(this);
}
// 复选框
box = new JCheckBox("覆盖源文件");
box.addActionListener(this);
// 第一个标签添加到北面
label[0].setHorizontalAlignment(SwingConstants.CENTER);
content.add(label[0], BorderLayout.NORTH);
// 添加中间组件
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEtchedBorder());
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
panel.add(label[1]);
panel.add(text[0]);
panel.add(button[0]);
panel.add(label[2]);
panel.add(text[1]);
panel.add(button[1]);
panel.add(label[3]);
panel.add(text[2]);
panel.add(label[4]);
panel.add(box);
content.add(panel, BorderLayout.CENTER);
// 添加南面组件
JPanel p3 = new JPanel();
p3.add(button[2]);
p3.add(button[3]);
content.add(p3, BorderLayout.SOUTH);
}
public void showMe() {
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
}
/**
* @param args
*/
public static void main(String[] args) {
new FileCode_GUI().showMe();
}
@Override
public void actionPerformed(ActionEvent arg0) {
if (arg0.getActionCommand().trim().equals("浏览1")) {
JFileChooser jf = new JFileChooser();
jf.showOpenDialog(this);
file1 = jf.getSelectedFile();
text[0].setText(file1.getAbsolutePath());
} else if (arg0.getActionCommand().trim().equals("确定")) {
int temp = Integer.parseInt(text[2].getText());
if (temp < 0 || temp > 255) {
JOptionPane.showMessageDialog(this, "加密算子 输入有误", "提示信息",
JOptionPane.ERROR_MESSAGE);
} else {
code = temp;
if (box.isSelected()) {
// 如果复选框为选中状态
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(file1, "rw");
int a;
while ((a = raf.read()) != -1) {
raf.seek(raf.getFilePointer() - 1);
raf.write(a ^ code);
}
JOptionPane.showMessageDialog(this, "文件加密成功!!!",
"提示信息", JOptionPane.INFORMATION_MESSAGE);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭
if (raf != null) {
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} else {
// 不选中状态
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
while (bis.available() > 0) {
bos.write(bis.read() ^ code);
}
JOptionPane.showMessageDialog(this, "文件加密成功!!!",
"提示信息", JOptionPane.INFORMATION_MESSAGE);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
} else if (arg0.getActionCommand().trim().equals("清空")) {
for (int i = 0; i < text.length; i++) {
text[i].setText("");
}
} else if (arg0.getActionCommand().trim().equals("浏览2")) {
JFileChooser jf = new JFileChooser();
jf.showOpenDialog(this);
file2 = jf.getSelectedFile();
text[1].setText(file2.getAbsolutePath());
} else if (arg0.getActionCommand().equals("覆盖源文件")) {
if (box.isSelected()) {
text[1].setText(file1.getAbsolutePath());
} else {
text[1].setText("");
}
}
}
}
一个简单的文件加密器(java)
3星 · 超过75%的资源 需积分: 9 121 浏览量
2009-07-20
22:25:33
上传
评论
收藏 2KB RAR 举报
monkeyking1987
- 粉丝: 305
- 资源: 28
最新资源
- 使用C语言与python分别实现文件的读写功能
- zephyr sdk package 2
- zephyr sdk package 1
- optimization.ipynb
- 数据库标识码BSM重排序工具、重构标识码工具
- 基于C语言的校园导航系统报告.doc
- __init__.py
- tensorflow-gpu-2.6.5-cp39-cp39-manylinux2010-x86-64.whl
- tensorflow-rocm-2.13.1.600-cp38-cp38-manylinux2014-x86-64.whl
- tensorflow-rocm-2.13.0.570-cp311-cp311-manylinux2014-x86-64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈