`
sit379vt
  • 浏览: 14398 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

爪哇版的加解密小程序

阅读更多

爪哇版的加解密小程序
2012年01月16日
  //要先安装JDK  ,JAR包我也有
  import java.io.*;
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  public class Main {
  private JFrame f;  //主窗口
  private Button b1,b2,babout_me;  //选择按钮
  private Label l1,l2,lp2;  //标签
  private Panel p1,p2,p3,p2p;   //面板
  private FileDialog fd;   //文件打开
  private int size=21; //密码长度
  //主界面
  Main(){
  this.f =new JFrame("文件加解密程序");
  f.setBounds(450,150,500,200);
  f.setVisible(true);
  f.setLayout(new BorderLayout());
  p1=new Panel(new FlowLayout());
  p2=new Panel(new BorderLayout());
  p2p=new Panel();
  p3=new Panel();
  b1=new Button("加密文件");
  b2=new Button("解密文件");
  babout_me =new Button("关于本软件");
  l1=new Label("感谢使用加解密程序");
  l2=new Label("by 水往高处流");
  lp2=new Label(" ");
  p1.add(l1);
  p2.add(p2p,"Center");
  p2.add(lp2,"South");
  p2p.add(b1);
  p2p.add(b2);
  p3.add(babout_me);
  p3.add(l2); 
  f.add(p1,"North");
  f.add(p2,"Center");
  f.add(p3,"South");
  //加密
  b1.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent a){
  lp2.setText(" ");
  JM();
  }
  });
  //解密
  b2.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent a){
  lp2.setText(" ");
  JM(0);
  }
  });
  //本软件的一些情况
  babout_me.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent a){
  Tips();
  }
  });
  //添加退出事件
  f.addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent e){
  System.exit(0);
  }
  });
  }
  //关于本软件
  public void Tips(){
  JFrame f =new JFrame("关于本软件");
  String line="\n-------------------------------\n";
  TextArea t=new TextArea("文件后缀大概如下:\n" +line+
  " 1.文本类:.txt,.doc,.pdf,.html,.accdb,.cpp,.java,.ppt\n" +
  " 2.多媒体类:.mp3,.mp4,.gif,.jpg,.jpeg,.mpg,.3gp,aac,.bat,.avi,.flv\n" +
  " 3.压缩包类:.7z,.rar,.zip,.cab\n" +line +
  "此软件会不定时修改,衷心感谢您的使用!\n"+
  "本软件有不足之处欢迎给我E-main:373422113@qq.com");
  t.setEditable(false); //设置为只读
  f.add(t);
  f.setVisible(true);
  f.setBounds(550,250,450,200);
  }
  //加密算法
  public int JM(){
  fd =new FileDialog(f,"打开需要加密文件",FileDialog.LOAD);
  fd.setVisible(true);
  String fname=fd.getFile(); //得到文件名
  String fdir=fd.getDirectory(); //得到文件所在目录 
  String str=fdir + "\\" + fname;// 文件绝对路径
  if(fname == null){
  lp2.setText("未选择文件!");
  return 0;
  }
  lp2.setText("即将加密文件: "+fname+" 文件大小为:"+new File(str).length()+"字节");    
  String strpw=JOptionPane.showInputDialog(null,"请输入密码,在"+(size-1)+"个字符以内","此密码需牢记",1);
  char[] password =strpw.toCharArray();  //密码
  if(isArrayOutOfBounds(password)){ //检查密码长度
  lp2.setText("error: 密码长度超出范围!");
  return 0;
  }
  try { 
  FileInputStream fin=new FileInputStream(str);//打开文件输入流
  BufferedInputStream bufin = new BufferedInputStream(fin,4096); //使用缓冲流
  DataInputStream din = new DataInputStream(bufin);
  String out_name =JOptionPane.showInputDialog(null,"请输入加密后的文件名");
  //判断是否输入文件名
  if(!isFile_name(out_name)){
  lp2.setText("error:请输入文件名!");
  return 0;
  }
  out_name += ".jm"; //加入默认后缀
  FileOutputStream fout=new FileOutputStream(fdir+out_name); //打开文件输出流
  BufferedOutputStream bufout = new BufferedOutputStream(fout,4096);//使用缓冲流
  DataOutputStream dout = new DataOutputStream(bufout);  
  lp2.setText("加密文件"+fname+"中,所用时间可能会有点长,请耐心等待.....");
  long time1 = System.currentTimeMillis();//开始计时
  int i = runJM(din,dout,password);  //调用函数
  long time2 = System.currentTimeMillis();//结束计时
  //关闭流
  fin.close();
  din.close();
  fout.close();
  dout.close();
  i *=4;//总字节数(1 int =4 byte)
  time2 -= time1; //所用时间
  lp2.setText("加密了"+i+"个字节,平均加密速度:"+(i/time2)+"字节/秒"); //完成提示 
  String strSucce="文件 '"+fname+"' 加密成功,加密后的文件是 '"+fdir+"' 目录下的'"+out_name+"',总共用时:"+time2+"毫秒";
  JOptionPane.showMessageDialog(null,strSucce,"加密文件成功",2);
  }catch(FileNotFoundException e){lp2.setText("error:file not found!");e.printStackTrace();}
  catch(IOException e){lp2.setText("error:input or output fault!");e.printStackTrace();}
  catch(NullPointerException e){lp2.setText("error:password not input!");e.printStackTrace();}
  catch(Exception e){lp2.setText("unknown error!")/*隐藏提示*/;e.printStackTrace();}
  return 1;
  }
  //解密算法
  public int JM(int n){
  fd =new FileDialog(f,"打开解密文件",FileDialog.LOAD);
  fd.setVisible(true);
  fd.setFilenameFilter(null);
  String fname=fd.getFile();
  String fdir=fd.getDirectory();
  if(fname == null){
  lp2.setText("未选择文件!");
  return 0;
  }
  String str=fdir + "\\" + fname;// 文件绝对路径
  lp2.setText("准备为您解密文件: "+fname+" 文件大小为:"+new File(str).length()+"字节");
  try {  
  String strpw=JOptionPane.showInputDialog(null,"请正确输入加密时的密码","输入密码",1);
  char[] password =strpw.toCharArray();  //密码
  if(isArrayOutOfBounds(password)){  //检测密码范围
  lp2.setText("error: 密码长度超出范围!");
  return 0;
  }
  FileInputStream fin=new FileInputStream(str);//打开文件输入流
  BufferedInputStream bufin = new BufferedInputStream(fin,4096); //使用缓冲流
  DataInputStream din = new DataInputStream(bufin);
  String out_name;
  out_name=JOptionPane.showInputDialog(null,"请输入解密后的完整文件名(包括后缀名,如*.txt,*.doc)");
  //判断是否输入文件名
  if(!isFile_name(out_name)){
  lp2.setText("error:请输入文件名!");
  return 0;
  }
  if(new File(fdir+out_name).exists()){
  lp2.setText("文件:"+out_name+" 已经存在!");
  return 0;
  }
  FileOutputStream fout=new FileOutputStream(fdir+out_name);
  BufferedOutputStream bufout = new BufferedOutputStream(fout,4096);//使用缓冲流
  DataOutputStream dout = new DataOutputStream(bufout); 
  lp2.setText("解密文件"+fname+"中,所用时间可能会有点长,请耐心等待.....");
  long time1 = System.currentTimeMillis();//开始计时
  int i = runJM(din,dout,password);
  long time2 = System.currentTimeMillis();//结束计时
  //关闭流
  fin.close();
  din.close();
  fout.close();
  dout.close();
  i *=4;//总字节数(1 int =4 byte)
  time2 -= time1;
  lp2.setText(" 解密总"+i+"个字节,平均解密速度:"+(i/time2)+"字节/秒"); //完成提示
  String strSucce="文件 '"+fname+"' 解密成功,请留意 '"+fdir+"' 目录下的文件'"+out_name+"',总共用时:"+time2+"毫秒";
  JOptionPane.showMessageDialog(null,strSucce,"文件解密成功",2);
  }catch(FileNotFoundException e){lp2.setText("error:file not found!");e.printStackTrace();}
  catch(IOException e){lp2.setText("error:input or output fault!");e.printStackTrace();}
  catch(NullPointerException e){lp2.setText("error:password not input!");e.printStackTrace();}
  catch(Exception e){lp2.setText("unknown error!")/*隐藏提示*/;e.printStackTrace();}
  return 1;
  }
  //判断密码长度是否超出范围
  public boolean isArrayOutOfBounds(char[] pw){
  if(pw.length>=size)
  return true;  //超出范围
  return false;
  }
  //判断是否输入文件名
  public boolean isFile_name(String out_name){
  if(out_name==null||out_name.equals(""))
  return false;
  return true;
  }
  //加解密进行函数
  public int runJM(DataInputStream din,DataOutputStream dout,char []password) throws IOException{
  int  i=0,read=0;
  int len=password.length;  //得到密码长度
  while((read=din.read())!=-1){
  read=read^password[i%len]; //循环用密码与文件异或加密或解密
  dout.write(read);    
  i++;
  }
  dout.flush(); //清空流
  return i; //返回字节数
  }
  //主函数
  public static void main(String args[]){
  new Main();
  }
  }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics