DES(一)加密解密工具类

import org.junit.Test;

import javax.crypto.Cipher;
import java.security.Key;
import java.security.Security;

/**
 * 描述 des加密工具类
 *
 * @author ***
 * @date 2019/7/10 9:27
 */
public class DesUtil {
    /**
     * 随机字符串
     */
    private static final String RANDOM_STRING = "hoaiudhfliojoajidflkadnfak";

    /**
     * 加密模式为des
     */
    private static final String ENCRYPTION_MODE = "DES";

    /**
     * 加密器
     */
    private static Cipher encryptCipher = null;

    /**
     * 解密器
     */
    private static Cipher decryptCipher = null;

    static {
        Security.addProvider(new com.sun.crypto.provider.SunJCE());
        try {
            Key key = getKey(RANDOM_STRING.getBytes("UTF8"));
            //初始化解密器
            encryptCipher = Cipher.getInstance(ENCRYPTION_MODE);
            encryptCipher.init(Cipher.ENCRYPT_MODE, key);
            //初始化解密器
            decryptCipher = Cipher.getInstance(ENCRYPTION_MODE);
            decryptCipher.init(Cipher.DECRYPT_MODE, key);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 描述 入口-加密字符串
     *
     * @param str
     * @return String
     * @author ***
     * @date 2019/7/9 18:46
     */
    public static String encrypt(String str) throws Exception {
        String cipherStr = byteArrToHexStr(entry(str.getBytes("UTF8")));
        return cipherStr;
    }

    /**
     * 描述 入口-解密字符串
     *
     * @param str
     * @return String
     * @author ***
     * @date 2019/7/9 19:00
     */
    public static String decrypt(String str) throws Exception {
        String cipherStr = new String(decrypt(hexStrToByteArr(str)), "UTF8");
        return cipherStr;
    }

    /**
     * 描述 加密字节数组
     *
     * @param byteArray
     * @return byte[]
     * @author ***
     * @date 2019/7/9 18:43
     */
    static byte[] entry(byte[] byteArray) throws Exception {
        return encryptCipher.doFinal(byteArray);
    }

    /**
     * 描述 解密字节数组
     *
     * @param byteArray
     * @return byte[]
     * @author ***
     * @date 2019/7/9 19:08
     */
    static byte[] decrypt(byte[] byteArray) throws Exception {
        return decryptCipher.doFinal(byteArray);
    }

    /**
     * 描述 加密过程->将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813,
     * 此方法与 public static byte[] hexStrToByteArr(String strIn) 互为可逆的转换过程
     *
     * @param byteArray
     * @return String
     * @author ***
     * @date 2019/7/9 18:47
     */
    private static String byteArrToHexStr(byte[] byteArray) throws Exception {
        int iLength = byteArray.length;

        /**
         * 一个字节,8位二进制
         * 十六进制,一个十六进制可以表示4位二进制
         * 所以一个字节需要2个十六进制‘字符’来表示
         * 所以定义字符串的长度应是数组长度的两倍
         */
        StringBuffer sb = new StringBuffer(iLength * 2);

        for (int i = 0; i < iLength; i++) {
            int intTmp = byteArray[i];
            // 把负数转换为正数
            while (intTmp < 0) {
                intTmp = intTmp + 256;
            }

            // 小于0F的数需要在前面补0
            if (intTmp < 16) {
                sb.append("0");
            }

            sb.append(Integer.toString(intTmp, 16));
        }

        return sb.toString();
    }

    /**
     * 描述 解密过程->将表示16进制值的字符串转换为byte数组,
     * 此方法与public static String byteArrToHexStr(byte[] byteArray) 互为可逆的转换过程
     *
     * @param str
     * @return byte[]
     * @author ***
     * @date 2019/7/9 19:04
     */
    static byte[] hexStrToByteArr(String str) throws Exception {
        byte[] resArray = str.getBytes("UTF8");
        int iLength = resArray.length;

        // 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
        byte[] arrOut = new byte[iLength / 2];
        for (int i = 0; i < iLength; i = i + 2) {
            String strTmp = new String(resArray, i, 2, "UTF8");
            arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
        }

        return arrOut;
    }

    /**
     * 描述 根据随机字符串生成一个秘钥key
     *
     * @param byteArray
     * @return Key
     * @author ***
     * @date 2019/7/9 18:19
     */
    static Key getKey(byte[] byteArray) {
        //定义一个长度为8的字节数组
        byte[] bytesArr = new byte[8];

        //取原始字节数组的前8个,用于生成秘钥
        for (int i = 0; i < bytesArr.length && i < byteArray.length; i++) {
            bytesArr[i] = byteArray[i];
        }

        //生成秘钥
        Key key = new javax.crypto.spec.SecretKeySpec(bytesArr, ENCRYPTION_MODE);
        return key;
    }


    @Test
    public void test01() {
        try {
            String mingwen = "admin123";
            String miwen = encrypt(mingwen);
            System.out.println(miwen);
            String mw = decrypt(miwen);
            System.out.println(mw);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值