DES程序代码(java类库)

来源:互联网 发布:js ajax post 参数 编辑:程序博客网 时间:2024/05/19 22:01

//DES程序共2个类

 //该代码是用java的des类库完成的,如果要用数组做的DES请到

http://blog.csdn.net/hacklew1985/archive/2007/04/14/1564958.aspx

 

//*************************加密类**************************

import java.security.*;
import javax.crypto.*;
import sun.misc.*;

 

//使用DES加密与解密,可对byte[],String类型进行加密与解密
 
public class DesEncrypt  {
 
 
     Key key;
 public DesEncrypt(String str){
    setKey(str);
  }
//生成密匙
   public void setKey(String strKey){
    try{
      KeyGenerator  generator = KeyGenerator.getInstance("DES");
      generator.init(new SecureRandom(strKey.getBytes()));
       this.key = generator.generateKey();
      generator = null;
    }catch (Exception e){
      throw new RuntimeException("Error initializing SqlMap class. Cause: "+e);
    }
  }
 
 // 加密String明文输入,String密文输出
 
  public String getEncString(String strMing) {
    byte[] byteMi = null;
    byte[] byteMing = null;
    String strMi = "";
    BASE64Encoder base64en = new BASE64Encoder();
    try {
      byteMing = strMing.getBytes("UTF8");
      byteMi = this.getEncCode(byteMing);
      strMi = base64en.encode(byteMi);
    } catch (Exception e) {
      throw new RuntimeException("Error initializing SqlMap class. Cause: "+e);
    } finally {
      base64en = null;
      byteMing = null;
      byteMi = null;
    }
    return strMi;
  }
 
  // 解密:以String密文输入,String明文输出
 
  public  String getDesString(String strMi) {
    BASE64Decoder base64De = new BASE64Decoder();
    byte[] byteMing = null;
    byte[] byteMi = null;
    String strMing = "";
    try {
      byteMi = base64De.decodeBuffer(strMi);
      byteMing = this.getDesCode(byteMi);
      strMing = new String(byteMing, "UTF8");
    } catch (Exception e) {
      throw new RuntimeException("Error initializing SqlMap class. Cause: "+e);
    } finally {
      base64De = null;
      byteMing = null;
      byteMi = null;
    }
    return strMing;
  }
 
  //加密:以byte[]明文输入,byte[]密文输出
 
  public byte[] getEncCode(byte[] byteS) {
    byte[] byteFina = null;
    Cipher cipher;
    try {
      cipher = Cipher.getInstance("DES");
      cipher.init(Cipher.ENCRYPT_MODE, key);
      byteFina = cipher.doFinal(byteS);
    } catch (Exception e) {
      throw new RuntimeException("Error initializing SqlMap class. Cause: "+e);
    } finally {
      cipher = null;
    }
    return byteFina;
  }
  
  // 解密以byte[]密文输入,以byte[]明文输出
 
  public byte[] getDesCode(byte[] byteD) {
    Cipher cipher;
    byte[] byteFina = null;
    try {
      cipher = Cipher.getInstance("DES");
      cipher.init(Cipher.DECRYPT_MODE, key);
      byteFina = cipher.doFinal(byteD);
    } catch (Exception e) {
      throw new RuntimeException("Error initializing SqlMap class. Cause: "+e);
    } finally {
      cipher = null;
    }
    return byteFina;
  }
}

**************************GUI类 ************** *********

import java.applet.Applet;
import java.awt.*;
public class DES2 extends Applet{
   
 
 
  /**
  *
  */
  private static final long serialVersionUID = 1L;
 
  //声明变量和对象 
     private DesEncrypt des;
        private String s;
     private String s2;
     private String s3;
 
  //GUI组件  
     private Label label1;
  private Label label2;
  private Label label3;
  private TextField textfield1;
  private TextField textfield2;
  private TextField textfield3;
  private Button button1;
  private Button button2;
        private Panel p;
       
//初始化组件
   public void init(){
   
   
  p=new Panel(); 
 label1=new Label("    明文");
 label2=new Label("    密码");
 label3=new Label("    密文");
 textfield1=new TextField(10);
 textfield2=new TextField(10);
 textfield3=new TextField(10);
 button1=new Button("加密");
 button2=new Button("解密");
 setLayout(new GridLayout(4,3,20,25));
 setBackground(Color.gray);
 
    add(label1);
 add(textfield1);
 add(label2);
 add(textfield2);
 add(label3);
 add(textfield3);
 add(button1);
 add(button2);
}
public boolean action(Event e,Object o){
    
               
      if(e.target instanceof Button){
          s2=textfield2.getText();
          des=new DesEncrypt(s2);
  //对按纽1情况进行的处理     
          if(e.target==button1){
                   s=textfield1.getText();
                   s3=des.getEncString(s);
       textfield3.setText(s3);
   //加密完后清空文本    
       textfield1.setText(null);
                 }
   //对按纽2情况进行的处理      
          else if(e.target==button2){
                    s=des.getDesString(s3);
              textfield1.setText(s);
                }
          }
        return true;   
 }
}
 
原创粉丝点击