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

📄 mainframe.java

📁 用差分方法攻击加密文件
💻 JAVA
字号:
package encrypt;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;  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();  private JLabel jLabel6 = new JLabel();  private JTextField keyTextField = new JTextField();  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(47, 89, 71, 31));    contentPane.setLayout(null);    this.setSize(new Dimension(400, 350));    this.setTitle("Single encrypt");    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 so simple that it is just for the difference-attack " +    "test.");    jLabel2.setBounds(new Rectangle(8, 20, 390, 18));    jLabel3.setBounds(new Rectangle(26, 156, 96, 31));    jLabel3.setText("Destination file :");    startBtn.setBounds(new Rectangle(25, 226, 92, 71));    startBtn.setText("Start !");    startBtn.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        startBtn_actionPerformed(e);      }    });    sourceTextField.setBounds(new Rectangle(123, 94, 236, 22));    destiTextField.setBounds(new Rectangle(122, 159, 236, 22));    srcBtn.setBounds(new Rectangle(126, 122, 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(125, 190, 89, 29));    jLabel4.setText("Welcome to have a tryout on another");    jLabel4.setBounds(new Rectangle(134, 245, 207, 19));    jLabel5.setText("program named \"difference_attack\". ");    jLabel5.setBounds(new Rectangle(133, 272, 200, 18));    jLabel6.setText("work key :");    jLabel6.setBounds(new Rectangle(58, 62, 62, 18));    keyTextField.setText("222");    keyTextField.setBounds(new Rectangle(122, 59, 236, 22));    contentPane.add(jLabel2, null);    contentPane.add(jLabel6, null);    contentPane.add(keyTextField, null);    contentPane.add(sourceTextField, null);    contentPane.add(srcBtn, null);    contentPane.add(jLabel1, null);    contentPane.add(jLabel3, null);    contentPane.add(destiTextField, null);    contentPane.add(desBtn, 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 b;    byte by[]=new byte[buffer];    try{      key=Integer.parseInt(this.keyTextField.getText());    }    catch (NumberFormatException ex){    }    try{      FileInputStream readFile=new FileInputStream(this.sourceTextField.getText());      FileOutputStream writeFile=new FileOutputStream(this.destiTextField.getText());      b=readFile.read(by,0,buffer);        while(b>0){          for(int i=0;i<b;i++){            by[i]=encrypt(by[i]);          }          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);    }  }  byte encrypt(byte b){    int by;    //key=91;System.out.print(b+"--->");    by=b^key;//System.out.print(by+":");    by=sBox(by);//System.out.print(by+" ");    //key=107;    //by=b^key; System.out.print(by+":");    //by=sBox(by);System.out.println(by);    return (byte)by;  }  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 + -