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

📄 mainframe.java~1~

📁 用差分方法攻击加密文件
💻 JAVA~1~
字号:
package difference_attack;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;public class MainFrame extends JFrame {  JFileChooser fd_load,fd_load2;  final int buffer=10000;  final int count=250;  int key;  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 JLabel jLabel2 = new JLabel();  private JLabel jLabel3 = new JLabel();  private JButton startBtn = new JButton();  private JTextField sourceTextField = new JTextField();  private JTextField destiTextField = new JTextField();  private JButton srcBtn = new JButton();  private JButton desBtn = new JButton();  private JLabel jLabel4 = new JLabel();  private JLabel jLabel5 = new JLabel();  public MainFrame() {    enableEvents(AWTEvent.WINDOW_EVENT_MASK);    try {      jbInit();    }    catch(Exception e) {      e.printStackTrace();    }  }  private void jbInit() throws Exception  {    contentPane = (JPanel) this.getContentPane();    jLabel1.setText("Source file :");    jLabel1.setBounds(new Rectangle(36, 42, 71, 31));    contentPane.setLayout(null);    this.setSize(new Dimension(500, 350));    this.setTitle("Difference attack");    fd_load=new JFileChooser();    fd_load.setFileFilter(new Filter(".txt"));    fd_load.setVisible(false);    fd_load2=new JFileChooser();    fd_load2.setFileFilter(new Filter(".txt"));    fd_load2.setVisible(false);    jLabel2.setText("This program is based on a pair of files processed by the program " +    " \"simple encrypt\".");    jLabel2.setBounds(new Rectangle(8, 20, 467, 18));    jLabel3.setBounds(new Rectangle(21, 109, 89, 31));    jLabel3.setText("encrypted file :");    startBtn.setBounds(new Rectangle(21, 191, 92, 71));    startBtn.setText("Start !");    startBtn.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        startBtn_actionPerformed(e);      }    });    sourceTextField.setBounds(new Rectangle(112, 47, 355, 22));    destiTextField.setBounds(new Rectangle(111, 112, 356, 22));    srcBtn.setBounds(new Rectangle(115, 75, 89, 29));    srcBtn.setText("Browse...");    srcBtn.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        srcBtn_actionPerformed(e);      }    });    desBtn.setText("Browse...");    desBtn.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        desBtn_actionPerformed(e);      }    });    desBtn.setBounds(new Rectangle(114, 143, 89, 29));    jLabel4.setText("The work key is: ");    jLabel4.setBounds(new Rectangle(135, 214, 98, 19));    jLabel5.setFont(new java.awt.Font("Dialog", 1, 16));    jLabel5.setForeground(Color.red);    jLabel5.setText("null");    jLabel5.setBounds(new Rectangle(239, 210, 82, 27));    contentPane.add(jLabel2, null);    contentPane.add(jLabel1, null);    contentPane.add(sourceTextField, null);    contentPane.add(srcBtn, null);    contentPane.add(destiTextField, null);    contentPane.add(desBtn, null);    contentPane.add(jLabel3, null);    contentPane.add(startBtn, null);    contentPane.add(jLabel4, null);    contentPane.add(jLabel5, null);  }  protected void processWindowEvent(WindowEvent e) {    super.processWindowEvent(e);    if (e.getID() == WindowEvent.WINDOW_CLOSING) {      System.exit(0);    }  }  void srcBtn_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();          this.destiTextField.setText(parent+"\\en_"+name);      }      else fd_load.setVisible(false);  }  void desBtn_actionPerformed(ActionEvent e) {    fd_load2.setVisible(true);    int result=fd_load2.showOpenDialog(this);    if(result==fd_load2.APPROVE_OPTION){      this.sourceTextField.setText(fd_load2.getSelectedFile().getPath());    }    else fd_load.setVisible(false);  }  void startBtn_actionPerformed(ActionEvent e) {    int srcLength,enLength;    byte src[]=new byte[buffer];    byte en[]=new byte[buffer];    try{      FileInputStream srcFile=new FileInputStream(this.sourceTextField.getText());      FileInputStream enFile=new FileInputStream(this.destiTextField.getText());      srcLength=srcFile.read(src,0,buffer);      enLength=enFile.read(en,0,buffer);      if(srcLength!=enLength){        JOptionPane.showMessageDialog(this,"File Error!unmatched pair","Error",JOptionPane.ERROR_MESSAGE);        return;      }      int Eh,El;      int EEh,EEl;//E*      int EPh,EPl;//E'      int b,bh,bl;      int keyArrayHigh[]=new int[16];      int keyArrayLow[]=new int[16];      for(int i=0;i<count;i++){//begin analysis        int random=(int)(Math.random()*buffer)%srcLength;        Eh=(src[random]>>>4)&15;        El=src[random]&15;        int random2;        do{            random2=(int)(Math.random()*buffer)%srcLength;        }while(random2==random);        EEh=(src[random2]>>>4)&15;        EEl=src[random2]&15;        EPh=Eh^EEh;        EPl=El^EEl;        b=en[random]^en[random2];        bh=(b>>>2)&3;        bl=b&3;        for(int j=0;j<16;j++){         int jj=j^EPh;         int sj=s0Box(j);         int sjj=s0Box(jj);         if((sj^sjj)==bh)           keyArrayHigh[j^Eh]++;        }        for(int j=0;j<16;j++){          int jj=j^EPl;          if((s1Box(j)^s1Box(jj))==bl)            keyArrayLow[j^El]++;        }      }      for(int i=0;i<16;i++){        if(keyArrayHigh[i]==count)          System.out.println(i);      }      for(int i=0;i<16;i++){        if(keyArrayLow[i]==count)          System.out.println(i);      }      srcFile.close();enFile.close();    }catch(IOException ex){      JOptionPane.showMessageDialog(this,"File Error!check the path..","Error",JOptionPane.ERROR_MESSAGE);    }  }  int s0Box(int a){    int result=0;    int x1=a>>>2;int x=x1&2|a&1;    int y=(a>>>1)&3;    result|=s0[x][y];    return result;  }  int s1Box(int a){    int result=0;    int x1=a>>>2;int x=x1&2|a&1;    int y=(a>>>1)&3;    result|=s1[x][y];    return result;  }  /*int sBox(int a){    int result=0;    int x2=a>>>4;int x1=a>>>6;int x=x1&2|(x2&1);    int y=(a>>>5)&3;    result|=(s0[x][y]<<2);    x1=a>>>2;x=x1&2|a&1;    y=(a>>>1)&3;    result|=s1[x][y];    return result;  }*/}

⌨️ 快捷键说明

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