packageJframe;importjava.awt.GridLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.io.Reader;importjava.io.UnsupportedEncodingException;importjava.nio.charset.StandardCharsets;importjavax.crypto.Cipher;importjavax.crypto.spec.IvParameterSpec;importjavax.crypto.spec.SecretKeySpec;importjavax.swing.JButton;importjavax.swing.JFileChooser;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JOptionPane;importjavax.swing.JPanel;importjavax.swing.JTextField;public class Jframe1 extends JFrame implementsActionListener{privateFile lockfile;privateFile unlockfile;privateString temp1;

JButton jb1,jb2,jb3,jb4,jb5;

JLabel jlb1,jlb2,jlb3;

JPanel jp1,jp2,jp3,jp4,jp5;

JTextField jtf1,jtf2,jtf3,jtf4;publicJframe1(){

jb1=new JButton("加密文件");

jb2=new JButton("解密文件");

jb3=new JButton("退出");

jb4=new JButton("...");

jb5=new JButton("...");

jb1.addActionListener(this);

jb2.addActionListener(this);

jb3.addActionListener(this);

jb4.addActionListener(this);

jb5.addActionListener(this);

jlb1=new JLabel("选择加密文件:");

jlb2=new JLabel("口令");

jlb3=new JLabel("选择解密文件:");

jtf1=new JTextField(10);

jtf2=new JTextField(10);

jtf2.setText("请输入16位Key");

jtf3=new JTextField(10);

jp1=newJPanel();

jp2=newJPanel();

jp3=newJPanel();

jp5=newJPanel();

jp1.add(jlb1);

jp1.add(jtf1);

jp1.add(jb4);

jp2.add(jlb2);

jp2.add(jtf2);

jp3.add(jlb3);

jp3.add(jtf3);

jp3.add(jb5);

jp5.add(jb1);

jp5.add(jb2);

jp5.add(jb3);this.add(jp1);this.add(jp2);this.add(jp3);this.add(jp5);this.setLayout(new GridLayout(5,1));this.setTitle("文件AES加解密程序");this.setBounds(300, 200, 400, 320);this.setResizable(false);this.setVisible(true);this.setDefaultCloseOperation(EXIT_ON_CLOSE);

}public static voidmain(String[] args) {//TODO Auto-generated method stub

newJframe1();

}

@Overridepublic voidactionPerformed(ActionEvent e) {//TODO Auto-generated method stub//监听按钮

if(e.getActionCommand()=="退出"){

System.exit(0);

}if(e.getActionCommand()=="加密文件"){try{this.lock();

}catch(FileNotFoundException e1) {//TODO Auto-generated catch block

e1.printStackTrace();

}catch(IOException e1) {//TODO Auto-generated catch block

e1.printStackTrace();

}catch(Exception e1) {//TODO Auto-generated catch block

e1.printStackTrace();

}

}if(e.getActionCommand()=="解密文件"){try{this.unlock();

}catch(IOException e1) {//TODO Auto-generated catch block

e1.printStackTrace();

}catch(Exception e1) {//TODO Auto-generated catch block

e1.printStackTrace();

}

}if(e.getSource()==jb4){

JFileChooser jfc=newJFileChooser();

jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );

jfc.showDialog(new JLabel(), "选择");

lockfile=jfc.getSelectedFile();

jtf1.setText(lockfile.getName());

}if(e.getSource()==jb5){

JFileChooser jfc=newJFileChooser();

jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );

jfc.showDialog(new JLabel(), "选择");

unlockfile=jfc.getSelectedFile();

jtf3.setText(unlockfile.getName());

}

}//加密

public void lock() throwsException{int buffersize=1024,length;

String path,addpath=".e";char[] buffer=new char[buffersize];

StringBuffer out=newStringBuffer();

InputStream is= newFileInputStream(lockfile.getPath().toString());

Reader in= new InputStreamReader(is, "UTF-8");

String context;for(; ; ) {int rsz = in.read(buffer, 0, buffer.length);if (rsz < 0)break;

out.append(buffer,0, rsz);

}

context=out.toString();

temp1=AESUtil.encrypt(context, jtf2.getText().toString());

path=lockfile.getAbsolutePath();

length=path.length();

addpath=path.substring(0, length)+addpath;

File fout=newFile(addpath);

FileOutputStream fos=newFileOutputStream(fout);

BufferedWriter bw=new BufferedWriter(newOutputStreamWriter(fos));

bw.write(temp1);

JOptionPane.showMessageDialog(this, "加密成功");

bw.close();

}//解密

public void unlock() throwsException{int buffersize=1024,length;

String path,addpath=".e";char[] buffer=new char[buffersize];

StringBuffer out=newStringBuffer();

InputStream is= newFileInputStream(unlockfile.getPath().toString());

Reader in= new InputStreamReader(is, "UTF-8");

String context;for(; ; ) {int rsz = in.read(buffer, 0, buffer.length);if (rsz < 0)break;

out.append(buffer,0, rsz);

}

context=out.toString();

temp1=AESUtil.decrypt(context, jtf2.getText().toString());

path=unlockfile.getAbsolutePath();

length=path.length();

addpath=path.substring(0, length-2);

File fout=newFile(addpath);

FileOutputStream fos=newFileOutputStream(fout);

BufferedWriter bw=new BufferedWriter(newOutputStreamWriter(fos));

bw.write(temp1);

JOptionPane.showMessageDialog(this, "解密成功");

bw.close();

}

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐