jiaosong
2023-10-18 051abca66db233f5dbde3f69aa706422e1ac9fd9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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();
    }
 
}