?? encryption.java~141~
字號:
package cry;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;/** * <p>Title: CRY-DES</p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company: Fontys</p> * @author Ying & Bill * @version 1.0 */public class Encryption extends JFrame implements ActionListener{ JPanel jPanel1 = new JPanel();//panel JMenuBar jMenuBar1 = new JMenuBar();//menubar JMenu jMenu1 = new JMenu();//menubar JMenuItem jMenuItem1 = new JMenuItem(); JMenuItem jMenuItem2 = new JMenuItem(); JMenuItem jMenuItem3 = new JMenuItem(); JLabel jLabel1 = new JLabel();//label JMenu jMenu2 = new JMenu();//menu JTabbedPane jTabbedPane1 = new JTabbedPane(); JPanel pEncry = new JPanel();//encryption panel JLabel lKey = new JLabel();//key label JTextField tKey = new JTextField(); JLabel jLabel2 = new JLabel(); JButton bEncry = new JButton(); JLabel Input = new JLabel(); JLabel Output = new JLabel(); JPanel pDecry = new JPanel(); JLabel jLabel3 = new JLabel(); JLabel jLabel4 = new JLabel(); JButton bDecry = new JButton(); JButton bLoad = new JButton(); JScrollPane jScrollPane1 = new JScrollPane(); JTextArea tEncryinput = new JTextArea(); JScrollPane jScrollPane2 = new JScrollPane(); JTextArea tEncryoutput = new JTextArea(); JScrollPane jScrollPane3 = new JScrollPane(); JScrollPane jScrollPane4 = new JScrollPane(); JTextArea tDecryinput = new JTextArea(); JTextArea tDecryoutput = new JTextArea(); private JFileChooser jfc = new JFileChooser(); String text=""; //byte[] s; public Encryption() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { Encryption encryption1 = new Encryption(); encryption1.setSize(600,700); encryption1.show(); } private void jbInit() throws Exception { //setJMenuBar(jMenuBar1); setTitle("CRY - DES Author: Ying & Bill"); jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); jScrollPane1.setAutoscrolls(true); jScrollPane1.setBounds(new Rectangle(39, 115, 441, 169)); tEncryinput.setText("Here is the input of encryption"); tEncryinput.setLineWrap(true); jScrollPane2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); jScrollPane2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); jScrollPane2.setAutoscrolls(true); jScrollPane2.setBounds(new Rectangle(42, 325, 444, 170)); tEncryoutput.setText("Here is the output of encryption"); tEncryoutput.setLineWrap(true); tEncryoutput.setBackground(Color.BLACK); tEncryoutput.setForeground(Color.GREEN); jScrollPane3.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); jScrollPane3.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); jScrollPane3.setAutoscrolls(true); jScrollPane3.setBounds(new Rectangle(16, 82, 492, 192)); jScrollPane4.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); jScrollPane4.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); jScrollPane4.setAutoscrolls(true); jScrollPane4.setBounds(new Rectangle(15, 322, 496, 170)); tDecryinput.setText("Here is the input of decryption"); tDecryinput.setLineWrap(true); tDecryoutput.setText("Here is the output of encryption"); tDecryoutput.setLineWrap(true); jMenuBar1.add(jMenu1); jMenuBar1.add(jMenu2); jPanel1.setLayout(null); jMenu1.setText("File"); jMenuItem1.setText("Open"); jMenuItem2.setText("Save"); jLabel1.setText("CRY - DES"); jLabel1.setBounds(new Rectangle(246, 10, 180, 32)); jMenu2.setText("About Us"); jMenuItem3.setText("Help"); jTabbedPane1.setBounds(new Rectangle(4, 75, 556, 533)); pEncry.setLayout(null); pEncry.setName("encryption"); lKey.setText("Key"); lKey.setBounds(new Rectangle(15, 45, 47, 29)); tKey.setBounds(new Rectangle(50, 50, 189, 24)); jLabel2.setText("(Please enter a key with 16 bits)"); jLabel2.setBounds(new Rectangle(250, 50, 189, 22)); bEncry.setBounds(new Rectangle(373, 19, 120, 29)); bEncry.setText("Encryption"); Input.setText("Encryption input"); Input.setBounds(new Rectangle(38, 80, 113, 25)); Output.setText("Output text"); Output.setBounds(new Rectangle(38, 297, 92, 27)); pDecry.setLayout(null); jLabel3.setText("Decryption text"); jLabel3.setBounds(new Rectangle(16, 56, 91, 27)); jLabel4.setText("Decryption output"); jLabel4.setBounds(new Rectangle(15, 293, 104, 26)); bDecry.setBounds(new Rectangle(361, 6, 154, 35)); bDecry.setText("Decryption"); bLoad.setBounds(new Rectangle(183, 7, 138, 34)); bLoad.setText("Load"); jMenuBar1.add(jMenu1); jMenuBar1.add(jMenu2); jMenu1.add(jMenuItem1); jMenu1.add(jMenuItem2); jMenu2.add(jMenuItem3); jPanel1.add(jLabel1, null); jPanel1.add(jTabbedPane1, null); jPanel1.add(lKey,null); jPanel1.add(tKey,null); jPanel1.add(jLabel2,null); //pEncry.add(tKey, null); //pEncry.add(lKey, null); pEncry.add(bEncry, null); //pEncry.add(jLabel2, null); pEncry.add(Output, null); pEncry.add(Input, null); pEncry.add(jScrollPane1, null); pEncry.add(jScrollPane2, null); jScrollPane2.getViewport().add(tEncryoutput, null); jScrollPane1.getViewport().add(tEncryinput, null); pDecry.add(bDecry, null); pDecry.add(bLoad, null); pDecry.add(jScrollPane3, null); jScrollPane3.getViewport().add(tDecryinput, null); pDecry.add(jLabel3, null); pDecry.add(jLabel4, null); pDecry.add(jScrollPane4, null); jScrollPane4.getViewport().add(tDecryoutput, null); jTabbedPane1.add(pEncry, "Encryption"); jTabbedPane1.add(pDecry, "Decryption"); getContentPane().add(jPanel1); jMenuItem1.addActionListener(this); jMenuItem2.addActionListener(this); jMenuItem3.addActionListener(this); bEncry.addActionListener(this); bDecry.addActionListener(this); bLoad.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource()==bEncry) { String key=tKey.getText(); System.out.println("the length of key is "+key.length()); if(!checkBi(key)) { JOptionPane.showMessageDialog(null, "please enter binary bits 0 or 1 as key!\n example: 10000101010101010"); } else if((key.length()!=16)) JOptionPane.showMessageDialog(null, "please enter 16 bits as key!\n example: 10000101010101010"); else { //key=getKey(key); String encryText=""; encryText=encry(tEncryinput.getText(),key); tEncryoutput.setText(""); tEncryoutput.append(encryText); } } if(e.getSource()==bDecry) { String key = tKey.getText(); System.out.println("key length is "+key.length()); if (key.length() != 16) JOptionPane.showMessageDialog(null, "please enter 16 bits binary as key!\n example: 10000101010101010"); else { //key = getKey(key); String decryText = ""; decryText = decry(tDecryinput.getText(), key); tDecryoutput.setText(""); tDecryoutput.append(decryText); } } if(e.getSource()==bLoad) { tDecryinput.setText(""); tDecryinput.append(tEncryoutput.getText()); } } public boolean checkBi(String key) { char m=' ';; for(int i=0;i<key.length();i++) { m=key.charAt(i); if((m!='0')&&(m!='1')) { System.out.print("m is "+m+" which is not binary"+i); return false; } } System.out.println("no problem for key"); return true; } public String getKey(String character) { String key=""; String sub1=""; String sub2=""; int c1=character.charAt(0); int c2=character.charAt(1); System.out.println("&&&&&&&&&&&&&&&&&c1 is "+c1); System.out.println("&&&&&&&&&&&&&&&&&c2 is "+c2); sub1=convertDToB(c1,8); System.out.println("sub1 is "+sub1); sub2=convertDToB(c2,8); key=sub1+sub2; System.out.println("&&&&&&&&&&&&&&&&&&&&&&&key is "+key); return key; } public String encry(String text, String key) { String subText=""; String subKey1=""; String subKey2=""; String result=""; subKey1=calSubKey(key)[0]; subKey2=calSubKey(key)[1]; System.out.println("in encry subkey1 is "+subKey1); System.out.println("in encry subkey2 is "+subKey2); //for the odd numbers of length in text if((text.length()%2)==1) text+=" "; for(int i=0; i<text.length();i++) { subText=text.substring(i,i+2); result+=subcry(subText,subKey1,subKey2); //i will be add i again in the for loop i++; } return result; } public String decry(String text, String key) { String subText=""; String subKey1=""; String subKey2=""; String result=""; subKey1=calSubKey(key)[1]; subKey2=calSubKey(key)[0]; //for the odd numbers of length in text if((text.length()%2)==1) text+=" "; for(int i=0; i<text.length();i++) { subText=text.substring(i,i+2); result+=subcry(subText,subKey1,subKey2); i++; } return result; } public String subcry(String subText, String subkey1,String subkey2) { String subEncry=""; String textInBi=textToBi(subText); String passIP=""; String g1=""; String g2=""; String passF=""; String passCombine1=""; String passCombine2=""; String beforeIP_1=""; String passIP_1=""; System.out.println("**************************subkey1 is "+subkey1); System.out.println("**************************subkey2 is "+subkey2); int[] IP={10,6,14,2, 8,16,12,4, 1,13,7,9, 5,11,3,15}; int[] IP_1={9,4,15,8, 13,2,11,5, 12,1,14,7, 10,3,16,6};
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -