import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import javax.crypto.EncryptedPrivateKeyInfo;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
class Encrypter{
public Encrypter(int code2, JFileChooser f, boolean selected) {
// TODO Auto-generated constructor stub
}
public void encode() {
// TODO Auto-generated method stub
}
}
public class where extends JFrame {
JLabel l1,l2;
JFileChooser f;
JTextField t;
JButton b1,b2;
JCheckBox c;
JPanel p1,p2;
int code;
boolean rewrite;
File sourceFile;
FileInputStream fin;
FileOutputStream fout;
String sourceFileName;
String obJFileName;
where(){
l1=new JLabel("选择加密文件");
l2=new JLabel("选择加密/解密算子");
t=new JTextField(10);
b1=new JButton("确定");
b2=new JButton("取消");
c=new JCheckBox("覆盖文件");
f=new JFileChooser();
p1=new JPanel();
p2=new JPanel();
setLayout(new BorderLayout());
add(p1,BorderLayout.CENTER);
add(p2,BorderLayout.SOUTH);
f.setControlButtonsAreShown(false);
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p1.add(l1);
p1.add(f);
p1.add(l2);
p1.add(t);
p1.add(c);
p2.add(b1);
p2.add(b2);
this.setSize(700,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);//固定窗口
this.setVisible(true);
setEvent();
}
private void setEvent() {
// TODO Auto-generated method stub
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
File f1=f.getSelectedFile();
int code=Integer.parseInt(c.getText());
new Encrypter(code,f,c.isSelected()).encode();
final JDialog jb=new JDialog(where.this,"加密成功!");
jb.add(new JLabel("文件加密成功"),BorderLayout.NORTH );
JButton jbok;
jb.add(jbok=new JButton("确定"),BorderLayout.SOUTH);
jbok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
jb.dispose();
}
});
jb.setLocationRelativeTo(where.this);
jb.setSize(300,200);
jb.setVisible(true);
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
});
}
public void encode(){
if(rewrite){
RandomAccessFile fra=null;
try{
fra=new RandomAccessFile(sourceFile, "rw");
int temp;
while((temp=fra.read())!=-1){
fra.seek(fra.getFilePointer()-1);
fra.write(temp^code);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(fra!=null)
try{
fra.close();
}catch(Exception e){
}
}
}else{
FileInputStream fis=null;
FileOutputStream fos=null;
try{
fis=new FileInputStream(sourceFile);
fos=new FileOutputStream(obJFileName);
while(fis.available()>0){
fos.write(fis.read()^code);
}
fos.flush();
}catch(Exception e){
e.printStackTrace();
}finally{
if(fis!=null)
try{
fis.close();
}catch(Exception e){
}
if(fos!=null)
try{
fos.close();
}catch(Exception e){
}
}
}
}
public static void main(String[] args) {
new where();
}
}
呦呦米
相关分类