package com.huatuo.register.base.util;

import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.apache.commons.codec.binary.Base64;

public class DESUtil {

	// Algorithm name
    public static final String KEY = "A2B1C7D8E5F60708";
    // Algorithm name/encryption mode/filling mode
    public static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";

    /** ** Generates the key object *@paramKeyStr Key character string *@returnKey object@throws InvalidKeyException   
     * @throws NoSuchAlgorithmException   
     * @throws InvalidKeySpecException   
     * @throws Exception 
     */
    private static SecretKey keyGenerator(String keyStr) throws Exception {
        byte input[] = HexString2Bytes(keyStr);
        DESKeySpec desKey = new DESKeySpec(input);
        // Create a key factory and use it to convert DESKeySpec to
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(desKey);
        return securekey;
    }

    private static int parse(char c) {
        if (c >= 'a') return (c - 'a' + 10) & 0x0f;
        if (c >= 'A') return (c - 'A' + 10) & 0x0f;
        return (c - '0') & 0x0f;
    }

    // Convert from hexadecimal string to byte array
    public static byte[] HexString2Bytes(String hexstr) {
        byte[] b = new byte[hexstr.length() / 2];
        int j = 0;
        for (int i = 0; i < b.length; i++) {
            char c0 = hexstr.charAt(j++);
            char c1 = hexstr.charAt(j++);
            b[i] = (byte) ((parse(c0) << 4) | parse(c1));
        }
        return b;
    }

    /** * encrypt data *@paramData Indicates the data to be encrypted@paramThe key key *@returnEncrypted data */
    public static String encrypt(String data, String key)  throws Exception{
        Key deskey = keyGenerator(key);
        // Instantiates a Cipher object, which is used to perform actual encryption
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        SecureRandom random = new SecureRandom();
        // Initializes a Cipher object and sets it to the Cipher mode
        cipher.init(Cipher.ENCRYPT_MODE, deskey, random);
        byte[] results = cipher.doFinal(data.getBytes());
        // The encrypted result is transmitted using Base64 encoding
        return Base64.encodeBase64String(results);
    }

    /** * decrypt data *@paramData Data to be decrypted *@paramThe key key *@returnDecrypted data */
    public static String decrypt(String data, String key) throws Exception {
        Key deskey = keyGenerator(key);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        // Initializes a Cipher object and sets it to decryption mode
        cipher.init(Cipher.DECRYPT_MODE, deskey);
        // Perform decryption
        return newString(cipher.doFinal(Base64.decodeBase64(data))); }}Copy the code