gui.java

来自「java算法实现AES、DES、3DES、MD5、RSA五种加密算法」· Java 代码 · 共 986 行 · 第 1/3 页

JAVA
986
字号
                //用RSA加密
                for(int i=0;i<sj.length;i++){
                	RSA rsa =new RSA();
	 			    BigInteger data = new BigInteger(String.valueOf(sj[i]));
	 			    BigInteger code = rsa.encode(data,rsa.e);
	 			    sj[i]=code.intValue();
                }
                //输出
                StringBuffer sb=new StringBuffer ();
                for(int i=0;i<sj.length;i++){
        	      sb.append(String.valueOf(sj[i]));
                }
                md5output_text.setEditable(true);
                md5output_text.setText(sb.toString());
                md5output_text.setEditable(false);
	 		}
	 		else if(e.getSource() == encryptionRSA_item){//点击加密菜单中的RSA,则用RSA方法加密
	 			output.setEditable(true);
	 			String input_string = input.getText();
	 			String temp_string = keys.getText();
	 			RSA rsa =new RSA();
	 			BigInteger data = new BigInteger(input_string);
                int key=0;
	 			change chang =new change();
	 			for(int i=0;i<temp_string.length();i++){
	 				key=key+((int)Math.pow(10,temp_string.length()-i-1))*(chang.chs(temp_string.charAt(i)));
	 			}
	 			BigInteger code = rsa.encode(data,key);
	 			output.setText(String.valueOf(code));
	 			output.setEditable(false);
	 		}
	 		else if(e.getSource() == decryptionAES_item){//点击解密菜单中的AES,则用AES方法解密
	 				String input_string = input_text.getText();
	 			    String temp_string = temp_text.getText();
	 			    String output_string = output_text.getText();
	 			    decryptfile(temp_string,output_string);
	 			    }
	 		else if(e.getSource() == decryptionDES_item){//点击解密菜单中的DES,则用DES方法解密
	 				String input_string = input_text.getText();
	 			    String temp_string = temp_text.getText();
	 			    String output_string = output_text.getText();
	 			    desdecrypt(temp_string,output_string);
	 		}
	 		else if(e.getSource() == decryption3DES_item){//点击解密菜单中的3DES,则用3DES方法解密
	 				String input_string = input_text.getText();
	 			    String temp_string = temp_text.getText();
	 			    String output_string = output_text.getText();
	 			    des3decrypt(temp_string,output_string);
	 		}
	 		//else if(e.getSource() == decryptionMD5_item){//点击解密菜单中的MD5,则用MD5方法解密
	 		//	String temp_string = temp_text.getText();
	 		//}
	 		else if(e.getSource() == decryptionRSA_item){//点击解密菜单中的RSA,则用RSA方法解密
	 			output.setEditable(true);
	 			String input_string = input.getText();
	 			String temp_string = keys.getText();
	 			RSA rsa =new RSA();
	 			BigInteger data = new BigInteger(input_string);
                int key=0;
	 			change chang =new change();
	 			for(int i=0;i<temp_string.length();i++){
	 				key=key+((int)Math.pow(10,temp_string.length()-i-1))*(chang.chs(temp_string.charAt(i)));
	 			}
	 			BigInteger code = rsa.encode(data,key);
	 			output.setText(String.valueOf(code));
	 			output.setEditable(false);
	 		}
	 		else if(e.getSource() == help_item){//点击帮助菜单中的帮助,则弹出帮助对话框
                help_dialog.setBounds(300,200,440,200);  
	 			help_dialog.setVisible (true);
	 		}
	 		else if(e.getSource() == AES_button){//点击帮助对话框的AES按钮,则文本域显示关于AES的简介
	 			t.setText("简介\n    AES:");//“\n”为换行
	 		}
	 		else if(e.getSource() == DES_button){//点击帮助对话框的DES按钮,则文本域显示关于DES的简介
	 			t.setText("简介\n    DES:");
	 		}
	 		else if(e.getSource() == DES3_button){//点击帮助对话框的3DES按钮,则文本域显示关于3DES的简介
	 			t.setText("简介\n    3DES:");
	 		}
	 		else if(e.getSource() == MD5_button){//点击帮助对话框的MD5按钮,则文本域显示关于MD5的简介
	 			t.setText("简介\n    MD5:");
	 		}
	 		else if(e.getSource() == RSA_button){//点击帮助对话框的RSA按钮,则文本域显示关于RSA的简介
	 			t.setText("简介\n    RSA:");
	 		}
	 		else if(e.getSource() == about_item){//点击帮助菜单中的关于,则弹出关于对话框
	 			about_dialog.setBounds(300,200,350,130); 
	 			about_dialog.setVisible (true);
	 		}
	 		else if(e.getSource() == input_button){
	 			JFileChooser jf=new JFileChooser ();
	 			jf.setDialogTitle("Open class File");
	 			int result = jf.showOpenDialog(null);
                jf.setVisible(true);
                if (result == JFileChooser.APPROVE_OPTION){
                  File file = jf.getSelectedFile();
                  input_text.setText(file.getPath());
                } 
	 		}
	 		else if(e.getSource() == temp_button){
	 			JFileChooser jf=new JFileChooser();
	 			jf.setDialogTitle("Open class File");
	 			int result = jf.showOpenDialog(null);
                jf.setVisible(true);
                if (result == JFileChooser.APPROVE_OPTION){
                  File file = jf.getSelectedFile();
                  temp_text.setText(file.getPath());
                } 
	 		}
	 		else if(e.getSource() == output_button){
	 		    JFileChooser jf=new JFileChooser ();
	 			jf.setDialogTitle("Open class File");
	 			int result = jf.showOpenDialog(null);
                jf.setVisible(true);
                if (result == JFileChooser.APPROVE_OPTION){
                  File file = jf.getSelectedFile();
                  output_text.setText(file.getPath());
                } 	
	 		}
	 		else if(e.getSource() == gen){
	 			key2_text.setEditable(true);
	 			String key1 = key1_text.getText();
	 			int key=0;
	 			change chang =new change();
	 			for(int i=0;i<key1.length();i++){
	 				key=key+((int)Math.pow(10,key1.length()-i-1))*(chang.chs(key1.charAt(i)));
	 			}
	 			RSA rsa = new RSA();
	 			key=(int)rsa.calculateD(key);
	 		    key2_text.setText(String.valueOf(key));
	 		    key2_text.setEditable(false);
	 		}
	 	} catch(Exception en){
			//警告对话框:
			//     警告!
			// 文本框不能为空!
			//请核对后重新输入!
        	JDialog Warning_dialog=new JDialog (this,"Warning!",true);
        	Container Warning_ContainerPane =Warning_dialog.getContentPane();
        	Warning_dialog.setBounds(180,180,310,240);
        	JLabel labelwarning1=new JLabel("警告!");
       		JLabel labelwarning2=new JLabel("文本框不能为空!"); 
        	JLabel labelwarning3=new JLabel("请核对后重新输入!"); 
        	labelwarning1.setForeground(Color.red);
        	labelwarning1.setFont(new Font("宋体",1,25));
        	labelwarning2.setFont(new Font("宋体",1,20));
        	labelwarning3.setFont(new Font("宋体",1,20));
        	JPanel warning1_panel=new JPanel();warning1_panel.add(labelwarning1);
        	JPanel warning2_panel=new JPanel();warning2_panel.add(labelwarning2); 
        	JPanel warning3_panel=new JPanel();warning3_panel.add(labelwarning3); 
        	Warning_ContainerPane.setLayout(new GridLayout(3,1));
        	Warning_ContainerPane.add(warning1_panel);
        	Warning_ContainerPane.add(warning2_panel);
        	Warning_ContainerPane.add(warning3_panel);
            Warning_dialog.setVisible (true);
	 	}
	 }
	 
	 class WL extends WindowAdapter{//窗口事件监听器类
		public void windowClosing(WindowEvent e){
			System.exit(0);
		}
	}
//DES加密算法
    public static void desencrypt(String src,String dest)throws Exception{
      String Algorithm = "DES";
      Security.addProvider(new com.sun.crypto.provider.SunJCE());
      byte [] code=new byte [10000000];
      final byte[] keyBytes = {0x11, 0x22, 0x4F, 0x58, (byte)0x88, 0x10, 0x40, 0x38
                               }; //24字节的密钥
      try{
       DataInputStream in =new DataInputStream( new FileInputStream(src));
       int len=in.available();
       try{
         in.readFully(code);
       }catch(java.io.IOException e){
       }
       byte []coded=new byte[len];
         for(int i=0;i<len;i++){
           coded[i]=code[i];
         }
        //生成密钥
        SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);
        // 加密
        Cipher c1 = Cipher.getInstance(Algorithm);
        c1.init(Cipher.ENCRYPT_MODE, deskey);
        byte [] encoded=c1.doFinal(coded);
        System.out.print(encoded.length);
        DataOutputStream out=new DataOutputStream(new FileOutputStream(dest));
        for(int i=0;i<encoded.length;i++){
          out.writeByte(encoded[i]);
        }//将加密后的信息写入文件
      }catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
      }catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
      }catch (java.lang.Exception e3) {
            e3.printStackTrace();
            throw e3;
      }
    }
     //DES解密算法   
     public static void desdecrypt(String src,String dest)throws Exception{
       String Algorithm = "DES";
       final byte[] keyBytes = {0x11, 0x22, 0x4F, 0x58, (byte)0x88, 0x10, 0x40, 0x38
                                    }; //24字节的密钥
       byte [] code=new byte [10000000];
       int len;
       DataInputStream in =new DataInputStream( new FileInputStream(src));
       len=in.available();
       try{
         in.readFully(code);
       }catch(java.io.IOException e){
       }
       try{
         //生成密钥
         SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);
         //解密
         Cipher c1 = Cipher.getInstance(Algorithm);
         c1.init(Cipher.DECRYPT_MODE, deskey);
         byte []coded=new byte[len];
         for(int i=0;i<len;i++){
           coded[i]=code[i];
         }
         byte [] encoded=c1.doFinal(coded);
         DataOutputStream out=new DataOutputStream(new FileOutputStream(dest));
        for(int i=0;i<encoded.length;i++){
          out.writeByte(encoded[i]);
        }//将加密后的信息写入文件
       }catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
       }catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
       }catch (java.lang.Exception e3) {
            e3.printStackTrace();
            throw e3;
       } 
     }
//3DES加密算法
    public static void des3encrypt(String src,String dest)throws Exception{
       String Algorithm = "DESede";
       Security.addProvider(new com.sun.crypto.provider.SunJCE());
       byte [] code=new byte [10000000];
       int len=0;
       final byte[] keyBytes = {0x11, 0x22, 0x4F, 0x58, (byte)0x88, 0x10, 0x40, 0x38
                               , 0x28, 0x25, 0x79, 0x51, (byte)0xCB, (byte)0xDD, 0x55, 0x66
                               , 0x77, 0x29, 0x74, (byte)0x98, 0x30, 0x40, 0x36, (byte)0xE2}; //24字节的密钥
       try{
         DataInputStream in =new DataInputStream( new FileInputStream(src));
         len=in.available();
         try{
           in.readFully(code);
         }catch(java.io.IOException e){
         }
         byte []coded=new byte[len];
         for(int i=0;i<len;i++){
           coded[i]=code[i];
         }
         //生成密钥
         SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);
         // 加密
         Cipher c1 = Cipher.getInstance(Algorithm);
         c1.init(Cipher.ENCRYPT_MODE, deskey);
         byte [] encoded=c1.doFinal(coded);
         DataOutputStream out=new DataOutputStream(new FileOutputStream(dest));
         for(int i=0;i<encoded.length;i++){
           out.writeByte(encoded[i]);
         }//将加密后的信息写入文件 
       }catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
       }catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
       }catch (java.lang.Exception e3) {
            e3.printStackTrace();
            throw e3;
       }
    }
//3DES解密算法   
     public static void des3decrypt(String src,String dest)throws Exception{
       String Algorithm = "DESede";
       final byte[] keyBytes = {0x11, 0x22, 0x4F, 0x58, (byte)0x88, 0x10, 0x40, 0x38
                               , 0x28, 0x25, 0x79, 0x51, (byte)0xCB, (byte)0xDD, 0x55, 0x66
                               , 0x77, 0x29, 0x74, (byte)0x98, 0x30, 0x40, 0x36, (byte)0xE2}; //24字节的密钥
       byte [] code=new byte [10000000];
       int len;
       DataInputStream in =new DataInputStream( new FileInputStream(src));
       len=in.available();
       try{
         in.readFully(code);
       }catch(java.io.IOException e){
       }
       try{
         //生成密钥
         SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);
         //解密
         Cipher c1 = Cipher.getInstance(Algorithm);
         c1.init(Cipher.DECRYPT_MODE, deskey);
         byte []coded=new byte[len];
         for(int i=0;i<len;i++){
           coded[i]=code[i];
         }
         byte [] encoded=c1.doFinal(coded);
         DataOutputStream out=new DataOutputStream(new FileOutputStream(dest));
         for(int i=0;i<encoded.length;i++){
           out.writeByte(encoded[i]);
         }  
       }catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
       }catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
       }catch (java.lang.Exception e3) {
            e3.printStackTrace();
            throw e3;
       } 
     }
     //AES加密算法
    public static void encryptfile(String src, String dest) throws Exception{
      String AES = "AES";
      Cipher cipher;
      try{
        String pwd = "1235445455445";
        KeyGenerator kgen = KeyGenerator.getInstance(AES);
        kgen.init(128, new SecureRandom(pwd.getBytes()));
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, AES);
        cipher = Cipher.getInstance(AES);
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

⌨️ 快捷键说明

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