package com.doumee.core.utils; 
 | 
  
 | 
import java.io.UnsupportedEncodingException; 
 | 
import java.security.MessageDigest; 
 | 
import java.security.NoSuchAlgorithmException; 
 | 
  
 | 
import javax.crypto.Cipher; 
 | 
import javax.crypto.spec.IvParameterSpec; 
 | 
import javax.crypto.spec.SecretKeySpec; 
 | 
  
 | 
import org.apache.commons.lang.StringUtils; 
 | 
  
 | 
public class EncryptUtil { 
 | 
  
 | 
    private static byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 }; 
 | 
  
 | 
    /** 
 | 
     * des加密 加密以 byte[] 明文输入 ,byte[] 密文输出 
 | 
     *  
 | 
     * @author: j 
 | 
     * @date: 2014-8-21 
 | 
     *  
 | 
     * @param encryptBytes 
 | 
     *            :要加密的字节数组 
 | 
     * @param encryptKey 
 | 
     *            :秘钥,只能是八位字母和数字的组合 
 | 
     * @return 
 | 
     * @throws Exception 
 | 
     */ 
 | 
    public static byte[] encryptDES(byte[] encryptBytes, String encryptKey) 
 | 
            throws Exception { 
 | 
        IvParameterSpec zeroIv = new IvParameterSpec(iv); 
 | 
        SecretKeySpec key = new SecretKeySpec(encryptKey 
 | 
                .getBytes( "UTF-8"), "DES"); 
 | 
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); 
 | 
        cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv); 
 | 
        byte[] encryptedData = cipher.doFinal(encryptBytes); 
 | 
  
 | 
        return Base64Util.Encode(encryptedData); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * @author: j 
 | 
     * @date: 2014-8-21 
 | 
     *  
 | 
     * @param decryptBytes 
 | 
     *            :要解密的字节数组 
 | 
     * @param decryptKey 
 | 
     *            :解密的秘钥,只能是八位字母和数字的组合 
 | 
     * @return 
 | 
     * @throws Exception 
 | 
     */ 
 | 
    public static byte[] decryptDES(byte[] decryptBytes, String decryptKey) 
 | 
            throws Exception { 
 | 
        byte[] byteMi = Base64Util.decode(decryptBytes); 
 | 
        IvParameterSpec zeroIv = new IvParameterSpec(iv); 
 | 
        SecretKeySpec key = new SecretKeySpec(decryptKey 
 | 
                .getBytes( "UTF-8"), "DES"); 
 | 
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); 
 | 
        cipher.init(Cipher.DECRYPT_MODE, key, zeroIv); 
 | 
        byte decryptedData[] = cipher.doFinal(byteMi); 
 | 
  
 | 
        return decryptedData; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * MD5加密 2014-9-25 
 | 
     *  
 | 
     * @param plainText 
 | 
     * @return 
 | 
     * @throws NoSuchAlgorithmException 
 | 
     *             String 
 | 
     * @throws UnsupportedEncodingException 
 | 
     */ 
 | 
    public static String Md5(String plainText) throws NoSuchAlgorithmException, 
 | 
            UnsupportedEncodingException { 
 | 
        MessageDigest md = MessageDigest.getInstance("MD5"); 
 | 
        md.update(plainText.getBytes( "UTF-8")); 
 | 
        byte b[] = md.digest(); 
 | 
  
 | 
        int i; 
 | 
  
 | 
        StringBuffer buf = new StringBuffer(StringUtils.EMPTY); 
 | 
        for (int offset = 0; offset < b.length; offset++) { 
 | 
            i = b[offset]; 
 | 
            if (i < 0) { 
 | 
                i += 256; 
 | 
            } 
 | 
            if (i < 16) { 
 | 
                buf.append("0"); 
 | 
            } 
 | 
            buf.append(Integer.toHexString(i)); 
 | 
        } 
 | 
  
 | 
        return buf.toString(); 
 | 
    } 
 | 
  
 | 
} 
 |