问题
编程实现对一个文本文件进行加密,保存成另一个文件,然后对此文件解密,查看解密后结果。
代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test extends JFrame implements ActionListener
{
private final JTextField t_encrypt=new JTextField(30);
private final JTextField t_decrypt=new JTextField(30);
public Test()
{
super("文件破译器");
setBounds(200,200,600,150);
setLayout(new FlowLayout());
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JButton b_decrypt=new JButton("加密");
JLabel l_encrypt=new JLabel("输入准备加密的文件地址:");
JLabel l_decrypt=new JLabel("文件加密完成:");
add(l_encrypt);
add(t_encrypt);
add(b_decrypt);
add(l_decrypt);
add(t_decrypt);
setVisible(true);
b_decrypt.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e)
{
String fi_name=t_encrypt.getText();
String fo_name="D:\\2.txt";
try {
FileInputStream fi=new FileInputStream(fi_name);
byte []b=new byte[1024*24];//确保输入字节能全部加密
int key=fi.read(b);
for(int i=0;i<key;i++)
{
b[i]+=1;
}
FileOutputStream fo=new FileOutputStream(fo_name);
fo.write(b);
fo.flush();
fo.close();
t_decrypt.setText(fo_name);
}
catch (IOException fileNotFoundException)
{
fileNotFoundException.printStackTrace();
}
}
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null,"活动代号:泰达米尔");
new Test();
}
}
画蛇添足,其实完全没必要,try里面的代码其实就够了