?? mainframe.java
字號(hào):
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=150; 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 keyLabel = 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)); keyLabel.setFont(new java.awt.Font("Dialog", 1, 16)); keyLabel.setForeground(Color.red); keyLabel.setText("null"); keyLabel.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(keyLabel, 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 E; int EE;//E* int EP;//E' int b; int keyArray[]=new int[256]; for(int i=0;i<count;i++){//begin analysis int random=(int)(Math.random()*buffer)%srcLength; E=src[random]; int random2; do{ random2=(int)(Math.random()*buffer)%srcLength; }while(random2==random); EE=src[random2]; EP=E^EE; b=en[random]^en[random2]; b=b&15; for(int j=0;j<256;j++){ int jj=j^EP; if((sBox(j)^sBox(jj))==b) keyArray[j^E]++; } } String keyStr=""; for(int i=0;i<256;i++){ if(keyArray[i]==count) keyStr+=i+" "; } this.keyLabel.setText(keyStr); srcFile.close();enFile.close(); }catch(IOException ex){ JOptionPane.showMessageDialog(this,"File Error!check the path..","Error",JOptionPane.ERROR_MESSAGE); } } 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; }}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -