a26ec4a6da76acfe9e73510fa94e056902750a78.svn-base 3.1 KB
package com.espeed.yxy.tool;
import java.security.*;  
import javax.crypto.*;  
import sun.misc.*;
/**
 * 程序名称:    	EspeedMail_时速邮箱
 * 程序版本:    	V1.0
 * 作    者:    	深圳市科飞时速网络技术有限公司(0755-88843776)
 * 版权所有:    	深圳市科飞时速网络技术有限公司
 * 技术支持:    	Tech@21gmail.com
 * 单元名称:    DES加密解密工具类
 * 开始时间:    	2013.09.02
 * 程 序 员:    	谢勇
 * 最后修改:    
 * 备    注:		如需修改请通知程序员    
 */
public  class DES_Encrypt
{
	  private Key key;
	  private byte[] byteMi = null;
	  private byte[] byteMing = null;
	  private String strMi= "";
	  private String strM= ""; 
	  /**根据参数生成KEY*/   
	  public void setKey(String strKey)throws Exception{ 		  
         KeyGenerator _generator = KeyGenerator.getInstance("DES");  
         _generator.init(new SecureRandom(strKey.getBytes()));  
         this.key = _generator.generateKey();  
         _generator=null;
	 }
	  
	  /**加密String明文输入,String密文输出 */
	  public void setEncString(String strMing)throws Exception{
		   BASE64Encoder base64en = new BASE64Encoder();  
		    
	     this.byteMing = strMing.getBytes("UTF8");  
	     this.byteMi = this.getEncCode(this.byteMing);  
	     this.strMi = base64en.encode(this.byteMi);
	     this.byteMing = null;  
	     this.byteMi = null;
		  }  
	  /**加密以byte[]明文输入,byte[]密文输出 */   
	  private byte[] getEncCode(byte[] byteS)throws Exception{
	   byte[] byteFina = null;  
	    Cipher cipher;  
	    
	      cipher = Cipher.getInstance("DES");  
	      cipher.init(Cipher.ENCRYPT_MODE,key);  
	      byteFina = cipher.doFinal(byteS);
	     cipher = null;
	       
	   return byteFina;
	  }

	  /** 解密:以String密文输入,String明文输出*/   
	  public void setDesString(String strMi)throws Exception{  
	    BASE64Decoder base64De = new BASE64Decoder();   
	    this.byteMi = base64De.decodeBuffer(strMi);  
	    this.byteMing = this.getDesCode(byteMi);  
	    this.strM = new String(byteMing,"UTF8");  
	    base64De = null;  
	    byteMing = null;  
	    byteMi = null;
	   }
	
	  /** 解密以byte[]密文输入,以byte[]明文输出 */   
	   private byte[] getDesCode(byte[] byteD)throws Exception{
	     Cipher cipher;  
	      byte[] byteFina=null;  	     
	      cipher = Cipher.getInstance("DES");  
	      cipher.init(Cipher.DECRYPT_MODE,key);  
	      byteFina = cipher.doFinal(byteD);
	      cipher=null;	         
	      return byteFina;
	    } 
	  /**返回加密后的密文strMi*/  
	   public String getStrMi()
	    {
	     return strMi;
	    }
	  /**返回解密后的明文*/
	   public String getStrM()
	    {
	     return strM;
	    }
	    
	    public static void main (String[] args) throws Exception
	    {
			DES_Encrypt desEncrypt = new DES_Encrypt();
        	desEncrypt.setKey("06");
			desEncrypt.setEncString("15113887519BBs");
			System.out.println("加密后: "+desEncrypt.getStrMi());
			
			//desEncrypt.setDesString("77K/j0NmDm9ZsWbbiw2f8g==");
			//System.out.println(desEncrypt.getStrM());
			
	    	
		}

	  
	  
	  
}