⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainframe.java

📁 简单的基于DES的加密算法及雪崩效应分析。请仔细阅读内附的说明文件。
💻 JAVA
字号:
package sdespassword;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author unascribed * @version 1.0 */public class MainFrame extends JFrame {//********************************************************  JFileChooser fd_load,fd_save;  final int buffer=10000;  byte key[]={0,0};  int p10[]={3,5,2,7,4,10,1,9,8,6};  int p8[] = {6,3,7,4,8,5,10,9 };  int IP[] = { 2,6,3,1,4,8,5,7 };  int IP_con[]={4,1,3,5,7,2,8,6 };  int EPright[]={8,5,6,7,6,7,8,5};  int p4right[] = {0,0,0,0,6,8,7,5};  byte s0[][]={  {1,0,3,2},  {3,2,1,0},  {0,2,1,3},  {3,1,3,2}  };  byte s1[][]={  {0,1,2,3},  {2,0,1,3},  {3,0,1,0},  {2,1,0,3}  };//********************************************************  private JPanel contentPane;  private JLabel jLabel1 = new JLabel();  private JComboBox EDComboBox = new JComboBox();  private JLabel jLabel2 = new JLabel();  private JLabel jLabel3 = new JLabel();  private JTextField keyTextField = new JTextField();  private JTextField sourceTextField = new JTextField();  private JTextField destiTextField = new JTextField();  private JLabel jLabel4 = new JLabel();  private JButton startBtn = new JButton();  private JButton sourceBtn = new JButton();  private JButton destiBtn = new JButton();  public MainFrame() {    enableEvents(AWTEvent.WINDOW_EVENT_MASK);    try {      jbInit();    }    catch(Exception e) {      e.printStackTrace();    }  }  private void jbInit() throws Exception  {    contentPane = (JPanel) this.getContentPane();    jLabel1.setToolTipText("");    jLabel1.setText("Encrypt/decrypt : ");    jLabel1.setBounds(new Rectangle(22, 18, 96, 19));    contentPane.setLayout(null);    this.setSize(new Dimension(400, 314));    this.setTitle("SDES");    EDComboBox.setToolTipText("");    EDComboBox.setBounds(new Rectangle(121, 16, 141, 22));    jLabel2.setText("Source file :");    jLabel2.setBounds(new Rectangle(22, 96, 96, 18));    jLabel3.setText("Key :");    jLabel3.setBounds(new Rectangle(22, 57, 96, 18));    keyTextField.setText("291");    keyTextField.setBounds(new Rectangle(121, 55, 141, 22));    sourceTextField.setBounds(new Rectangle(121, 94, 259, 22));    destiTextField.setToolTipText("");    destiTextField.setBounds(new Rectangle(121, 150, 261, 22));    jLabel4.setBounds(new Rectangle(22, 152, 96, 18));    jLabel4.setText("Destination file :");    startBtn.setBounds(new Rectangle(21, 209, 82, 59));    startBtn.setText("Start !");    startBtn.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        startBtn_actionPerformed(e);      }    });    sourceBtn.setBounds(new Rectangle(121, 122, 103, 22));    sourceBtn.setText("Browse...");    sourceBtn.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        sourceBtn_actionPerformed(e);      }    });    destiBtn.setText("Browse...");    destiBtn.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        destiBtn_actionPerformed(e);      }    });    destiBtn.setBounds(new Rectangle(121, 181, 104, 22));    contentPane.add(jLabel1, null);    contentPane.add(EDComboBox, null);    contentPane.add(jLabel3, null);    contentPane.add(keyTextField, null);    contentPane.add(jLabel2, null);    contentPane.add(sourceTextField, null);    contentPane.add(sourceBtn, null);    contentPane.add(destiTextField, null);    contentPane.add(jLabel4, null);    contentPane.add(destiBtn, null);    contentPane.add(startBtn, null);//*************************************************************    //*********************************************************    this.EDComboBox.addItem("Encrypt");    this.EDComboBox.addItem("Decrypt");    this.EDComboBox.setSelectedIndex(0);    fd_load=new JFileChooser();    fd_load.setFileFilter(new Filter(".txt"));    fd_load.setVisible(false);    fd_save=new JFileChooser();    fd_save.setFileFilter(new Filter(".txt"));    fd_save.setVisible(false);    //*********************************************************//*************************************************************  }  protected void processWindowEvent(WindowEvent e) {    super.processWindowEvent(e);    if (e.getID() == WindowEvent.WINDOW_CLOSING) {      System.exit(0);    }  }  void sourceBtn_actionPerformed(ActionEvent e) {    fd_load.setVisible(true);      int result=fd_load.showOpenDialog(this);      if(result==fd_load.APPROVE_OPTION){        this.sourceTextField.setText(fd_load.getSelectedFile().getPath());        String parent=fd_load.getSelectedFile().getParent();        String name=fd_load.getSelectedFile().getName();        if(this.EDComboBox.getSelectedIndex()==0)          this.destiTextField.setText(parent+"\\en_"+name);        else          this.destiTextField.setText(parent+"\\de_"+name);      }      else fd_load.setVisible(false);  }//**********************************************************  //********************************************************  void destiBtn_actionPerformed(ActionEvent e) {    fd_save.setVisible(true);    int result=fd_save.showOpenDialog(this);    if(result==fd_save.APPROVE_OPTION){      this.sourceTextField.setText(fd_save.getSelectedFile().getPath());    }    else fd_load.setVisible(false);  }  void startBtn_actionPerformed(ActionEvent e) {    int b;    byte by[]=new byte[buffer];    generateKey(this.keyTextField.getText(),key);    try{      FileInputStream readFile=new FileInputStream(this.sourceTextField.getText());      FileOutputStream writeFile=new FileOutputStream(this.destiTextField.getText());      b=readFile.read(by,0,buffer);      if(this.EDComboBox.getSelectedIndex()==0){        byte pre=34;        //pre=encrypt(pre);        while(b>0){          for(int i=0;i<b;i++){            by[i]=encrypt((byte)(by[i]^pre));            pre=by[i];          }          writeFile.write(by,0,b);          b=readFile.read(by,0,buffer);        }      }      else{        byte pre=34;byte temp;        while(b>0){          for(int i=0;i<b;i++){            temp=by[i];            by[i]=(byte)(decrypt(by[i])^pre);            pre=temp;          }          writeFile.write(by,0,b);          b=readFile.read(by,0,buffer);        }      }      JOptionPane.showMessageDialog(this,"Done!","finish",JOptionPane.INFORMATION_MESSAGE);      readFile.close();writeFile.close();    }catch(IOException ex){      JOptionPane.showMessageDialog(this,"File Error!check the path..","Error",JOptionPane.ERROR_MESSAGE);    }  } //***********************************************************  void generateKey(String str,byte []k){    int key=0;    try{      key=Integer.parseInt(str);      key=exchange_10(p10,key);      int keyHigh=key&0x3e0;      int keyLow=key&0x1f;      key=moveOne_5(keyHigh,keyLow);      k[0]=(byte)(exchange_10to8(p8,key));      keyHigh=key&0x3e0;      keyLow=key&0x1f;      key=moveOne_5(keyHigh,keyLow);      keyHigh=key&0x3e0;      keyLow=key&0x1f;      key=moveOne_5(keyHigh,keyLow);      k[1]=(byte)(exchange_10to8(p8,key));    //System.out.println(k[1]);      }catch (NumberFormatException ex) {}  }//************************************************************  int moveOne_5(int keyHigh,int keyLow){      keyHigh<<=1;      if((keyHigh&0x400) !=0)        keyHigh|=0x20;      keyLow<<=1;      if((keyLow&0x20) !=0)        keyLow|=1;      return (keyHigh&0x3e0)|(keyLow&0x1f);  }  int exp[]={0,0x80,0x40,0x20,0x10,8,4,2,1};  int exp_10[]={0,0x200,0x100,0x80,0x40,0x20,0x10,8,4,2,1};  byte exchange(int []p,byte a){    int b=0;    int temp;    for(int i=0;i<p.length;i++){      temp=a&exp[p[i]];      if(temp!=0){        b=b|exp[i+1];      }    }    return (byte)b;  }  int exchange_10(int []p,int a){    int b=0;    int temp;    for(int i=0;i<p.length;i++){      temp=a&exp_10[p[i]];      if(temp!=0){        b=b|exp_10[i+1];      }    }    return b;  }  byte exchange_10to8(int []p,int a){    int b=0;    int temp;    for(int i=0;i<p.length;i++){      temp=a&exp_10[p[i]];      if(temp!=0){        b=b|exp[i+1];      }    }    return (byte)b;  }  byte sBox(byte a){    int result=0;    //byte temp=a;    int x2=a>>>4;int x1=a>>>6;int x=x1&2|(x2&1);    //temp=a;    int y=(a>>>5)&3;    result|=(s0[x][y]<<2);    x1=a>>>2;x=x1&2|a&1;    //temp=a;    y=(a>>>1)&3;    result|=s1[x][y];    return (byte)result;  }//****************************************************************  byte encrypt(byte by){   //for(int k=1; k<127;k*=2){    //by=(byte)k;    //by=0;    by=exchange(IP,by);    int high=by&0xf0;int low=by&0x0f;    by=exchange(EPright,by);    by=(byte)(by^key[0]);    by=sBox(by);    by=exchange(p4right,by);    by=(byte)(by^(high>>>4));    by=(byte)((low<<4)|by);    high=by&0xf0; low=by&0x0f;    by=exchange(EPright,by);    by=(byte)(by^key[1]);    by=sBox(by);    by=exchange(p4right,by);     by=(byte)((by<<4)^high);     by=(byte)(by|low);     by=exchange(IP_con,by);     //System.out.println(by&0xff);    return by;  }  byte decrypt(byte by){    by=exchange(IP,by);    int high=by&0xf0;int low=by&0x0f;    by=exchange(EPright,by);    by=(byte)(by^key[1]);    by=sBox(by);    by=exchange(p4right,by);    by=(byte)(by^(high>>>4));    by=(byte)((low<<4)|by);    high=by&0xf0; low=by&0x0f;    by=exchange(EPright,by);    by=(byte)(by^key[0]);    by=sBox(by);    by=exchange(p4right,by);     by=(byte)((by<<4)^high);     by=(byte)(by|low);     by=exchange(IP_con,by);    return by;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -