DesHelper.java 2.98 KB
Newer Older
yanzg's avatar
yanzg committed
1 2
package com.yanzuoguang.util.helper;

yanzg's avatar
yanzg committed
3 4 5
import com.yanzuoguang.util.contants.SystemContants;
import com.yanzuoguang.util.exception.CodeException;

yanzg's avatar
yanzg committed
6 7 8 9
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
yanzg's avatar
yanzg committed
10
import java.io.UnsupportedEncodingException;
yanzg's avatar
yanzg committed
11 12 13 14 15 16 17 18
import java.security.Key;
import java.security.SecureRandom;

/**
 * DES加密
 *
 * @author 颜佐光
 */
yanzg's avatar
yanzg committed
19
public class DesHelper {
yanzg's avatar
yanzg committed
20 21 22 23 24 25 26 27 28 29 30

    private static final String ALGORITHM_DES = "des";

    /**
     * DES加密
     *
     * @param key     秘钥key
     * @param content 待加密内容
     * @return byte[]
     */
    public static String DESEncrypt(final String key, final String content) {
yanzg's avatar
yanzg committed
31 32 33 34 35 36 37
        try {
            byte[] from = content.getBytes(SystemContants.UTF8);
            byte[] to = processCipher(from, getSecretKey(key), Cipher.ENCRYPT_MODE, ALGORITHM_DES);
            return RsaHelper.encodeBase64(to);
        } catch (UnsupportedEncodingException ex) {
            throw new CodeException("加密失败:" + ex.getMessage(), ex);
        }
yanzg's avatar
yanzg committed
38 39 40 41 42 43 44 45 46
    }

    /**
     * DES解密
     *
     * @param key            秘钥key
     * @param encoderContent 已加密内容
     * @return byte[]
     */
yanzg's avatar
yanzg committed
47 48 49 50 51 52 53 54
    public static String DESDecrypt(final String key, final String encoderContent) {
        try {
            byte[] from = RsaHelper.decodeBase64(encoderContent);
            byte[] to = processCipher(from, getSecretKey(key), Cipher.DECRYPT_MODE, ALGORITHM_DES);
            return new String(to, SystemContants.UTF8);
        } catch (UnsupportedEncodingException ex) {
            throw new CodeException("解密失败:" + ex.getMessage(), ex);
        }
yanzg's avatar
yanzg committed
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 99
    }

    /**
     * 根据key生成秘钥
     *
     * @param key 给定key,要求key至少长度为8个字符
     * @return SecretKey
     */
    public static SecretKey getSecretKey(final String key) {
        try {
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
            SecretKeyFactory instance = SecretKeyFactory.getInstance(ALGORITHM_DES);
            SecretKey secretKey = instance.generateSecret(desKeySpec);
            return secretKey;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


    /**
     * 加密/解密处理
     *
     * @param processData 待处理的数据
     * @param key         提供的密钥
     * @param opsMode     工作模式
     * @param algorithm   使用的算法
     * @return byte[]
     */
    private static byte[] processCipher(final byte[] processData, final Key key,
                                        final int opsMode, final String algorithm) {
        try {
            SecureRandom secureRandom = new SecureRandom();
            Cipher cipher = Cipher.getInstance(algorithm);
            //初始化
            cipher.init(opsMode, key, secureRandom);
            return cipher.doFinal(processData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}