com.greenline.hrs.admin.util.encrypt.DESUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.greenline.hrs.admin.util.encrypt.DESUtil.java

Source

/*
 * Project: admin-parent
 * 
 * File Created at 2014-04-09
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package com.greenline.hrs.admin.util.encrypt;

import com.greenline.hrs.admin.util.exception.DecryptException;
import com.greenline.hrs.admin.util.exception.EncryptException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;

/**
 * @author July
 * @version V1.0
 * @type DESUtil
 * @desc DES
 * @date 2014-04-09
 */
public class DESUtil {

    /**
     * 
     */
    private static final Log LOG = LogFactory.getLog(DESUtil.class);

    /**
     * 
     */
    public static final EncryptType TYPE = EncryptType.DES;

    /**
     * 
     */
    private static final String DES = EncryptType.DES.name();

    /**
     * ??
     */
    private DESUtil() {
        super();
    }

    /**
     * Description ?
     *
     * @param data
     * @param key  byte
     * @return datakeynulldata
     * @throws Exception
     */
    public static String encrypt(String data, String key) throws EncryptException {
        try {
            if (data == null || key == null) {
                return data;
            }
            byte[] bt = encrypt(data.getBytes(), key.getBytes());
            return new BASE64Encoder().encode(bt);
        } catch (Exception e) {
            LOG.error("EncryptError:" + data + ":" + key, e);
            throw new EncryptException(e);
        }
    }

    /**
     * Description ?
     *
     * @param data
     * @param key  byte
     * @return datakeynulldata
     * @throws IOException
     * @throws Exception
     */
    public static String decrypt(String data, String key) throws DecryptException {
        try {
            if (data == null || key == null) {
                return data;
            }
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] buf = decoder.decodeBuffer(data);
            byte[] bt = decrypt(buf, key.getBytes());
            return new String(bt);
        } catch (IOException e) {
            LOG.error("IOException:" + data, e);
            throw new DecryptException(e);
        } catch (GeneralSecurityException e) {
            LOG.error("DecryptException:" + data + ":" + key, e);
            throw new DecryptException(e);
        }
    }

    /**
     * Description ?
     *
     * @param data
     * @param key  byte
     * @return
     * @throws Exception
     */
    private static byte[] encrypt(byte[] data, byte[] key) throws GeneralSecurityException {
        // ????
        SecureRandom sr = new SecureRandom();

        // ?DESKeySpec
        DESKeySpec dks = new DESKeySpec(key);

        // ?DESKeySpec??SecretKey
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);

        // Cipher??
        Cipher cipher = Cipher.getInstance(DES);

        // ?Cipher
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

        return cipher.doFinal(data);

    }

    /**
     * Description ?
     *
     * @param data
     * @param key  byte
     * @return
     * @throws Exception
     */
    private static byte[] decrypt(byte[] data, byte[] key) throws GeneralSecurityException {
        // ????
        SecureRandom sr = new SecureRandom();

        // ?DESKeySpec
        DESKeySpec dks = new DESKeySpec(key);

        // ?DESKeySpec??SecretKey
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);

        // Cipher??
        Cipher cipher = Cipher.getInstance(DES);

        // ?Cipher
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

        return cipher.doFinal(data);
    }
}